Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inline comments in vimrc mappings

Tags:

vim

(I've asked the same question on superuser, but realized it wasn't the appropriate page for this question)

I'm cleaning up my .vimrc config and noticed that the section with mappings is excessively sparse due to some mappings having comments (for maintainability and future reference).

The problem is that you can't add a comment on the same line as a mapping, because it will be interpreted as continuation of right-hand side.

Example of current state (sparse):

" Do foo
nmap <Leader>f :foo<Return>

" Do bar
nmap <Leader>b :bar<Return>

Desired state (wrong!):

nmap <Leader>f :foo<Return>  " Do foo
nmap <Leader>b :bar<Return>  " Do bar

Is there a nice way of containing comment in the same line as the mapping?

like image 914
megapctr Avatar asked Jul 12 '14 19:07

megapctr


People also ask

How do I comment a line in Vimrc?

Go to first character on the first line you want to comment out. Hit Ctrl + q in GVIM or Ctrl + v in VIM, then go down to select first character on the lines to comment out. Then press c , and add the comment character.

How do I comment in a Vim script?

Using the up and down arrow key, highlight the lines you wish to comment out. Once you have the lines selected, press the SHIFT + I keys to enter insert mode. Enter your command symbol, for example, # sign, and press the ESC key. Vim will comment out all the highlighted lines.


2 Answers

You can use this method, but be sure to do not include space before |, because it will be the part of the mapping:

nmap <Leader>f :foo<Return>|  " Do foo
nmap <Leader>b :bar<Return>|  " Do bar

The | separates commands in vim, thus the lines above works like this:

nmap <Leader>f :foo<Return>
" Do foo
nmap <Leader>b :bar<Return>
" Do bar

If you want to use the | char in the mapping itself, then see the help of map_bar for additional information:

                                                        *map_bar*
Since the '|' character is used to separate a map command from the next
command, you will have to do something special to include  a '|' in {rhs}.
There are three methods:
  use       works when                    example      ~
  <Bar>     '<' is not in 'cpoptions'     :map _l :!ls <Bar> more^M
  \|        'b' is not in 'cpoptions'     :map _l :!ls \| more^M
  ^V|       always, in Vim and Vi         :map _l :!ls ^V| more^M

(here ^V stands for CTRL-V; to get one CTRL-V you have to type it twice; you
cannot use the <> notation "<C-V>" here).

All three work when you use the default setting for 'cpoptions'.

When 'b' is present in 'cpoptions', "\|" will be recognized as a mapping
ending in a '\' and then another command.  This is Vi compatible, but
illogical when compared to other commands.
like image 115
bimlas Avatar answered Oct 06 '22 19:10

bimlas


No, it's impossible.

From :help map-comments:

It is not possible to put a comment after these commands, because the '"' character is considered to be part of the {lhs} or {rhs}.

like image 23
romainl Avatar answered Oct 06 '22 18:10

romainl