How do you yank X number of characters in vim?

Solution 1:

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:

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:

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

enter image description here