(Change <tt> to <code>, perhaps also minor tweak.)
Line 3:
Line 3:
|previous=1592
|previous=1592
|next=1594
|next=1594
−
|created=March 3, 2008
+
|created=2008
|complexity=basic
|complexity=basic
|author=
|author=
Line 25:
Line 25:
</pre>
</pre>
−
In Vim, put the cursor on the closing brace '<tt>}</tt>' in the following. Then press Ctrl-v.
+
In Vim, put the cursor on the closing brace '<code>}</code>' in the following. Then press Ctrl-v.
<pre>
<pre>
if (x==1){
if (x==1){
Line 43:
Line 43:
==Explanation==
==Explanation==
−
The command <tt>"+P</tt> pastes the system clipboard before the cursor position. The pasted text becomes the "last change" which sets mark <tt>'[</tt> to the beginning of the pasted text, and mark <tt>']</tt> to the end. After the <tt>P</tt> command, the cursor is at the <tt>'[</tt> mark, and the command <tt>=']</tt> applies the <tt>=</tt> filter to the pasted text (all lines from the cursor line to the line with the <tt>']</tt> mark, inclusive). Assuming the defaults, the <tt>=</tt> command applies the indent rules for the C language, or for the language of the current buffer, if your syntax rules have defined an indent expression. In insert mode, Ctrl-o (<tt><c-o></tt>) executes one normal-mode command, then returns to insert mode.
+
The command <code>"+P</code> pastes the system clipboard before the cursor position. The pasted text becomes the "last change" which sets mark <code>'[</code> to the beginning of the pasted text, and mark <code>']</code> to the end. After the <code>P</code> command, the cursor is at the <code>'[</code> mark, and the command <code>=']</code> applies the <code>=</code> filter to the pasted text (all lines from the cursor line to the line with the <code>']</code> mark, inclusive). Assuming the defaults, the <code>=</code> command applies the indent rules for the C language, or for the language of the current buffer, if your syntax rules have defined an indent expression. In insert mode, Ctrl-o (<code><c-o></code>) executes one normal-mode command, then returns to insert mode.
Here is a mapping that provides a "smart paste" to insert the system clipboard and automatically indent the pasted text. This is useful when pasting code from another application into the program you are working on. You could put the following lines in your vimrc:
The command "+P pastes the system clipboard before the cursor position. The pasted text becomes the "last change" which sets mark '[ to the beginning of the pasted text, and mark '] to the end. After the P command, the cursor is at the '[ mark, and the command ='] applies the = filter to the pasted text (all lines from the cursor line to the line with the '] mark, inclusive). Assuming the defaults, the = command applies the indent rules for the C language, or for the language of the current buffer, if your syntax rules have defined an indent expression. In insert mode, Ctrl-o (<c-o>) executes one normal-mode command, then returns to insert mode.