Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/vim/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在vim中为特定文件名设置快捷方式?_Vim - Fatal编程技术网

如何在vim中为特定文件名设置快捷方式?

如何在vim中为特定文件名设置快捷方式?,vim,Vim,在我的vimrc里,我有 " Rspec Shortcuts nnoremap <leader>t :!clear; bundle exec rspec % --fail-fast<cr> " Minitest Shortcuts nnoremap <leader>t :!clear; bundle exec ruby -w -Itest %<cr> “Rspec快捷方式 nnoremap t:!清除;bundle exec rspec%--快

在我的vimrc里,我有

" Rspec Shortcuts
nnoremap <leader>t :!clear; bundle exec rspec % --fail-fast<cr>

" Minitest Shortcuts
nnoremap <leader>t :!clear; bundle exec ruby -w -Itest %<cr>
“Rspec快捷方式
nnoremap t:!清除;bundle exec rspec%--快速失败
“小型快捷方式
nnoremap t:!清楚的bundle exec ruby-w-Itest%
我想使这些快捷方式与文件名相适应

我尝试过这样设置条件:

if @% =~ '*_spec.rb'
  nnoremap <leader>t :!clear; bundle exec rspec % --fail-fast<cr>
endif

if @% =~ '*_test.rb'
  nnoremap <leader>t :!clear; bundle exec ruby -w -Itest %<cr>
endif
if@%=~'*_spec.rb'
nnoremap t:!清楚的bundle exec rspec%--快速失败
恩迪夫
如果@%=~'*_test.rb'
nnoremap t:!清楚的bundle exec ruby-w-Itest%
恩迪夫
但它没有起作用。
如果没有这些条件,快捷方式当然会起作用。

您需要做两件事:

  • 通过添加
  • 可能通过
    :autocmd
    在特定文件名上设置这些映射
映射将如下所示:

nnoremap <buffer> <leader>t :!clear; bundle exec rspec % --fail-fast<cr>
nnoremap <buffer> <leader>t :!clear; bundle exec ruby -w -Itest %<cr>
另一个选择是将这些映射放入
~/.vim/after/ftplugin/ruby.vim
,如下所示:

if @% =~ '*_spec.rb'
  nnoremap <buffer> <leader>t :!clear; bundle exec rspec % --fail-fast<cr>
endif

if @% =~ '*_test.rb'
  nnoremap <buffer> <leader>t :!clear; bundle exec ruby -w -Itest %<cr>
endif
if @% =~ '*_spec.rb'
  nnoremap <buffer> <leader>t :!clear; bundle exec rspec % --fail-fast<cr>
endif

if @% =~ '*_test.rb'
  nnoremap <buffer> <leader>t :!clear; bundle exec ruby -w -Itest %<cr>
endif
:h :map-local
:h :autocmd
:h BufNewFile
:h BufRead