How do I move to end of line in Vim?

Learn how do i move to end of line in vim? with practical examples, diagrams, and best practices. Covers vim, editor, keyboard-shortcuts development techniques with visual explanations.

Mastering Vim: Navigating to the End of a Line

Vim editor interface with cursor at the end of a line, illustrating efficiency

Learn essential Vim commands to quickly move your cursor to the end of the current line, boosting your editing efficiency.

Vim is a powerful text editor renowned for its efficiency and keyboard-driven interface. One of the most fundamental navigation tasks is moving the cursor to the end of a line. While seemingly simple, Vim offers several ways to achieve this, each with its own nuances that can be leveraged for different scenarios. Understanding these commands is crucial for fluid and fast editing.

The Basic Command: $

The most common and straightforward way to move to the end of the current line in Vim's normal mode is by pressing the $ key. This command instantly places your cursor on the last character of the line. It's a fundamental movement that you'll use constantly.

This is a sample line of text.
                   ^ Cursor here

Press `$`

This is a sample line of text.
                               ^ Cursor here

Demonstration of the $ command

Moving to the End and Entering Insert Mode: A

Often, when you move to the end of a line, your intention is to append text. Vim provides a dedicated command for this: A (capital 'a'). This command moves the cursor to the end of the current line and immediately switches Vim into insert mode, ready for you to type. This saves you an extra keystroke compared to $ followed by i or a.

This is a sample line.
                   ^ Cursor here

Press `A`

This is a sample line.|
                       ^ Cursor here, now in INSERT mode

Using A to append text at the end of a line

Combining with Counts for Multiple Lines

Vim's power often comes from combining commands with counts. While $ typically applies to the current line, you can use counts with other movement commands to affect multiple lines. For instance, to move to the end of the current line and the next two lines (a total of three lines), you might use a combination of commands or visual mode.

flowchart TD
    A[Start at current line] --> B{Press 'G' to go to last line?}
    B -- No --> C{Press '$' to go to end of current line}
    B -- Yes --> D{Press '$' to go to end of last line}
    C --> E[End of current line reached]
    D --> F[End of last line reached]

Decision flow for navigating to end of line in Vim

Advanced Navigation: g_ and G

While $ moves to the last character of the current line, g_ moves to the last non-blank character of the current line. This can be useful if you have trailing whitespace you want to ignore. For moving to the end of the entire file, you would first use G to go to the last line, and then $ to go to the end of that line.

This line has trailing spaces.   
                               ^ Cursor with `$`

This line has trailing spaces.   
                             ^ Cursor with `g_`

Press `G` then `$` to go to the end of the last line in the file.

Distinction between $ and g_, and using G$

1. Move to end of current line

Ensure you are in normal mode (press <Esc>). Then, simply press the $ key. Your cursor will jump to the last character of the current line.

2. Move to end and append text

From normal mode, press A (capital 'A'). This will move the cursor to the end of the line and automatically switch you into insert mode, allowing you to start typing immediately.

3. Move to end of last non-blank character

In normal mode, press g_. This will place your cursor on the last non-blank character of the current line, ignoring any trailing whitespace.

4. Move to end of the entire file

First, press G (capital 'G') to jump to the last line of the file. Then, press $ to move to the end of that specific line.