4

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.

7

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

|improve this answer|||||
  • I assume that there would also be a key to enter insert mode above current line, isn't there? – k0pernikus Nov 27 '15 at 23:09
  • 2
    That would be O. (shift-o) – EvergreenTree Nov 27 '15 at 23:09
  • @Alex Follow up question cause you blew my mind! vi.stackexchange.com/questions/5634/… – k0pernikus Nov 27 '15 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 '15 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. – G-Man Says 'Reinstate Monica' Nov 28 '15 at 7:23
1
  • :$ 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.

|improve this answer|||||

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.