I would like automating the following in my .vimrc:

:'<,'>s/ \+| \+/|/g 

With something like the following but it does not work:

vmap <Leader>rt :s/ \+| \+/|/g<CR>

it seems the whitespace has to be escaped, I tried \s but there is another warning. What is the easiest solution ?


You can use <space> to describe a space, although it is not mandatory. However, | has to be escaped: \|

the result would be: vmap <Leader>rt :s/ \+\| \+/\|/g<cr> (or vmap <Leader>rt :s/<space>\+\|<space>\+/\|/g<cr> with <space>)

On top of it, I'd suggest to use vnoremap, unless you actively need a recursive mapping.

  • 2
    Well, xnoremap, unless you need select mode. And theoretically <space> is unnecessary here.
    – D. Ben Knoble
    Nov 16 '20 at 14:16
  • Indeed, editing.
    – padawin
    Nov 16 '20 at 14:42
  • 1
    You might want to add a reference to :help map-bar, which explains why this is needed here.
    – filbranden
    Nov 16 '20 at 16:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.