简体   繁体   中英

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:

  1. Select several lines with vim's Visual linewise mode (V)
  2. Press \\ar
  3. Visual selection is lost, back to Normal mode and the text remains unchanged.

I've tested the Tabularize command and it works. 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.

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 | in your mapping as | separates vim commands.

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

Some other thoughts:

  • you should probably be using noremap unless you are using <Plug> or <SID> mappings.
  • You are using v mode which maps both visual mode and select mode. It is probably best to use xnoremap instead
  • You may be interested to know that :Tabularize will align all the neighboring lines that match your pattern with out a range
  • :AddTabularPattern can be used to name a pattern. Add :AddTabularPattern routes /'[^']*'\\<bar>=>/l1l0 to your ~/.vim/after/plugin/tabular.vim . Now you can do :Tabularize routes

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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