1

I use the following line in ~/.vimrc to allow me to press jk instead of Esc.

:imap jk <Esc>

It works fine until I add a :set paste:

" Pressing jk is same as pressing Esc
:imap jk <Esc>

" Enable paste from system clipboard
" Bug? This disables imap jk <Esc>  
set paste 

Can anyone tell me why this happens? It happens on at least these systems:

Host: Amazon Linux AMI Vim version: 7.4

Host: MacOS 10.14.5 Vim version 8.0.1283

3
  • 1
    You might want to check out the Vi and Vim Stack Exchange for questions on Vim!
    – filbranden
    Aug 6, 2019 at 2:35
  • 1
    Aargh, thanks, filbranden. And thanks even more for covering me this time!
    – tomcam
    Aug 6, 2019 at 22:27
  • Happy to answer questions on Vim on every site... Just on Vi+Vim.SE you'll get more exposure to Vim experts, that's why the recommendation! :-)
    – filbranden
    Aug 6, 2019 at 22:31

1 Answer 1

1

This is expected.

The point of 'paste' is to "avoid unexpected affects" when pasting text and insert-mode mappings are first in the list of features disabled by it.

From :help 'paste':

When the 'paste' option is switched on (also when it was already on):

  • mapping in Insert mode and Command-line mode is disabled
  • abbreviations are disabled
  • [...]

It also disables other options that cause Vim to modify pasted text, such as options related to indentation and text wrapping.

The only exception of keystroke that is special in paste mode (other than ESC, to leave insert mode) is a keystroke set by 'pastetoggle', if one is set. That keystroke will disable paste mode (when enabled), so you can go back to normal insert mode.

Also note that 'paste' mode is not really supposed to be left on, in most cases. Since you're disabling a lot of Vim features when you enable it, you typically only want it one for the duration of a paste and disable it right afterwards.


If you're willing to look at a plug-in recommendation that has useful keybindings for paste mode, I can suggest tpope's vim-unimpaired, which exposes these three mappings (from normal mode):

  • [op: Paste before the current line
  • ]op: Paste after the current line
  • yop: Paste replacing the current line

In all cases, Vim will be put into Insert-mode with 'paste' on, and 'paste' will be turned off as soon as you leave Insert-mode with an ESC. The idea is that pasting is the only thing you'll do with that option turned on, so the sequence of one of those keystrokes, followed by the actual pasted contents, followed by an ESC will become a coherent operation.

See :help pasting for that plug-in for more details.

1
  • 1
    Thanjks, filbranden! I read the help but somehow I glossed over the word "mapping" whereas I think I would have caught it if the word "keyboard" preceded it. You've given me a better way to read the help text.
    – tomcam
    Aug 6, 2019 at 22:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.