Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim yanking range of lines

I'm a C# developer who has just recently decided to expand my knowledge of the tools available to me. The first tool I've decided to learn is Vi/Vim. Everything has been going well so far, but there are a couple of questions I can't seem to find the answer to:

  1. Lets say I wanted to yank a range of lines. I know there are many ways of doing so, but I would like to do it by line number. I figured it would be similar to how the substitute commands work, something like 81,91y. Is there a way to do this?

  2. I'm a little confused about the g command in normal mode. It seems to do a myriad of things and I can't really determine what the g command does at its core. I'm confused on whether or not it's a motion command or a kind of "catch all" for other commands ran through normal mode. Can someone please explain this or point me to a reference that gives a good explanation of the g command?

like image 963
jnadro52 Avatar asked Jan 07 '10 19:01

jnadro52


People also ask

How do I yank multiple lines in Vim?

To place the yanked line in a new line above the cursor, type P . The yy command works well with a count: to yank 11 lines, for example, just type 11yy . Eleven lines, counting down from the cursor, will be yanked, and vi indicates this with a message at the bottom of the screen: 11 lines yanked .

How can you yank 3 lines in Vim?

Below are some helpful yanking commands: yy - Yank (copy) the current line, including the newline character. 3yy - Yank (copy) three lines, starting from the line where the cursor is positioned. y$ - Yank (copy) everything from the cursor to the end of the line.

How do you copy a range of a line in vi?

Press the ESC key to be sure you are in vi Command mode. Place the cursor on the first line of the text you wish to copy. Type 12yy to copy the 12 lines. Move the cursor to the place where you wish to insert the copied lines.

How do I select a range of lines in Vim?

Press V to switch to VISUAL LINE mode and then go to line 1701 by typing: 1701G . Now your lines are selected, you can run a command on them. For example, to replace foo with bar type: :s/foo/bar/ .


1 Answers

Yank lines 81-91

:81,91y<enter> 

If your fingers don't like to find the : and , keys, this would work as well (go to line 81, yank 11 lines)

81gg11yy  

My only use of g is 5gg. To go to the 5th line. 22gg: 22nd line. As jimbo said, it's really only a modifier for some other commands.

For completeness, (http://vim.wikia.com/wiki/Power_of_g) explains a lot of how g works in command mode.

like image 86
hometoast Avatar answered Oct 01 '22 10:10

hometoast