vi: how can I jump to end of file with a new line appended?

22

3

Here's an answer I want about 5 times a day.

In vi, the command to go to the end of the file is G.

But that always leaves me at the start of the last line of the file, if files don't have a blank line at the end.

How can I jump to the end of the file and append a new blank line, so that I can just start typing?

Thanks!

Richard

Posted 2011-06-28T11:23:27.017

Reputation: 605

Answers

45

What about Go, successively? ;-)

binfalse

Posted 2011-06-28T11:23:27.017

Reputation: 1 426

@binfalse : Many times while coding we want to start a new code block, so after the end of file, we need to skip a line and then enter insert mode, if I use G2o, then I am moved to the next line to the EOF and into insert mode and when I move back to normal mode another line is created, to which I'm instantly shifted, how to go to insert mode after 2/n lines ? – aspiring1 – 2020-01-05T14:32:07.130

@Richard Also check out the capital 'O' command if you aren't familiar with that one. It creates a new line above the current one and puts you in insert mode. – Corey – 2011-06-28T16:38:54.297

3

An alternative with command line:

:$put _

If you want to understand that, you can also read this answer on registers

Benoit

Posted 2011-06-28T11:23:27.017

Reputation: 6 563

1

One way to do this is to create a macro and place it in your .vimrc file.

map , GA<enter>

This will map the comma key. When pressed, vi will go to the end of the file, go to the end of that line and create a new blank line. This will also leave you in insert / edit mode.

jftuga

Posted 2011-06-28T11:23:27.017

Reputation: 2 877

1A<enter> ? o as suggested by @binfalse, is the right way! – Benoit – 2011-06-28T14:06:23.630