Wikia

Vim Tips Wiki

Changes: Format pasted text automatically

Edit

Back to page

(Change <tt> to <code>, perhaps also minor tweak.)
 
Line 16: Line 16:
 
</pre>
 
</pre>
   
Some users prefer to have all of their text pasted with indenting intact. In order to make this easier, the <tt>]p</tt> command can be mapped to the <tt>p</tt> command, so that whenever <tt>p</tt> is used, <tt>]p</tt> will be executed. The following can be added to the .vimrc to accomplish this:
+
Some users prefer to have all of their text pasted with indenting intact. In order to make this easier, the <code>]p</code> command can be mapped to the <code>p</code> command, so that whenever <code>p</code> is used, <code>]p</code> will be executed. The following can be added to the .vimrc to accomplish this:
 
<pre>
 
<pre>
 
:nnoremap p ]p
 
:nnoremap p ]p
Line 22: Line 22:
 
</pre>
 
</pre>
   
This simply maps normal mode <tt>p</tt> to what <tt>]p</tt> normally does. While <tt>ctrl+p</tt> now performs just <tt>p</tt> without the indenting functionality.
+
This simply maps normal mode <code>p</code> to what <code>]p</code> normally does. While <code>ctrl+p</code> now performs just <code>p</code> without the indenting functionality.
   
 
==Correcting bad indent while pasting==
 
==Correcting bad indent while pasting==
Unfortunately, the <tt>]p</tt> command will only adjust indent to match the current line, it will not re-indent the pasted text to correct it according to your current indent rules. You can do this as well, using the special mark, <tt>`]</tt>. This will jump to the last character of the paste, so you could change the p mapping above to:
+
Unfortunately, the <code>]p</code> command will only adjust indent to match the current line, it will not re-indent the pasted text to correct it according to your current indent rules. You can do this as well, using the special mark, <code>`]</code>. This will jump to the last character of the paste, so you could change the p mapping above to:
 
<pre>
 
<pre>
 
:nnoremap p p=`]
 
:nnoremap p p=`]
 
</pre>
 
</pre>
   
This takes advantage of the fact that a paste operation will place the cursor at the beginning of the inserted text, and uses the <tt>=</tt> operator to [[Indenting source code|indent]] the entire inserted text.
+
This takes advantage of the fact that a paste operation will place the cursor at the beginning of the inserted text, and uses the <code>=</code> operator to [[Indenting source code|indent]] the entire inserted text.
   
 
==References==
 
==References==

Latest revision as of 05:22, July 13, 2012

Tip 272 Printable Monobook Previous Next

created 2002 · complexity basic · author RobertKellyIV · version 6.0


If a user would like to paste text into a buffer and have that text indented properly so that the text matches surrounding indents, the following command can be given:

]p

Some users prefer to have all of their text pasted with indenting intact. In order to make this easier, the ]p command can be mapped to the p command, so that whenever p is used, ]p will be executed. The following can be added to the .vimrc to accomplish this:

:nnoremap p ]p
:nnoremap <c-p> p

This simply maps normal mode p to what ]p normally does. While ctrl+p now performs just p without the indenting functionality.

Correcting bad indent while pastingEdit

Unfortunately, the ]p command will only adjust indent to match the current line, it will not re-indent the pasted text to correct it according to your current indent rules. You can do this as well, using the special mark, `]. This will jump to the last character of the paste, so you could change the p mapping above to:

:nnoremap p p=`]

This takes advantage of the fact that a paste operation will place the cursor at the beginning of the inserted text, and uses the = operator to indent the entire inserted text.

ReferencesEdit

Related pluginsEdit

  • vim-pasta allows for pasting with automatic adjusting of indentation to destination context.

CommentsEdit

Around Wikia's network

Random Wiki