Vim Tips Wiki
(use better method from Jürgen Krämer at vim_use (works if yank in one buffer and paste in another))
(→‎Comments: ask for reliable example)
(4 intermediate revisions by 3 users not shown)
Line 13: Line 13:
 
Here is an easy way to select the text you just pasted. For example, you may want to paste some text, then select it for [[Shifting blocks visually|indenting]], or formatting, or performing a [[Search and replace in a visual selection|substitution]].
 
Here is an easy way to select the text you just pasted. For example, you may want to paste some text, then select it for [[Shifting blocks visually|indenting]], or formatting, or performing a [[Search and replace in a visual selection|substitution]].
   
A simple procedure would be to press <tt>`[</tt> to jump to the start of the text you last changed. For example, you may use <tt>ciw</tt> to change inner word, or <tt>p</tt> to paste, then scroll elsewhere. Typing <tt>`[</tt> would jump to the start of the word you just changed, or to the start of the text you just pasted (see [[Using marks|marks]]).
+
A simple procedure would be to press <code>`[</code> to jump to the start of the text you last changed. For example, you may use <code>ciw</code> to change inner word, or <code>p</code> to paste, then scroll elsewhere. Typing <code>`[</code> would jump to the start of the word you just changed, or to the start of the text you just pasted (see [[Using marks|marks]]).
   
 
To select the last changed text (or the text that was just pasted), use a mapping like this in your [[vimrc]]:
 
To select the last changed text (or the text that was just pasted), use a mapping like this in your [[vimrc]]:
Line 20: Line 20:
 
</pre>
 
</pre>
   
After pasting, type <tt>gp</tt> to select the pasted text in visual mode. This is similar to the standard <tt>gv</tt> which you can type to select the last visually-selected text.
+
After pasting, type <code>gp</code> to select the pasted text in visual mode. This is similar to the standard <code>gv</code> which you can type to select the last visually-selected text.
   
 
Following is a more elaborate alternative:
 
Following is a more elaborate alternative:
Line 27: Line 27:
 
</pre>
 
</pre>
   
With this alternative, typing <tt>gp</tt> will select the last changed (or pasted) text, and the visual mode will be the same as was last used. For example, you may press <tt>v</tt> to start character-wise visual selection, then move the cursor and press <tt>y</tt> to yank (copy) the selected text. Elsewhere, you may press <tt>p</tt> to paste the text. In that case typing <tt>gp</tt> would select the pasted text character-wise.
+
With this alternative, typing <code>gp</code> will select the last changed (or pasted) text, and the visual mode will be the same as was last used. For example, you may press <code>v</code> to start character-wise visual selection, then move the cursor and press <code>y</code> to yank (copy) the selected text. Elsewhere, you may press <code>p</code> to paste the text. In that case typing <code>gp</code> would select the pasted text character-wise.
   
Repeating this example, but using <tt>V</tt> would use line-wise visual selection (whole lines would be selected). Similarly, using Ctrl-V (or Ctrl-Q if you use Ctrl-V for pasting) would use block-wise visual selection.
+
Repeating this example, but using <code>V</code> would use line-wise visual selection (whole lines would be selected). Similarly, using Ctrl-V (or Ctrl-Q if you use Ctrl-V for pasting) would use block-wise visual selection.
   
The mapping uses "<tt><expr></tt>" which means that when you type <tt>gp</tt>, the mapping is evaluated as an expression, then the result is used. The dot operator (<tt>.</tt>) is used to concatenate three strings. The first string (without quotes) is <tt>`[</tt> and the third string is <tt>`]</tt>. The second string is the first character ({{help|strpart()}}) of the result from calling <tt>getregtype()</tt> which is the "type" of the last register used in a normal mode command. The type is 'v', 'V', or Ctrl-V depending on whether the text in the register resulted, respectively, from a character, a line, or a block operation. ({{help|getregtype()}})
+
The mapping uses "<code><expr></code>" which means that when you type <code>gp</code>, the mapping is evaluated as an expression, then the result is used. The dot operator (<code>.</code>) is used to concatenate three strings. The first string (without quotes) is <code>`[</code> and the third string is <code>`]</code>. The second string is the first character ({{help|strpart()}}) of the result from calling <code>getregtype()</code> which is the "type" of the last register used in a normal mode command. The type is 'v', 'V', or Ctrl-V depending on whether the text in the register resulted, respectively, from a character, a line, or a block operation. ({{help|getregtype()}})
   
For example, if you type <tt>yiw</tt> to copy a word, the function <tt>getregtype()</tt> will return <tt>v</tt>, and the result of the mapping expression will be <tt>`[v`]</tt> (the same as the first mapping given above). However, if you type <tt>3Y</tt> to copy three lines, the result will be <tt>`[V`]</tt> (linewise).
+
For example, if you type <code>yiw</code> to copy a word, the function <code>getregtype()</code> will return <code>v</code>, and the result of the mapping expression will be <code>`[v`]</code> (the same as the first mapping given above). However, if you type <code>3Y</code> to copy three lines, the result will be <code>`[V`]</code> (linewise).
   
 
==References==
 
