Monday, February 22, 2010

Vim multiple commands in configuration file

I was struggling to come up with a means of mapping a keystroke to execute multiple Vim commands, within the .vimrc settings file.
Somewhere I read that to string multiple commands on one line, they must be separated with a | (pipe). That works inside of Vim, but causes errors when placed inside the configuration file.
Then I tried using :argdo, which also worked inside of Vim but not in the configuration file.

Finally I came upon a blog post which solved my problem. It appears that in the configuration file, pipes for separating commands must be escaped with a forward slash \.

I used to have a line in my configuration file:
noremap <silent> <C-h> :noh <CR>

This means that pushing Control-h would remove search highlighting.

Now it is:
noremap <silent> <C-h> :noh\| :set number!\| :set list! <CR>

The added commands toggle visibility of line numbers and whitespace. I like having those things visible, but I often need to copy text from vim to somewhere else and the line numbers and line-terminators get copied with it if they are displayed.

As for why I added these commands to an existing mapping: I am a vim novice and unaware of many vim shortcuts, so I decided not to accidentally overwrite any shortcuts. I figured that the existing Ctrl-h command was used rarely enough so that the annoyance of having to now push it twice for clearing highlighting (to negate the toggling produced by the first push) was an acceptable price to pay. And also one less shortcut to remember.

No comments:

Post a Comment