Im trying to yank part of another line:

1  foo=bar 
3  (paste only foo here - without "going/moving" to 1)

I understand that I can yank the whole line using :1y.

A "pseudocode" for that would be :1yt=


Your question has two parts:

  • Executing a command on a line different from the current one
  • Executing a command without moving the cursor

First to yank on the first line you can use this ex command: :1norm! yt=

This can be decomposed in three parts:

  • 1 the line on which you want to execute the command
  • norm! which means "execute the following text as some normal mode key presses
  • yt= yank to = as you already figured out

Now that you know how to yank on another line, you'll realize that the command moves your cursor. You have several solutions to this problem, one of them is to use marks.

You could simply use ma before using the previous ex command and after that use `a to go back to where you were before the command. (IMO it's the simplest solution)

Or you could use several ex commands like this:

execute 'norm! ma '| execute '1norm! yt=' | execute 'norm! `a'

Here you'll have to use execute which means "execute the following string as an ex command". this is necessary otherwise, vim will consider the | characters as a part of your norm! commands.

For more details you can read the following help topics:

| improve this answer | |
  • thanks a lot for the answer. If I need to do this in two steps, would not be better to yank the line and then edit it like :1y , paste it and then f=d$ ? – AndreiR Aug 16 '17 at 14:36
  • @AndreiR well it's up to you, depending on what you prefer. Still my last command only requires one step. – statox Aug 16 '17 at 15:20

Statox's answer is a great solution. I'd like to provide an alternative:

:norm m`1Gyt=<C-o>

Note that <C-o> is ctrl-o, and you can enter it on the command line with ctrl-v-ctrl-o. This uses a single normal command to add the current location to the jumplist, yank what you need, and then jump to the previous location in the jumplist.


| improve this answer | |
  • 1
    That is much faster to use than my solution, really nice answer :-) – statox Aug 17 '17 at 7:40

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.