105

I know many ways to copy things:

yiw = yank in current word
yaw = yank all word (includes a trailing space)
yy = yank the current line
3yy = yank three lines starting at the current one
yap = yank all paragraph (includes trailing newline)

I can also delete or change a single character:

x = delete the character under the cursor
r = replace the character under the cursor

But how in the world do you simply copy a single character under the cursor?

My motivation is that I'm programming in Perl 6 and some of the operators are Unicode characters. Right now I'm using tadzik's Perl 6 Config::INI code as a starting point for a custom parser, for example, and I would have liked to have copied just that one French quote character (a hyper operator) from this line:

my %hash = $<sections>».ast;

I could use the two character "Texas" version of the hyper operator >>, but I thought that looked better and less ambiguous than >>>:

my %hash = $<sections>>>.ast;

enter image description here
enter image description here

9
  • 1
    Easily, compose the operation with the motion, as usual: yl
    – VanLaser
    Commented Dec 12, 2015 at 22:40
  • @VanLaser Good idea! I was thinking that movement wouldn't work because I only wanted that one character. But your suggestion works great for ASCII. Post that as an answer and I'll vote it up. Interestingly it doesn't work with this Unicode character, but ytX ("yank til X") seems to work when X is the character just to the right. Commented Dec 12, 2015 at 22:51
  • I wonder, is that one character, be it Unicode, or aren't there two characters, hidden/replaced using Vim conceal feature?
    – VanLaser
    Commented Dec 12, 2015 at 22:57
  • @VanLaser Seems like two "characters". Commented Dec 12, 2015 at 23:50
  • 1
    @VanLaser Thank you for spelling that out! Using vim for years and always knew the normal f "f ind" command from the start; took me a good while to stumble on t ("t ill") but until your comment i never thought about it meaning "t ill" (or I guess it could also mean "t o"). (We differ on our spelling of till but that's here nor there.)
    – Zhora
    Commented Jan 18, 2017 at 1:25

4 Answers 4

103

Composing the yank operation with the so often used "one character to the right" motion should work: yl.

BTW (to comment on a OP comment), for me » can be yanked in this manner, in gVim or terminal Vim. Perhaps a (file)encoding issue?

3
  • 8
    Yanking » works fine for me too. I use vy to yank a single character. Not sure why I use that rather than yl, maybe I had a conscious reason once but now it's just reflex.
    – jjaderberg
    Commented Dec 13, 2015 at 19:07
  • 7
    I've always done y<space> thinking it was yank the character under the cursor, and I was going to add it as an answer but after checking the help I realize <space> and l are the same thing.
    – Brett Y
    Commented Dec 14, 2015 at 0:07
  • That's nice, and seemingly even easier to hit, except perhaps when the space is used as leader.
    – VanLaser
    Commented Dec 14, 2015 at 0:19
24

Usually I press vy (visual yank), sometimes xu (delete undo) too. Also , if you want to yank the char before your cursor, instead of lvy, you can press Xu, I think that's the reason why I press xu for the current. :-)

Note that, Xu will make your cursor move to that char.

1
  • 1
    You mean hvy. I tried to edit it, but apparently correcting one character in a vim command is not a significant change to be acceptable.
    – Shahbaz
    Commented Apr 17, 2017 at 21:49
10

The simplest way I know to do this is to use x to delete the character, which also puts it in the buffer, and then P to put it back. You can then move the cursor around and paste the character back wherever you want it with p (after cursor) or P (before cursor), as long as you do not change the buffer.

1

You can use yl or yv to copy a character which is on the cursor.

4
  • 5
    Welcome to Vi and Vim! I think the second one should be vy, but I'm not sure this answer adds much value over the existing ones...
    – D. Ben Knoble
    Commented Mar 28, 2020 at 19:34
  • Thanks, vy or yv both OK for me. Commented Mar 29, 2020 at 0:37
  • 1
    @meteorsh Welcome to our site. We usually value answers which make an effort to explain things clearly for the readers. Here i would be useful to explain how your solution is different from the already existing answers and why it specifically answers OP question. Usually answers which are not more than a sentence long are considered as low quality.
    – statox
    Commented Mar 30, 2020 at 8:08
  • 2
    Sorry, I am a new starter to use this website and may not know the rules. So I will try to improve my answers next time. Commented Mar 31, 2020 at 3:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.