Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you yank X number of characters in vim?

Tags:

vim

I know I can cut 5 characters in vim by typing 5x, and I often use this to copy and paste text by doing 5xu then pasting them somewhere else.

I am lazy and would rather just yank the 5 characters so I don't have to undo the cut action.

How can I do this?

like image 938
Cory Klein Avatar asked Sep 22 '11 19:09

Cory Klein


People also ask

How do I yank multiple lines in Vim?

To place the yanked line in a new line above the cursor, type P . The yy command works well with a count: to yank 11 lines, for example, just type 11yy . Eleven lines, counting down from the cursor, will be yanked, and vi indicates this with a message at the bottom of the screen: 11 lines yanked .

How do I yank an entire file in Vim?

The simplest and fastest way is to use: : % y + and then go over to Google Docs (or wherever) and paste. Explanation: % to refer the next command to work on all the lines. y to yank those lines.

How do I select and copy in Vim?

Press v to begin character-based visual selection, or V to select whole lines, or Ctrl-v or Ctrl-q to select a block. Move the cursor to the end of the text to be cut/copied. While selecting text, you can perform searches and other advanced movement. Press d (delete) to cut, or y (yank) to copy.


2 Answers

Yank supports standard movement commands. Use y5l (<- that's a lowercase L)

And if you want to yank 5 characters backwards, use y5h.

Now, if you're feeling really lazy, remap this particular sequence to a key-combination, like so:

:nnoremap <C-l> y5l 

In this case, "yank 5 to the right" gets mapped to Ctrl+L (lowercase L).


If you'd like, you can also use Space instead of L to yank characters in the forward direction (examples: ySpace for a single character or 5ySpace for 5). Some people may find it to be quicker or easier than hitting L.

like image 149
voithos Avatar answered Oct 10 '22 15:10

voithos


This is complementing the answer of voithos:

You can also use 5ySpace or y5Space (ty @hauleth for suggestion)

This is useful for me especially when I want to yank just one character:

ySpace

Is easier (for me at least) to find and hit Space than l.

like image 28
bolov Avatar answered Oct 10 '22 14:10

bolov