从命令行在 Vim 中执行命令 [英] Execute a command within Vim from the command line
问题描述
有没有办法从命令行对文件执行 Vim 命令?
Is there a way to execute a Vim command on a file from the command line?
我知道事实正好相反:
:!python %
但是如果我想 :retab
一个文件而不在 Vim 中打开它怎么办?例如:
But what if I wanted to :retab
a file without opening it in Vim? For example:
> vim myfile.c
:retab | wq
这将打开 myfile.c,用空格替换制表符,然后保存并关闭.我想以某种方式将这个序列链接到一个命令中.
This will open myfile.c, replace the tabs with spaces, and then save and close. I'd like to chain this sequence together to a single command somehow.
应该是这样的:
> vim myfile.c retab | wq
推荐答案
这有效:
gvim -c "set et|retab|wq" foo.txt
set et
(= set expandtab
) 确保 tab
字符被替换为正确数量的空格(否则,retab
不起作用).
set et
(= set expandtab
) ensures the tab
characters get replaced with the correct number of spaces (otherwise, retab
won't work).
我通常不使用它,但是 vim -c ...
也可以使用
I don't normally use it, but vim -c ...
also works
上面给出的解决方案假定默认的制表位 8 是合适的.例如,如果打算使用 4 个制表位,请使用命令序列 "set ts=4|set et|retab|wq"
.
The solution as given above presumes the default tab stop of eight is appropriate. If, say, a tab stop of four is intended, use the command sequence "set ts=4|set et|retab|wq"
.
这篇关于从命令行在 Vim 中执行命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!