==References==
Line 39: Line 39:
   
 
==Comments==
 
==Comments==
  +
<code>`[v`]</code> and <code>`[V`]</code> don't seem to work at least in Vim 7.4.443 and later.
  +
  +
Instead of selecting the last pasted text, it works similarly to <code>gv</code> or it selects the entire buffer if no previous selection has been performed.
  +
  +
To implement the same functionality in those versions, <code>`<v`></code> and <code>`<V`></code> instead seem to work.
  +
:That doesn't seem right, and I certainly do NOT see that in 7.4.430. If this is true then it is a bug in Vim. `< and `> are SUPPOSED to be the positions of the last visual selection, so what you post as the solution should be exactly equivalent to gv, not what is in the current tip. The `[ and `] marks, on the other hand, are SUPPOSED to be the positions surrounding the last change, including pastes. See the help for each of these marks. --[[User:Fritzophrenic|Fritzophrenic]] ([[User talk:Fritzophrenic|talk]]) 15:23, September 22, 2014 (UTC)
  +
::As a potential bug, I've posted to the vim_use mailing list at https://groups.google.com/d/topic/vim_use/qTc3SPuuhoQ/discussion. I suggest you follow up there if you're still seeing this behavior without any customizations, i.e. after running Vim with <code>vim -N -u NONE -i NONE</code>. --[[User:Fritzophrenic|Fritzophrenic]] ([[User talk:Fritzophrenic|talk]]) 17:23, September 22, 2014 (UTC)
  +
:Could you post a way to reproduce your issue reliably (preferably on the vim_dev mailinglist?) I can't seem make a simple example behave the way you describe here. [[User:Chrisbra|Chrisbra]] ([[User talk:Chrisbra|talk]]) 18:25, September 22, 2014 (UTC)

Revision as of 18:25, 22 September 2014

Tip 759 Printable Monobook Previous Next

created 2004 · complexity basic · version 6.0


Here is an easy way to select the text you just pasted. For example, you may want to paste some text, then select it for indenting, or formatting, or performing a substitution.

A simple procedure would be to press `[ to jump to the start of the text you last changed. For example, you may use ciw to change inner word, or p to paste, then scroll elsewhere. Typing `[ would jump to the start of the word you just changed, or to the start of the text you just pasted (see marks).

To select the last changed text (or the text that was just pasted), use a mapping like this in your vimrc:

nnoremap gp `[v`]

After pasting, type gp to select the pasted text in visual mode. This is similar to the standard gv which you can type to select the last visually-selected text.

Following is a more elaborate alternative:

nnoremap <expr> gp '`[' . strpart(getregtype(), 0, 1) . '`]'

With this alternative, typing gp will select the last changed (or pasted) text, and the visual mode will be the same as was last used. For example, you may press v to start character-wise visual selection, then move the cursor and press y to yank (copy) the selected text. Elsewhere, you may press p to paste the text. In that case typing gp would select the pasted text character-wise.

Repeating this example, but using V would use line-wise visual selection (whole lines would be selected). Similarly, using Ctrl-V (or Ctrl-Q if you use Ctrl-V for pasting) would use block-wise visual selection.

The mapping uses "<expr>" which means that when you type gp, the mapping is evaluated as an expression, then the result is used. The dot operator (.) is used to concatenate three strings. The first string (without quotes) is `[ and the third string is `]. The second string is the first character (:help strpart()) of the result from calling getregtype() which is the "type" of the last register used in a normal mode command. The type is 'v', 'V', or Ctrl-V depending on whether the text in the register resulted, respectively, from a character, a line, or a block operation. (:help getregtype())

For example, if you type yiw to copy a word, the function getregtype() will return v, and the result of the mapping expression will be `[v`] (the same as the first mapping given above). However, if you type 3Y to copy three lines, the result will be `[V`] (linewise).

References

Comments

`[v`] and `[V`] don't seem to work at least in Vim 7.4.443 and later.

Instead of selecting the last pasted text, it works similarly to gv or it selects the entire buffer if no previous selection has been performed.

To implement the same functionality in those versions, `<v`> and `<V`> instead seem to work.

That doesn't seem right, and I certainly do NOT see that in 7.4.430. If this is true then it is a bug in Vim. `< and `> are SUPPOSED to be the positions of the last visual selection, so what you post as the solution should be exactly equivalent to gv, not what is in the current tip. The `[ and `] marks, on the other hand, are SUPPOSED to be the positions surrounding the last change, including pastes. See the help for each of these marks. --Fritzophrenic (talk) 15:23, September 22, 2014 (UTC)
As a potential bug, I've posted to the vim_use mailing list at https://groups.google.com/d/topic/vim_use/qTc3SPuuhoQ/discussion. I suggest you follow up there if you're still seeing this behavior without any customizations, i.e. after running Vim with vim -N -u NONE -i NONE. --Fritzophrenic (talk) 17:23, September 22, 2014 (UTC)
Could you post a way to reproduce your issue reliably (preferably on the vim_dev mailinglist?) I can't seem make a simple example behave the way you describe here. Chrisbra (talk) 18:25, September 22, 2014 (UTC)