5

In my .vimrc I have:

map <Leader>c :GitGutterToggle<CR>
map <Leader>n :set invnumber<CR>

Is there any way I can combine these two into one Leader entry?

For example:

map <Leader>c :GitGutterToggle && :set invnumber<CR>

I've tried the above, and variations thereof, to no avail.

Thanks.

1
  • 1
    Try asking on vi.SE
    – Shahbaz
    Commented Apr 16, 2017 at 15:42

1 Answer 1

7

Vim uses | to chain ex commands. From :h cmdline-lines:

                                                        :bar :\bar
'|' can be used to separate commands, so you can give multiple commands in one
line.  If you want to use '|' in an argument, precede it with '\'.

These commands see the '|' as their argument, and can therefore not be
followed by another Vim command:

This is followed by a list of commands, which does not include :map or its variants. So, you'll need to use \|. Further down the help is a note about :map, which leads to :h map_bar:

                                                        map_bar 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'.

So, assuming you haven't modified cpoptions:

map <Leader>c :GitGutterToggle <bar> :set invnumber<CR>

Note that you should use noremap (and the more specific nnoremap, vnoremap, etc.) map commands so that you don't surprised by your mappings.

1
  • I was using a literal ("|") character in my ~/.vimrc and it wasn't working. But changing that to ("<bar>") did the trick! Commented Oct 18, 2018 at 10:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .