[英]File completion when mapping commands in 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. 我希望从当前目录自动完成文件features/account/sign_in.feature
。
This should simply save the current file and shell out on <leader>f
to run the command. 这应该只是将当前文件和shell保存在<leader>f
以运行命令。
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 . 映射时 VIM不允许填写文件名 。 Also using %
is totally different ( I want to map to a file statically ). 使用%
也完全不同(我想静态映射到文件)。
What is a better way of doing it? 这样做的更好方法是什么?
Thanks. 谢谢。
Allthough I think your question should be made a lot clearer, here are some hints: 虽然我认为你的问题应该更清楚,这里有一些提示:
:nnoremap <leader>f :w<bar>!exec "bundle exec cucumber " . expand('%') . ":" . line('.')<CR>
Will correctly save and insert current filename/line number in the command 将正确保存并在命令中插入当前文件名/行号
Also, you will want to know about the autowrite
option: 此外,您还需要了解autowrite
选项:
:se autowrite
Lastly, I usually employ makeprg
for this kind of purpose: 最后,我通常使用makeprg
来达到这种目的:
:let &makeprg="bundle exec cucumber %"
This has the added benefit of working with quickfix commands ( :copen
, :colder
, :cnewer
) and (given a correct makef
) also jumping to error lines 这有一个额外的好处,使用quickfix命令( :copen
, :colder
, :cnewer
)和(给定一个正确的makef
)也跳转到错误行
I don't know the answer but if I were you I'd use cabbr: 我不知道答案,但如果我是你,我会使用cabbr:
:cabbr fas features/account/sign_in.features:
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
请参阅:help user-command
Something like this should get you started: 这样的事情应该让你开始:
command -complete=file_in_path -nargs=1 Map call CreateMap(expand('<args>'))
function CreateMap(filename)
exec 'map <leader>f :w<cr>:! bundle exec cucumber ' . a:filename . ':41<cr>'
endfun
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: 然后你可以像这样使用Map
用户命令:
:Map fea<tab>ac<tab>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.