Vim Tips Wiki
(→‎Paste toggle: add solution for pastetoggle under insert)
(Moved comment to comments)
Line 44: Line 44:
 
The first line sets a mapping so that pressing <F2> in normal mode will invert the <tt>'paste'</tt> option, and will then show the value of that option. The second line does the same in insert mode (but insert mode mappings only apply when <tt>'paste'</tt> is off). The third line allows you to press <F2> when in insert mode, to turn <tt>'paste'</tt> off.
 
The first line sets a mapping so that pressing <F2> in normal mode will invert the <tt>'paste'</tt> option, and will then show the value of that option. The second line does the same in insert mode (but insert mode mappings only apply when <tt>'paste'</tt> is off). The third line allows you to press <F2> when in insert mode, to turn <tt>'paste'</tt> off.
   
 
==References==
----
 
 
*{{help|'paste'}}
 
*{{help|'pastetoggle'}}
  +
 
==Comments==
 
I've applied this tip (under VIM 7.2), but when pressing F2 under insert mode it cycles through 3 different modes:
 
I've applied this tip (under VIM 7.2), but when pressing F2 under insert mode it cycles through 3 different modes:
 
<pre>
 
<pre>
Line 57: Line 61:
 
set pastetoggle=<F2>
 
set pastetoggle=<F2>
 
</pre>
 
</pre>
  +
:Who's 'me'? ([[User:Spiiph|Spiiph]] 12:08, January 12, 2010 (UTC))
 
==References==
 
*{{help|'paste'}}
 
*{{help|'pastetoggle'}}
 
 
==Comments==
 

Revision as of 12:08, 12 January 2010

Tip 906 Printable Monobook Previous Next

created 2005 · complexity basic · version 6.0


Pasting text into a terminal running Vim with automatic indentation enabled can destroy the indentation of the pasted text. This tip shows how to avoid the problem.

See How to stop auto indenting for automatic indentation issues while you are typing.

Background

If you use Vim commands to paste text, nothing unexpected occurs. The problem only arises when pasting from another application, and only when you are not using a GUI version of Vim.

In a console or terminal version of Vim, there is no standard procedure to paste text from another application. Instead, the terminal may emulate pasting by inserting text into the keyboard buffer, so Vim thinks the text has been typed by the user. After each line ending, Vim may move the cursor so the next line starts with the same indent as the last. However, that will change the indentation already in the pasted text.

Paste toggle

Put the following in your vimrc (change the <F2> to whatever key you want):

set pastetoggle=<F2>

To paste from another application:

  • Press <F2> (toggles the 'paste' option on).
  • Use your terminal to paste text from the clipboard.
  • Press <F2> (toggles the 'paste' option off).

Then the existing indentation of the pasted text will be retained.

If you have a mapping for <F2>, that mapping will apply (and the 'pastetoggle' function will not operate).

Some people like the visual feedback shown in the status line by the following alternative for your vimrc:

nnoremap <F2> :set invpaste paste?<CR>
imap <F2> <C-O><F2>
set pastetoggle=<F2>

The first line sets a mapping so that pressing <F2> in normal mode will invert the 'paste' option, and will then show the value of that option. The second line does the same in insert mode (but insert mode mappings only apply when 'paste' is off). The third line allows you to press <F2> when in insert mode, to turn 'paste' off.

References

Comments

I've applied this tip (under VIM 7.2), but when pressing F2 under insert mode it cycles through 3 different modes:

-- (insert) --
-- INSERT (paste) --
-- INSERT --

So I have to hit F2 twice under insert mode to enter paste mode. Using this worked for me:

nnoremap <F2> :set invpaste paste?<CR>
imap <F2> <C-O>:set invpaste paste?<CR>
set pastetoggle=<F2>
Who's 'me'? (Spiiph 12:08, January 12, 2010 (UTC))