I am using a mac, and I usually paste things in by doing the following:

  • :set paste!
  • cmdp
  • :set paste!

This is really the only way I've been able to paste things into vim reliably (with the help of iTerm), whether it's on a local or remote server (i.e., on a remote server having "a yanked locally means nothing). I was wondering if there was some way to automate this behavior, either in vim or outside it. I don't think it's possible entirely in vim, as it doesn't recognize mac's cmd character. What might be a good way to do this?

  • Do you mean the other way around? :set paste before Cmd+P and :set nopaste after it? – filbranden Apr 28 '20 at 5:06
  • @filbranden yes, good spot thank you. Normally I'll just do paste! but yes I mixed them up (fixed now) – David542 Apr 28 '20 at 5:13

I have been using the paste mappings from vim-unimpaired by Tim Pope, which will drop you on insert mode under set paste and restore nopaste as soon as you leave insert mode.

The mappings are:

  • [op: Paste above the current line (similar to O but with set paste)
  • ]op: Paste below the current line (similar to o but with set paste)
  • yop: Paste replacing the current line (similar to S but with set paste)

So one could use the sequence:

  • ]op
  • Cmd+P
  • Esc

Since leaving Insert mode with "Esc" is enough to restore nopaste mode.

But I also like to paste from when I'm already in Insert mode, for example if I want to write something above the text I'm about to paste.

For that, I use the following mapping:

inoremap <C-e> <C-g>u<C-o>:normal <Plug>unimpairedPaste<cr>

This will use the Ctrl+E key in Insert mode to switch to set paste mode, while setting up the restoring of nopaste after leaving Insert mode. It reuses the internal mapping from vim-unimpaired. Before setting up for pasting, it issues a <C-g>u sequence, to break the "undo" tree, meaning you can undo the paste operation on its own, without undoing the additional text you wrote before your paste.

So then the sequence is:

  • i (or similar), to enter Insert mode
  • enter any text that should precede the paste
  • Ctrl+E, to set paste
  • Cmd+P, to actually paste the contents of the clipboard
  • Esc, to leave Insert mode

There is something called "bracketed paste mode", see :h xterm-bracketed-paste. Check if iTerm supports it. If it does, make sure you don't unset t_BE and t_BD in your configuration. (I don't have a Mac, so I can't test.)

If that doesn't work: I have configures F9 to toggle paste mode:

set pastetoggle=<F9>    " toggle paste option
  • 1
    You shouldnt need the mappings with pastetoggle set – D. Ben Knoble Apr 28 '20 at 12:08
  • 1
    @D.BenKnoble you're right. Those three lines in my vimrc are at least 10 years old. Updating (the answer and my vimrc :-)) – Ralf Apr 28 '20 at 12:30
  • @Ralf looks like it does, thanks for the tip: gitlab.com/gnachman/iterm2/-/wikis/Paste-Bracketing – David542 Apr 28 '20 at 18:29
  • 1
    @David542 Tell us if it works or not and what you had to do. – Ralf Apr 28 '20 at 22:15

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.