[英]Format code with VIM using external commands
I know that using VIM I can format C++ code just using 我知道使用VIM我可以使用格式化C ++代码
gg=G
Now I have to format 30 files, so doing it by hand becomes tedious. 现在我必须格式化30个文件,因此手动操作变得乏味。 I had a look how to do it passing external commands to VIM, so I tried
我看看如何将外部命令传递给VIM,所以我试过了
vim -c gg=G -c wq file.cpp
but it does not work. 但它不起作用。
Can you give me a hint? 你能给我一个提示吗?
Thanks 谢谢
为什么不在缓冲区中加载所有文件并使用bufdo
在所有文件上执行命令?
:bufdo "execute normal gg=G"
Change -c gg=G
to -c 'normal! gg=G'
改变
-c gg=G
到-c 'normal! gg=G'
-c 'normal! gg=G'
. -c 'normal! gg=G'
。 -c
switch accepts only ex mode commands, gg=G
are two normal mode commands. -c
switch仅接受ex模式命令, gg=G
是两个普通模式命令。
I prefer a slight change on the :bufdo
answer. 我更喜欢对
:bufdo
回答略有改动。 I prefer the arg list instead of the buffer list, so I don't need to worry about closing current buffers or opening up new vim session. 我更喜欢arg列表而不是缓冲区列表,所以我不需要担心关闭当前缓冲区或打开新的vim会话。 For example:
例如:
:args ~/src/myproject/**/*.cpp | argdo execute "normal gg=G" | update
args
sets the arglist, using wildcards ( **
will match the current directory as well as subdirectories) args
使用通配符设置arglist( **
将匹配当前目录以及子目录) |
lets us run multiple commands on one line argdo
runs the following commands on each arg (it will swallow up the second |
) argdo
在每个arg上运行以下命令(它将吞没第二个|
) execute
prevents normal
from swallowing up the next pipe. execute
可防止normal
吞下下一个管道。 normal
runs the following normal mode commands (what you were working with in the first place) normal
运行以下正常模式命令(您首先使用的是什么) update
is like :w
, but only saves when the buffer is modified. update
类似于:w
,但仅在修改缓冲区时保存。 This :args ... | argdo ... | update
这个
:args ... | argdo ... | update
:args ... | argdo ... | update
:args ... | argdo ... | update
pattern is very useful for any sort of project wide file manipulation (eg search and replace via '%s/foo/bar/ge' or setting uniform fileformat
or fileencoding
). :args ... | argdo ... | update
模式对于任何类型的项目范围文件操作非常有用(例如,通过'%s / foo / bar / ge'搜索和替换或设置统一fileformat
或文件fileencoding
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.