13

I often have to add stuff at the end of a file, hence I go to the last line:

:$

yet this is at the beginning of the line. Often I discover myself doing:

Ctrl + Arrow Right to jump by word until I am at the end of the line.

Then I enter insert mode us via a, and hit Enter and do whatever I need there.

This feels overcomplicated. How to add content at the end of the file?

Bonus point if I can do it by just one command.

2 Answers 2

23

If file is already opened in normal mode

Use:

Go
  • G goes to the end of the file
  • o enters insert mode on a line below the current one

If you are opening the file from the command line

You can open vim on the last line of a file from the command line using:

vim + filename

The + lets to specify the line number. Leaving it blank defaults to the last line of the file as explained in the man page:

       +[num]      For the first file the cursor will be  positioned  on  line
                   "num".   If "num" is missing, the cursor will be positioned
                   on the last line.

Then you can use o as above to enter insert mode on the line below.

What about inserting at the top of the file (or an arbitrary line number)?

In normal mode gg can be used to jump to any line in the file by number. So 5gg jumps to line 5. gg without a number is a shortcut for the first line of the file.

If you wanted to insert a line above the first line, you could do that with:

ggO

6
  • I assume that there would also be a key to enter insert mode above current line, isn't there?
    – k0pernikus
    Nov 27, 2015 at 23:09
  • 2
    That would be O. (shift-o) Nov 27, 2015 at 23:09
  • @Alex Follow up question cause you blew my mind! vi.stackexchange.com/questions/5634/…
    – k0pernikus
    Nov 27, 2015 at 23:16
  • 1
    @Alex If you want to expand your answer, please do so via edit. Comments are for meta-purpose (clarification, random side questions) and should not hide valuable information.
    – k0pernikus
    Nov 27, 2015 at 23:18
  • 1
    The first thing you say about gg is true for G as well; 5G will jump to line 5, just as 5gg does.  It's interesting that gg defaults to 1gg (= 1G) while G defaults to the last line. Nov 28, 2015 at 7:23
4
  • :$ jumps to the end of the file
  • Shift + A # enters insert mode at end of current line
  • Enter # inserts new line

This gets rids of the word by word navigation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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