Vim 将文本左对齐

Vim align text to left side

给出类似的东西:

dadscasd
  cas
    casdc 

如何在vim中将所有线路下到左侧?

dadscasd
cas
casdc 

我安装了 vim tabular。我知道如何对齐图案,但不知道如何将所有内容对齐到左侧。也不确定 vim 表格是否适合这项工作。

首先看一下:h shift-left-right,解释很多。

对于您的用例 :h left 会更好。我会这样做:

可视化 select 所有 3 行(c-v 然后键入 :left) 或者如果您希望整个文件左对齐::%left

如需更多选项,您可以查看 :h formatting

无需任何插件即可轻松做到这一点
在正常模式下,按:

ggVG<<

然后按 . 多次。

命令的解释

  • gg:跳转到文件顶部
  • V : 开始视觉 selection 一次抓取整行
  • G:转到文件末尾(在本例中,select从头到尾检查所有内容)
  • <<: 将 selected 文本向左移动一个缩进
  • . : 重复上一条命令(在本例中,我们应该将文件中的所有内容缩进一左)

如果你不想做所有的行,你只需要select你想移动的行,使用vV。然后按 <<>> 开始缩进。再一次,. 将重复上次发出的命令以使您的生活更轻松。

要了解更多信息,请打开 vim,不要输入任何其他内容,输入 :h << 并按回车键。

无需视觉确认的更快方法是键入

:%left

其中 % 在这种情况下表示当前缓冲区的整个范围,因为它是 1, $.
的别名 参见 :h left:h range

另一个解决方案,如果你想练习你的正则表达式用法

:%s/\v^[ ]+//c

这意味着:

:%  an ed command, apply to entire file 
s    I think this means 'sed' = 'stream edit' = find and replace
/    Use this as the separator for the next 3 fields (the find, the replace, and the sed commands)
\v  Means use "very magic mode" of vim ie characters not 0-9A-Za-z have special meanings and need escaping
^    The start of the line
[ ]   A space character (or whatever characters are present between the [ ]. I believe you could use \s instead to represent any space including tabs
+    Means find 1 or more, but select as many as possible (greedy)
//    ie replace with the 'nothing' between the separators here
c     Means confirm each replacement. You could omit this to do it automatically.