在 VIM 中映射命令时的文件完成

发布于 2024-12-28 22:07:09 字数 563 浏览 5 评论 0原文

我经常创建一个临时映射来对特定文件执行某些操作。

典型的例子是这样的:

:map <leader>f :w<cr>:! bundle exec cucumber features/account/sign_in.feature:41<cr>

我希望文件 features/account/sign_in.feature 从当前目录自动完成。

这应该简单地保存当前文件并在 f 上运行命令。

它使我能够轻松处理一段代码,而无需考虑跳转到特定文件并运行它。

问题是创建这样的绑定非常麻烦并且容易出错

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烟雨凡馨 2025-01-04 22:07:09

尽管我认为您的问题应该更清楚,但这里有一些提示:

:nnoremap <leader>f :w<bar>!exec "bundle exec cucumber " . expand('%') . ":" . line('.')<CR>

将在命令中正确保存并插入当前文件名/行号

此外,您将想了解autowrite选项:

:se autowrite

最后,我通常使用 makeprg 来达到这种目的:

:let &makeprg="bundle exec cucumber %"

这具有使用快速修复命令的额外好处(:copen, :colder, <代码>:cnewer)并且(给定正确的 makef)也会跳转到错误行

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:

:se autowrite

Lastly, I usually employ makeprg for this kind of purpose:

: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

梦途 2025-01-04 22:07:09

我不知道答案,但如果我是你,我会使用 cabbr:

:cabbr fas features/account/sign_in.features:

或类似的东西。它不是自动完成,但它确实减少了您所需的击键次数。

另一种替代方法是创建一个从用户命令映射的函数,该函数接受创建映射的参数。如果我没记错的话,您可以自定义用户命令完成,这样可能会起作用。

更新:是的,您可以自定义用户命令。请参阅 :help user-command

这样的内容应该可以帮助您开始:

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

执行此操作后,请确保您的“路径”设置包含当前目录“.”,或者如果您想让它递归搜索“./**”。然后您可以使用 Map 用户命令,如下所示:

:Map fea<tab>ac<tab>

I don't know the answer but if I were you I'd use 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

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 fea<tab>ac<tab>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文