Vim Tips Wiki
(Remove html character entities)
(Reviewed, updated functionality.)
Line 1: Line 1:
{{review}}
 
 
{{TipImported
 
{{TipImported
 
|id=272
 
|id=272
Line 9: Line 8:
 
|version=5.7
 
|version=5.7
 
|rating=32/11
 
|rating=32/11
|category1=
+
|category1=Usage
 
|category2=
 
|category2=
 
}}
 
}}
  +
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:
In times past I used a nice editor that had the neat feature of automatically setting pasted text to the proper indent level. Recently I've begun to miss this so I went looking in the help and came up with.
 
  +
<pre>]p</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:
=`]
 
  +
<pre>
 
which will format to the end of the pasted text. Perfect to call right after you past something as the cursor ends up at the top of the pasted text, thus the mapping:
 
 
:map <c-p> =`]
 
 
However I wanted the formatting to automatically be done so it was two simple (once I figured out how!) nnoremap:
 
 
" [Feral:185/02@14:27] map c-p to what p was (past with no formatting), map p to p and = to end of pasted text.
 
 
:nnoremap p p=`]
 
:nnoremap p p=`]
 
:nnoremap <c-p> p
 
:nnoremap <c-p> p
  +
</pre>
   
This simply (as the comment hints at) maps normal mode p to what p did (paste) then = to `] (last character in the previously changed text). While ctrl+p just does what p did. (just in case you find you don't want a bit of text auto formatted.).
+
This simply maps normal mode <tt>p</tt> to what <tt>]p</tt>. While <tt>ctrl+p</tt> now performs just <tt>p</tt> without the indenting functionality.
   
 
==References==
 
==References==
Line 33: Line 27:
 
*{{help|id==}}
 
*{{help|id==}}
 
*{{help|tag=`%5D|label=`&#93;}}
 
*{{help|tag=`%5D|label=`&#93;}}
  +
*{{help|]p}}
   
 
==Comments==
 
==Comments==
See also ]p
 
 
Help says, "Like "p", but adjust the indent to the current line." Also note [p
 
 
----
 

Revision as of 23:08, 11 January 2011

Tip 272 Printable Monobook Previous Next

created July 4, 2002 · complexity basic · author RobertKellyIV · version 5.7


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. While ctrl+p now performs just p without the indenting functionality.

References

Comments