48

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

  • 1
    Easily, compose the operation with the motion, as usual: yl – VanLaser Dec 12 '15 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. – Christopher Bottoms Dec 12 '15 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 Dec 12 '15 at 22:57
  • @VanLaser Seems like two "characters". – Christopher Bottoms Dec 12 '15 at 23:50
  • BTW, how do you introduce the » character? – VanLaser Dec 13 '15 at 23:38
40

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?

  • 4
    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 Dec 13 '15 at 19:07
  • 3
    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 Dec 14 '15 at 0:07
  • That's nice, and seemingly even easier to hit, except perhaps when the space is used as leader. – VanLaser Dec 14 '15 at 0:19
  • 1
    @jjaderberg I like your comment, please post it as an answer. Thanks! – Christopher Bottoms Dec 14 '15 at 14:46
  • 1
    @brettanomyces I like your comment, please post it as an answer. Thanks! – Christopher Bottoms Dec 14 '15 at 14:48
14

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.

  • 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 Apr 17 '17 at 21:49
9

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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