简体   繁体   English

如何在Vim中为可视逐行模式添加导图映射?

[英]How to add a map leader mapping for visual linewise mode in vim?

I'm having some trouble making this mapping work: 我在使此映射工作时遇到了一些麻烦:

if exists(":Tabularize")
  "rails routes file alignment in visual mode
  vmap <Leader>ar :Tabularize /'[^']*'\|=>/l1l0<CR>
endif

When I source my vimrc and then: 当我获取vimrc时,然后:

  1. Select several lines with vim's Visual linewise mode (V) 使用vim的可视逐行模式(V)选择几行
  2. Press \\ar \\ar
  3. Visual selection is lost, back to Normal mode and the text remains unchanged. 视觉选择丢失,返回到普通模式,文本保持不变。

I've tested the Tabularize command and it works. 我已经测试过Tabularize命令,它可以正常工作。 I now want to map it to a leader combination but it just doesn't work through the mapping. 现在,我想将其映射到领导者组合,但是它不能通过映射工作。 Only if I actually input the whole command in vim's CLI. 仅当我实际上在vim的CLI中输入了整个命令时。

Note: I've gone through a bunch of map leader questions but none had a solution to my particular issue. 注意:我经历了许多地图负责人问题,但没有一个解决我的特定问题的方法。

The issue is you need to use <bar> instead of | 问题是您需要使用<bar>而不是| in your mapping as | 在您的映射中| separates vim commands. 分隔vim命令。

xnoremap <leader>ar :Tabularize /'[^']*'\<bar>=>/l1l0<cr>

Some other thoughts: 其他一些想法:

  • you should probably be using noremap unless you are using <Plug> or <SID> mappings. 除非使用<Plug><SID>映射,否则您可能应该使用noremap
  • You are using v mode which maps both visual mode and select mode. 您正在使用v模式,该模式同时映射可视模式和选择模式。 It is probably best to use xnoremap instead 最好使用xnoremap代替
  • You may be interested to know that :Tabularize will align all the neighboring lines that match your pattern with out a range 您可能想知道:Tabularize会将与您的模式匹配的所有相邻线对齐,但不超出范围
  • :AddTabularPattern can be used to name a pattern. :AddTabularPattern可用于命名模式。 Add :AddTabularPattern routes /'[^']*'\\<bar>=>/l1l0 to your ~/.vim/after/plugin/tabular.vim . 添加:AddTabularPattern routes /'[^']*'\\<bar>=>/l1l0到您的~/.vim/after/plugin/tabular.vim Now you can do :Tabularize routes 现在您可以:Tabularize routes

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM