Vim Tips Wiki
(Move categories to tip template)
(Change <tt> to <code>, perhaps also minor tweak.)
 
(6 intermediate revisions by 4 users not shown)
Line 1: Line 1:
{{review}}
 
 
{{TipImported
 
{{TipImported
 
|id=272
 
|id=272
 
|previous=271
 
|previous=271
 
|next=273
 
|next=273
|created=July 4, 2002
+
|created=2002
 
|complexity=basic
 
|complexity=basic
 
|author=RobertKellyIV
 
|author=RobertKellyIV
|version=5.7
+
|version=6.0
 
|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 <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>
 
:nnoremap p ]p
  +
:nnoremap <c-p> p
  +
</pre>
   
  +
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.
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:
 
   
  +
==Correcting bad indent while pasting==
:map &lt;c-p&gt; =`]
 
  +
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>
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=`]
  +
</pre>
:nnoremap &lt;c-p&gt; 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 <code>=</code> operator to [[Indenting source code|indent]] the entire inserted text.
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.).
 
   
 
==References==
 
==References==
Line 33: Line 37:
 
*{{help|id==}}
 
*{{help|id==}}
 
*{{help|tag=`%5D|label=`&#93;}}
 
*{{help|tag=`%5D|label=`&#93;}}
  +
*{{help|tag=%5Dp|label=&#93;p}}
  +
  +
==Related plugins==
  +
* [https://github.com/sickill/vim-pasta vim-pasta] allows for pasting with automatic adjusting of indentation to destination context.
   
 
==Comments==
 
==Comments==
See also ]p
 
 
Help says, "Like "p", but adjust the indent to the current line." Also note [p
 
 
----
 

Latest revision as of 05:22, 13 July 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 pasting[]

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.

References[]

Related plugins[]

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

Comments[]