Knowledgebase

Find frequently asked questions and setup guides by searching or by browsing the categories.

Jump to the Last Line

Press Esc to make sure you are in normal mode, then press capital G (Shift+g). The cursor moves to the last line. Typing :$ and pressing Enter does the same from the command line. Moving the cursor does not change the text, so unsaved edits are not affected.

KeysMoves to
GThe last line.
:$The last line.
ggThe first line.
42GLine 42.
:42Line 42.
50%The middle of the file.
Ctrl+fOne screen down.
Ctrl+bOne screen up.

Jump to the First Line and Back

gg goes to the first line, as do :1 and 1G. After a jump with G or a search, Ctrl+o returns the cursor to where it was before, and Ctrl+i goes forward again. That makes it safe to check the top of a file and come back to the line you were editing. A jump with :1 or :42 is not recorded this way, so use 1G when you want to come back.

Go to a Specific Line

Error messages often name a line, for example a configuration test that reports a problem on line 42. In normal mode, type 42G, or type :42 and press Enter. To see line numbers while you work:

:set number
:set nonumber

Ctrl+g shows the file name, the total number of lines, and how far through the file the cursor is. :set relativenumber numbers lines by distance from the cursor, which helps with counts such as 5j to move five lines down.

Move Within a Line

These keys also work in normal mode, after Esc:

KeyMoves to
$The end of the line.
0The start of the line.
^The first non-blank character.
wThe next word.
bThe previous word.

To reach the last character of the file, combine them: G and then $.

Start Typing at the End

Press these in normal mode. They move the cursor and switch to insert mode, so what you type next becomes text:

  • A: appends at the end of the current line.
  • o: opens a new line below the cursor.
  • G then o: adds a new line at the end of the file, the usual way to append a setting to a configuration file.
  • G then A: continues the last line.

Press Esc when you are done typing, then save with :wq; see how to save and quit in Vim.

Open a File at the End

vim + /etc/hosts
vim +42 /etc/nginx/nginx.conf
vim +/server_name /etc/nginx/nginx.conf

A plus sign alone opens the file at its last line, +42 opens it at line 42, and +/pattern opens it at the first match. The same options work with vi and nvim. Searching inside the file is covered in how to search in Vim.

Large Log Files

Vim reads the whole file into memory. Opening a log of several gigabytes on a small server is slow and can push the server into swap. To read the end of a log, use another tool:

tail -n 100 /var/log/nginx/error.log
tail -f /var/log/nginx/error.log
less +G /var/log/nginx/error.log

tail -n 100 prints the last 100 lines, tail -f keeps following new lines until you press Ctrl+c, and less +G opens the file at its end without reading all of it into memory; press q to leave. If you only want to look at a file in Vim, open it read-only with view or vim -R, so a stray key cannot change it. Logs that grow too large are a disk space problem; see how to check disk space in Linux.

Was this answer helpful?
Loading...