103

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

Christopher Bottoms
  • 3,772
  • 6
  • 22
  • 33
  • 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
  • Rather than yutting (that ought to be a standard abbreviation for yank-put, but by saying so I've made this a whole lot longer) the character you can type (in insert mode) <C-K>>>. This uses digraphs to insert the » character. It's probably faster than exiting insert mode, navigating to the character, yanking it, navigating back, and putting. – jjaderberg Dec 14 '15 at 00:02
  • On my keyboard layout I can use the AltGr``> combo to insert that character. I was asking more to check the remote possibility that the un-yank-able » was a conceal character for a >> sequence. – VanLaser Dec 14 '15 at 00:17
  • 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 Jan 18 '17 at 01:25
  • @Zhora Yes, it should be till. I can't edit an old comment, unfortunately. – Christopher Bottoms Jan 18 '17 at 18:05

4 Answers4

101

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?

VanLaser
  • 9,700
  • 2
  • 24
  • 34
  • 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 Dec 13 '15 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 Dec 14 '15 at 00:07
  • That's nice, and seemingly even easier to hit, except perhaps when the space is used as leader. – VanLaser Dec 14 '15 at 00:19
23

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.

Christopher Bottoms
  • 3,772
  • 6
  • 22
  • 33
Kent
  • 1,310
  • 7
  • 12
  • 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 Apr 17 '17 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.

Karl Yngve Lervåg
  • 9,494
  • 25
  • 35
BruceK
  • 101
  • 2
1

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

  • 5
    Welcome to [vi.se]! 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 Mar 28 '20 at 19:34
  • Thanks, vy or yv both OK for me. – Patrick J. Holt Mar 29 '20 at 00: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 Mar 30 '20 at 08: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. – Patrick J. Holt Mar 31 '20 at 03:24