Vim Tips Wiki
(expand a little explaining the discussed options)
(6 intermediate revisions by 4 users not shown)
Line 13: Line 13:
 
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.
 
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 [[VimTip330|How to stop auto indenting]] for automatic indentation issues while you are typing.
+
See [[VimTip330|how to stop auto indenting]] for automatic indentation issues while you are typing.
   
 
==Background==
 
==Background==
Line 21: Line 21:
   
 
==Paste toggle==
 
==Paste toggle==
  +
Vim provides the {{help|prefix=no|'paste'}} option to aid in pasting text unmodified from other applications. You can set it manually like:
Put the following in your [[vimrc]] (change the <F2> to whatever key you want):
 
  +
  +
<pre>
  +
:set paste
  +
:set nopaste
  +
</pre>
  +
  +
Or, Vim offers the {{help|prefix=no|'pastetoggle'}} option to conveniently turn 'paste' on and off with one keypress.
  +
 
Put the following in your [[vimrc]] (change <F2> to whatever key you want):
 
<pre>
 
<pre>
 
set pastetoggle=<F2>
 
set pastetoggle=<F2>
Line 27: Line 36:
   
 
To paste from another application:
 
To paste from another application:
  +
*Start insert mode.
*Press <F2> (toggles the <tt>'paste'</tt> option on).
+
*Press F2 (toggles the <code>'paste'</code> option on).
 
*Use your terminal to paste text from the clipboard.
 
*Use your terminal to paste text from the clipboard.
*Press <F2> (toggles the <tt>'paste'</tt> option off).
+
*Press F2 (toggles the <code>'paste'</code> option off).
   
 
Then the existing indentation of the pasted text will be retained.
 
Then the existing indentation of the pasted text will be retained.
   
If you have a mapping for <F2>, that mapping will apply (and the <tt>'pastetoggle'</tt> function will not operate).
+
You do not have to start insert mode first, but if you are in normal mode and have a mapping for F2, that mapping will apply, and the <code>'pastetoggle'</code> function will not operate.
   
 
Some people like the visual feedback shown in the status line by the following alternative for your vimrc:
 
Some people like the visual feedback shown in the status line by the following alternative for your vimrc:
Line 42: Line 52:
 
</pre>
 
</pre>
   
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 allows you to press <F2> when in insert mode, to toggle <tt>'paste'</tt> on and off. The third line enables displaying whether <tt>'paste'</tt> is turned on in insert mode.
+
The first line sets a mapping so that pressing F2 in normal mode will invert the <code>'paste'</code> option, and will then show the value of that option. The second line allows you to press F2 when in insert mode, to toggle <code>'paste'</code> on and off. The third line enables displaying whether <code>'paste'</code> is turned on in insert mode.
   
 
==References==
 
==References==
Line 49: Line 59:
   
 
==Comments==
 
==Comments==
  +
  +
===Old comments===
  +
*Thanks for your fix Spiiph, and please check what I changed. I think the following is now attended to, and all these old comments can be deleted after a few days. [[User:JohnBeckett|JohnBeckett]] 01:39, January 13, 2010 (UTC)
  +
----
 
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 61: Line 75:
 
set pastetoggle=<F2>
 
set pastetoggle=<F2>
 
</pre>
 
</pre>
:Who's 'me'? The <tt>imap</tt> is superfluous. I'm not sure why it was there from the start, since the <tt>'showmode'</tt> option displays whether <tt>'paste'</tt> is turned on or not.([[User:Spiiph|Spiiph]] 12:08, January 12, 2010 (UTC))
+
:Who's 'me'? The <code>imap</code> is superfluous. I'm not sure why it was there from the start, since the <code>'showmode'</code> option displays whether <code>'paste'</code> is turned on or not.([[User:Spiiph|Spiiph]] 12:08, January 12, 2010 (UTC))

Revision as of 16:40, 20 June 2013

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

Vim provides the 'paste' option to aid in pasting text unmodified from other applications. You can set it manually like:

:set paste
:set nopaste

Or, Vim offers the 'pastetoggle' option to conveniently turn 'paste' on and off with one keypress.

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

set pastetoggle=<F2>

To paste from another application:

  • Start insert mode.
  • 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.

You do not have to start insert mode first, but if you are in normal mode and 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>
set pastetoggle=<F2>
set showmode

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 allows you to press F2 when in insert mode, to toggle 'paste' on and off. The third line enables displaying whether 'paste' is turned on in insert mode.

References

Comments

Old comments

  • Thanks for your fix Spiiph, and please check what I changed. I think the following is now attended to, and all these old comments can be deleted after a few days. JohnBeckett 01:39, January 13, 2010 (UTC)

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'? The imap is superfluous. I'm not sure why it was there from the start, since the 'showmode' option displays whether 'paste' is turned on or not.(Spiiph 12:08, January 12, 2010 (UTC))