How do you yank X number of characters in vim?
VimVim Problem Overview
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?
Vim Solutions
Solution 1 - Vim
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.
Solution 2 - Vim
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.
Solution 3 - Vim
Found this post because I too wanted to yank x
number characters without cutting and learned this while testing.
There may be times when is easier to look for certain characters than to count the number of characters to yank.
Enter the motions t
and T
.
Think ytx
as yank forward to character x
Think yTx
as yank backward to character x
Solution 4 - Vim
y5
will do it. yy
yanks lines, y
yanks characters.