Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mapping one key to multiple commands in vim

Tags:

vim

I understand that in order to execute multiple command in one line, for example save and execute pdflatex, I can do the following.

:w | !pdflatex %:t 

Note that the %:t gives you the current file name (without path). This code works fine in Vim. Now, if I want to map the whole thing above to, say ctrl+shift+F6, I'd like to be able to do the following

:nnoremap <C-S-F6> :w | !pdflatex %:t<CR> 

But this doesn't work, and gives me the following error.

:!pdflatex paper.tex<CR> /bin/bash: -c: line 0: syntax error near unexpected token `newline' /bin/bash: -c: line 0: `pdflatex paper.tex<CR>' 

Does this mean that I can't map ctrl+shift+F6 to the desired function, save and execute pdflatex? What can I do to get around this?

like image 227
Ray Avatar asked Apr 21 '14 19:04

Ray


People also ask

How do I run multiple commands in vim?

You can execute more than one command by placing a | between two commands. This example substitutes for htm, then moves on to JPEG, then GIF. The second command (and subsequent commands) are only executed if the prior command succeeds.

How do I map a key in vim?

To map a sequence of keys to execute another sequence of keys, use the ':map' command. For example, the following command maps the <F2> key to display the current date and time. The ':map' command creates a key map that works in normal, visual, select and operator pending modes.

Does vim allow for custom key bindings?

Creating a set of key bindings that is familiar, eases the learning curve of any new editor, and the flexibility vim allows in this configuration makes it easy to not only leverage the power of vim, but also make it feel like a familiar old friend.

What is silent key in vim?

<silent> tells vim to show no message when this key sequence is used. <leader> means the key sequence starts with the character assigned to variable mapleader -- a backslash, if no let mapleader = statement has executed yet at the point nmap executes.


1 Answers

Assuming <C-S-F6> actually works (it probably won't in CLI Vim), you must escape the bar or use <bar> instead:

:nnoremap <C-S-F6> :up \| !pdflatex %:t<CR> :nnoremap <C-S-F6> :up <bar> !pdflatex %:t<CR> 

See :help map_bar.

like image 155
romainl Avatar answered Oct 02 '22 18:10

romainl