简体   繁体   English

使用外部命令使用VIM格式化代码

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM