How To Go To Last Line In Vi Editor In Linux : Shift G Shortcut Method

Pressing Shift+G in vi’s command mode jumps your cursor directly to the final line of the file. If you are new to vi or vim, learning how to go to last line in vi editor in linux is one of the first skills you should master. This simple command saves you from scrolling through hundreds of lines manually.

Vi is a powerful text editor that comes pre-installed on almost every Linux distribution. It has two main modes: command mode and insert mode. To move to the last line, you need to be in command mode first. This guide will show you every method, including shortcuts and advanced tricks.

How To Go To Last Line In Vi Editor In Linux

The most direct way is to press Shift+G (capital G) while in command mode. This command is case-sensitive, so make sure you hold the Shift key. If you press lowercase g, vi will move to the first line of the file instead.

Here is the step-by-step process:

  1. Open a file in vi: vi filename.txt
  2. Press the Escape key to ensure you are in command mode
  3. Press Shift+G (capital G)
  4. Your cursor jumps to the last line of the file

That is all there is to it. The cursor will land on the very last character of the last line. If the file ends with a blank line, vi will place the cursor on that blank line.

Using The Colon Command To Go To The Last Line

Another reliable method uses the colon command. This is especially useful if you want to combine it with other actions. Type a colon (:) while in command mode, then type a dollar sign ($) and press Enter.

Steps:

  1. Press Escape to enter command mode
  2. Type : (colon)
  3. Type $ (dollar sign)
  4. Press Enter

This command tells vi to go to the last line of the file. The colon command line is located at the bottom of the terminal window. You will see the cursor move immediately.

Going To A Specific Line Number Near The End

Sometimes you do not need the exact last line, but a line close to the end. You can combine the colon command with a line number. For example, if your file has 200 lines and you want to go to line 195, type :195 and press Enter.

To find the total number of lines, use the :set number command. This shows line numbers on the left side. Then you can navigate precisely.

Steps:

  1. Press Escape
  2. Type :set number and press Enter
  3. Note the last line number
  4. Type : followed by the line number, then Enter

Using The Goto Command With Line Numbers

Vi also supports the : command with a line number directly. This is the same as the colon method but without the dollar sign. For the last line, you can use :$ as shown earlier.

If you know the exact line number, just type :123 to go to line 123. This is faster than scrolling when you know the number.

Navigating To The Last Line With The Gg And G Combo

Some users prefer to use the gg command to go to the first line, then G to go to the last line. This is a two-step process but works well if you are already at the top.

Steps:

  1. Press Escape
  2. Type gg to go to the first line
  3. Press Shift+G to go to the last line

This method is not necessary but can help if you are disoriented in the file. It resets your position before moving to the end.

Using The Ctrl+End Shortcut In Vim

If you are using vim (vi improved), there is an additional shortcut. Press Ctrl+End to jump to the last line. This works in graphical vim (gvim) and some terminal emulators.

Note: This shortcut may not work in all terminals. The Shift+G method is more universal and works everywhere.

Steps for Ctrl+End:

  1. Press Escape to enter command mode
  2. Hold the Ctrl key and press End
  3. Release both keys

Going To The Last Line And Entering Insert Mode

Often you want to go to the last line and start typing. You can combine the goto command with insert mode. Use G to go to the last line, then press o to open a new line below and enter insert mode.

Steps:

  1. Press Shift+G to go to the last line
  2. Press o (lowercase letter o)
  3. Vi creates a new blank line and enters insert mode
  4. Start typing your text

Alternatively, press O (uppercase O) to open a new line above the current line. This is useful for inserting text before the last line.

Using The :$ Command With Modifications

The colon command :$ can be combined with other commands. For example, to go to the last line and delete it, type :$d and press Enter. This deletes the last line entirely.

Other combinations:

  • :$s/old/new/g – Replace text on the last line
  • :$y – Yank (copy) the last line
  • :$p – Paste after the last line

These commands are powerful for editing large files quickly. Practice them to become more efficient.

Common Mistakes When Going To The Last Line

New users often make these errors:

  • Pressing lowercase g instead of Shift+G
  • Being in insert mode when pressing G
  • Typing : and then G instead of :$

If the command does not work, press Escape first to ensure you are in command mode. Then try again. The cursor should move instantly.

Going To The Last Line In Large Files

For very large files (thousands of lines), the Shift+G command is still instant. Vi does not scroll through the file; it jumps directly. This is much faster than using the Page Down key.

If your file is slow to load, consider using the tail command in the terminal to view the last lines. But for editing, vi is the best tool.

Using Marks To Return To The Last Line

Vi allows you to set marks for quick navigation. You can set a mark at the last line and return to it later. To set a mark, go to the last line and type ma (where ‘a’ is the mark name). To return, type 'a (apostrophe followed by the mark name).

Steps:

  1. Go to the last line with Shift+G
  2. Type ma to set mark ‘a’
  3. Move elsewhere in the file
  4. Type 'a to return to the last line

Marks are saved for the current session only. They are useful for jumping between specific lines.

Going To The Last Line With The :0 Command

You can also go to the first line with :0 and then to the last line with :$. This is a two-step process but works well if you need to start from the beginning.

Steps:

  1. Type :0 and press Enter to go to line 1
  2. Type :$ and press Enter to go to the last line

Using The Vim Status Line To See The Last Line

Vim shows the current line number and total lines in the status bar. Look at the bottom right corner. It displays something like “123,200” meaning line 123 of 200 lines. This helps you know where you are.

To enable the status line, use :set ruler. This is often enabled by default in vim.

Going To The Last Line In Multiple Files

If you have multiple files open in vi, you can navigate between them. To go to the last line of the current file, use Shift+G. To switch to the next file, type :n and press Enter. Then use Shift+G again.

Steps:

  1. Open multiple files: vi file1.txt file2.txt
  2. Press Shift+G to go to the last line of file1
  3. Type :n to go to file2
  4. Press Shift+G to go to the last line of file2

Using The :$ Command In Ex Mode

Vi also has an Ex mode for batch commands. You can enter Ex mode by typing Q (uppercase Q). Then type $ to go to the last line. Type vi to return to normal mode.

This is an advanced method and not needed for most users. Stick with Shift+G for simplicity.

Going To The Last Line And Counting Lines

To count the total number of lines, use :set number or Ctrl+G. The Ctrl+G command shows the filename, line count, and percentage. For example, “file.txt line 200 of 200 –100%–“.

Steps:

  1. Press Ctrl+G while in command mode
  2. Read the status line for total lines
  3. Press Shift+G to go to the last line

Using The :$ Command With Search

You can combine the goto command with search. For example, to go to the last occurrence of the word “error”, type ?error and press Enter. This searches backward from the cursor. If you are at the end of the file, it finds the last occurrence.

Steps:

  1. Press Shift+G to go to the last line
  2. Type ?error and press Enter
  3. Vi moves to the last occurrence of “error”

Going To The Last Line In Insert Mode

You cannot go to the last line directly from insert mode. You must press Escape first to enter command mode. Then use Shift+G. After that, you can press i to return to insert mode.

This is a common mistake. Always remember to exit insert mode before navigating.

Using The :$ Command With Line Numbers In Vim

In vim, you can also use the : command with a line number relative to the end. For example, :$ is the last line, :$-1 is the second-to-last line, and so on.

Examples:

  • :$-1 – Go to the line before the last
  • :$-5 – Go to the fifth line from the end
  • :$+1 – Go to a new line after the last (creates a blank line)

This is useful for editing the end of a file without knowing the exact line number.

Going To The Last Line And Deleting It

To delete the last line, use G to go there, then press dd to delete the line. Alternatively, use :$d from command mode.

Steps:

  1. Press Shift+G
  2. Type dd
  3. The last line is deleted

You can also delete multiple lines from the end. For example, :$-5,$d deletes the last 6 lines.

Going To The Last Line And Copying It

To copy the last line, use G to go there, then press yy to yank the line. Then move to another location and press p to paste.

Steps:

  1. Press Shift+G
  2. Type yy
  3. Move to where you want to paste
  4. Press p

Going To The Last Line In Vi Without A Mouse

Vi is designed for keyboard-only navigation. The Shift+G command is the fastest way to reach the end. You do not need a mouse at all. This makes vi efficient for remote servers and terminal work.

Practice these commands until they become second nature. You will save a lot of time editing files.

Going To The Last Line In Vi On Different Terminals

The Shift+G command works on all terminals that support vi. Whether you use xterm, gnome-terminal, or SSH, the command is the same. Some terminals may have key mappings that interfere, but this is rare.

If Shift+G does not work, check your terminal settings. You can also try the colon method as a backup.

Going To The Last Line And Saving The File

After editing the last line, you can save and exit. Use :wq to save and quit, or :x to save and exit. If you want to quit without saving, use :q!.

Steps:

  1. Make your edits on the last line
  2. Press Escape
  3. Type :wq and press Enter

Going To The Last Line In Vi With A Large File

For files with millions of lines, Shift+G is still instant. Vi does not load the entire file into memory for navigation. It uses efficient data structures to jump to the end.

If you experience lag, it may be due to syntax highlighting or plugins. Disable them with :syntax off for better performance.

Going To The Last Line And Using Visual Mode

You can select text from the current line to the last line using visual mode. Go to the starting line, press V (uppercase V) to enter visual line mode, then press G to extend the selection to the last line.

Steps:

  1. Go to the starting line
  2. Press V
  3. Press Shift+G
  4. All lines from start to end are selected

Then you can delete, copy, or format the selection.

Going To The Last Line And Changing Text

To change text on the last line, use G to go there, then press cw to change a word, or cc to change the entire line. This enters insert mode automatically.

Steps:

  1. Press Shift+G
  2. Type cc to change the entire line
  3. Type your new text
  4. Press Escape

Going To The Last Line And Appending Text

To append text at the end of the last line, use G to go there, then press $ to go to the end of the line, then press a to append. This enters insert mode at the end.

Steps:

  1. Press Shift+G
  2. Press $ (dollar sign)
  3. Press a
  4. Type your text

Going To The Last Line And Inserting A New Line

To insert a new line after the last line, use G to go there, then press o (lowercase o). This creates a blank line