Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make imap work in paste mode in Vim?

Tags:

vim

I recently started use :imap jj and am trying to unlearn Ctrl+[.

However, imap doesn't work when paste mode is on. How do I make it work in paste mode?

like image 830
Sam Kong Avatar asked Oct 09 '10 21:10

Sam Kong


People also ask

How do I paste from clipboard in Vim?

When using Vim under Windows, the clipboard can be accessed with the following: In step 4, press Shift+Delete to cut or Ctrl+Insert to copy. In step 6, press Shift+Insert to paste.

What does Vim set paste do?

IIRC when you paste into vim, it essentially thinks that you typed all those characters yourself. So if auto-indent is turned on, it will indent stuff for you, but the pasted text usually contains indentation already so the indent indeed gets "messed up". Switching to paste mode turns off stuff like auto-indent.


2 Answers

You simply can not have mappings work when 'paste' is enabled, since that is the whole point of Vim's paste mode.

Vim's paste mode is meant to allow you to paste stuff even in an instance of console Vim in a terminal—where Vim may not be aware you're using the mouse to paste—and you want to insert literal text form your paste buffer without triggering mappings, or auto/smart/expression indenting, etc.

I suggest you take a look at:

:help 'paste'
:help 'pastetoggle'

like image 156
Heptite Avatar answered Sep 30 '22 10:09

Heptite


Add following snippet to your .vimrc to trigger paste mode automatically when pasting via the terminal:

let &t_SI .= "\<Esc>[?2004h"
let &t_EI .= "\<Esc>[?2004l"

inoremap <special> <expr> <Esc>[200~ XTermPasteBegin()

function! XTermPasteBegin()
   set pastetoggle=<Esc>[201~
   set paste
return ""
endfunction

From: https://coderwall.com/p/if9mda

like image 34
JE42 Avatar answered Sep 30 '22 08:09

JE42