在 VIM 中映射命令时的文件完成
我经常创建一个临时映射来对特定文件执行某些操作。
典型的例子是这样的:
:map <leader>f :w<cr>:! bundle exec cucumber features/account/sign_in.feature:41<cr>
我希望文件 features/account/sign_in.feature
从当前目录自动完成。
这应该简单地保存当前文件并在
上运行命令。
它使我能够轻松处理一段代码,而无需考虑跳转到特定文件并运行它。
问题是创建这样的绑定非常麻烦并且容易出错。
VIM 不允许映射时完成文件名。另外使用 %
是完全不同的(我想静态地映射到文件)。
有什么更好的方法吗?
谢谢。
I often create a temporary mapping to do something on a particular file.
Typical example is this:
:map <leader>f :w<cr>:! bundle exec cucumber features/account/sign_in.feature:41<cr>
I want the file features/account/sign_in.feature
to be autocompleted from the current directory.
This should simply save the current file and shell out on <leader>f
to run the command.
It allows me to easily work on a piece of code not thinking about jumping to a particular file and run it.
The problem is that creating such a binding is cumbersome and error prone.
VIM doesn't allow completing the file name when mapping. Also using %
is totally different ( I want to map to a file statically).
What is a better way of doing it?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尽管我认为您的问题应该更清楚,但这里有一些提示:
将在命令中正确保存并插入当前文件名/行号
此外,您将想了解
autowrite
选项:最后,我通常使用
makeprg
来达到这种目的:这具有使用快速修复命令的额外好处(
:copen
,:colder
, <代码>:cnewer)并且(给定正确的makef
)也会跳转到错误行Allthough I think your question should be made a lot clearer, here are some hints:
Will correctly save and insert current filename/line number in the command
Also, you will want to know about the
autowrite
option:Lastly, I usually employ
makeprg
for this kind of purpose:This has the added benefit of working with quickfix commands (
:copen
,:colder
,:cnewer
) and (given a correctmakef
) also jumping to error lines我不知道答案,但如果我是你,我会使用 cabbr:
或类似的东西。它不是自动完成,但它确实减少了您所需的击键次数。
另一种替代方法是创建一个从用户命令映射的函数,该函数接受创建映射的参数。如果我没记错的话,您可以自定义用户命令完成,这样可能会起作用。
更新:是的,您可以自定义用户命令。请参阅
:help user-command
这样的内容应该可以帮助您开始:
执行此操作后,请确保您的“路径”设置包含当前目录“.”,或者如果您想让它递归搜索“./**”。然后您可以使用
Map
用户命令,如下所示:I don't know the answer but if I were you I'd use cabbr:
Or something like that. It's not autocomplete, but it sure does reduce the number of keystrokes you need.
Another alternative is to create a function mapped from a user command that accepts an argument that creates the mapping. If I remember it correctly you can customize the user command completion so that might work.
Update: yes you can customize user command. See
:help user-command
Something like this should get you started:
Once you do that, ensure that you 'path' setting includes the current directory ".", or if you want to make it search recursively "./**". Then you can use the
Map
user command like this: