what is the meaning of \ | in a map vim command? - vim

Here is a map command.
nnoremap <F5> :w\|!R %<CR>
1.what is the meaning of \ here?
2. does | mean pipe ?

The | character separates two Ex commands, see :help :|. It's like ; in programming languages like C and Java. It has nothing to do with pipes; Vim hasn't that concept (which is typically found in shells).
It is escaped here so that the entire command sequence belongs to the mapping; i.e. it maps to :w|!R %<CR>. Without escaping, Vim would execute the following instead:
:nnoremap <F5> :w
:!R %<CR>
Note that you can also write <Bar> (cp. :help key-notation) instead of \|, and the former is more frequently used.

Related

How can you map a binding with a pipe?

e.g. I want to map
nnoremap <a-v> :vs | term<cr>
but vim is interpreting this as nnoremap <a-v> :vs followed by the separate command term<cr>. How do I declare the the associativity here? Using eval and execute do not work.
I asked too soon, figured it out a minute later: you simply escape the pipe:
nnoremap <a-v> :vs \| term<CR>

How to map a key in a command line argument inside a key mapping?

The naive approach
nnoremap <leader>s :!echo "foo" <bar> vim -c="nnoremap x :q<cr>" -<cr>
displays errors. Is there a way to achieve this?
Figured it out. There are two problems with my original code. The first being an equals sign after -c. For the other, the solution is to use <lt>cr> instead of <cr>. So the entire command would look like this:
nnoremap <leader>s :!echo "foo" <bar> vim -c "nnoremap x :q<lt>cr>" -<cr>
:help <lt>

Error when loading .vimrc containing substitution

How do I map a substitution to a key in my .vimrc without getting an error when I source it?
I'm trying to add
nnoremap <leader>re :'<,'>s/<%=*\s//g | '<,'>s/\s-*%>//g
to my .vimrc but when I do so and reload the file I get the following error:
The problem is that you're combining two :s commands, but the command separator | concludes the :map command, so that the second substitution is executed immediately, causing the error. You need to escape the |, or better use the special <Bar> notation inside mappings:
nnoremap <leader>re :'<,'>s/<%=*\s//g <Bar> '<,'>s/\s-*%>//g
PS: Wouldn't it be more natural to define the mapping in visual mode (as it works on the last selection, anyway)? With a :vmap, the first '<,'> range will be inserted automatically:
xnoremap <leader>re :s/<%=*\s//g <Bar> '<,'>s/\s-*%>//g

Vimgrep mapping not working

If I run manually the command
:vimgrep /\CTODO:\|FIXME:\|NOTE:/ %
everything works as expected. However when i try the following mapping:
nnoremap <leader>tl :vimgrep /\CTODO:\|FIXME:\|NOTE:/ %<CR>
I get the error:
E480: No match: \CTODO:|FIXME:|NOTE:
I don't why the map is not working.
| is used to separated vim commands. You have escaped the | to treat the pipe as one command however this changes regex into \CTODO:|FIXME:|NOTE: Look at the mapping via :nmap to see how the mapping has changed.
I suggest you use <bar> instead of the pipe symbol in your mapping:
nnoremap <leader>tl :vimgrep /\CTODO:\<bar>FIXME:\<bar>NOTE:/ %<CR>

I want to update the file (if necessary) and run the system command on current file

I realize that I can :nmap <leader>rc :!cat %<CR> to provide an easy set of triggers, but I would like to do this instead.
nmap <leader>rc :up :!cat %<CR> but it complains about needing only one filename. How do I get vim to recognize both commands, in series?
You are missing a <CR> after :up. <CR> tells vim you want a carriage return here.
nmap <leader>rc :up<CR> :!cat %<CR>
The reason up is complaining about multiple file names is that it sees :!cat and %<CR> as two arguments to up.
So the new macro executes
:up
:!cat %
instead of
:up :!cat %
(Side Note: you should probably use nnoremap instead of nmap)
ZyX recommends using the following mapping instead.
nnoremap ,rc :up\|execute "!cat" shellescape(#%, 1)<CR>
This uses | to separate commands and escapes the %. Escaping the % leads to a more robust mapping just incase the filename contains special characters.
Help for :h execute and :h shellescape

Resources