NeuroAgent

Vim End of Line Navigation: Master the $ Command

Master Vim's $ command to navigate to end of lines efficiently. Learn the difference between $ and g$, alternative methods, and practical workflow tips for better editing speed.

Question

How do I move to the end of a line in Vim?

I know how to navigate in command mode, including jumping to specific lines. What is the command to move to the end of the current line in Vim?

NeuroAgent

To move to the end of the current line in Vim, press (dollarsign)innormal/commandmode.Thiscommandmovesyourcursortotheveryendofthecurrentline,includinganytrailingwhitespace.Ifyouneedtomovetotheendofthewrappedscreenline(whentextwrapstothenextlinevisually),useg** (dollar sign) in normal/command mode. This command moves your cursor to the very end of the current line, including any trailing whitespace. If you need to move to the end of the wrapped screen line (when text wraps to the next line visually), use **g instead.


Contents


Basic End of Line Navigation

The primary command for moving to the end of a line in Vim is .ThisisoneofthemostfundamentalnavigationcommandsthateveryVimusershouldmaster.Whenyoupress**. This is one of the most fundamental navigation commands that every Vim user should master. When you press ** in normal mode, your cursor jumps to the very last character position on the current line, including any trailing whitespace or spaces.

Key characteristics of the $ command:

  • Works in normal/command mode only
  • Moves cursor to the absolute end of the logical line
  • Can be combined with numbers for repetition (e.g., 3$ moves to end of 3rd line below)
  • Works with operators (e.g., d$ deletes to end of line, c$ changes to end of line)

Quick Tip: The $ command is mnemonic - the dollar sign represents the “end” or “value” of the line, making it easy to remember.

For those who prefer a visual approach, here’s how the $ command works in practice:

This is a sample line|          <- Cursor starts here
This is a sample line            <- After pressing $, cursor moves here|

Where | represents the cursor position.


$ vs g$: Understanding the Difference

One of the most common questions for Vim users is understanding the difference between andg** and **g. While both relate to moving to the end of a line, they behave differently in certain contexts.

The $ Command

  • Moves to the logical end of the current line
  • Always goes to the last character position, regardless of screen wrapping
  • Consistent behavior across all line types and contexts

The g$ Command

  • Moves to the visual/screen end of the current line
  • Becomes relevant when lines wrap across multiple screen lines
  • Useful when you want to move to where the text appears to end on your screen

When to use each:

Scenario Recommended Command Why
Normal line without wrapping $ Moves to actual end of line
Wrapped long lines g$ Moves to end of current screen line
Programming code $ Usually want logical line end
Editing prose with wrapping g$ Often more intuitive for screen-based editing

As LinuxHint explains, $ consistently moves to the end of the current logical line, while g$ adapts to the visual presentation of your text.


Alternative Methods for Line Navigation

While $ is the standard command for moving to the end of a line, Vim offers several alternative methods that can be more efficient in different contexts.

Using the A Command

The A command combines navigation and mode switching:

  • Press A to move to the end of the line and immediately enter insert mode
  • This is perfect when you want to append text to the end of a line
  • Equivalent to $ followed by i (insert mode)

Using g_ for Last Non-Blank Character

For cases where you want to move to the last non-blank character of a line (excluding trailing whitespace), use g_:

  • g_ moves to the last non-whitespace character on the line
  • Useful when editing code where trailing spaces are irrelevant
  • As one Stack Exchange answer explains: “$ → go to the end of line, g_ → go to the last non-blank character of line”

Numbered Repetitions

Like most Vim movement commands, $ can be combined with numbers:

  • 2$ - Move to end of line 2 lines below
  • 10$ - Move to end of line 10 lines below
  • 0$ - Move to end of current line (same as just $)

Operator Combinations

The $ command works well with operators for efficient text manipulation:

  • d$ - Delete from cursor to end of line
  • c$ - Change from cursor to end of line
  • y$ - Yank (copy) from cursor to end of line
  • v$ - Start visual selection to end of line

Practical Examples and Workflow Tips

Common Use Cases

  1. Code Editing: When working with function calls or statements, press $ to quickly navigate to the end of a line for adding semicolons, commas, or closing parentheses.

  2. Document Editing: For prose writing, consider using g$ when lines wrap, as it provides more intuitive screen-based navigation.

  3. Bulk Operations: Combine $ with counts for efficient navigation across multiple lines:

    • Move to end of 5th line below: 5$
    • Delete next 3 lines to their ends: d3$

Efficiency Tips

  1. Master the $ Command: Practice using $ until it becomes muscle memory - it’s one of the most frequently used navigation commands.

  2. Learn the Difference: Understand when to use $ vs g$ based on your current editing context and line wrapping settings.

  3. Use with Operators: Leverage $ with operators for efficient text manipulation without needing to switch modes.

  4. Combine with Search: Use $ in combination with search patterns for targeted navigation:

    • /pattern$ - Search for pattern at end of line
    • /pattern$ + n - Navigate between pattern occurrences at line ends

Workflow Integration

Typical workflow for editing end of lines:
1. Navigate to desired line (gg, G, line number + G)
2. Press $ to move to end of line
3. Use d$ to delete to end, or A to append, or c$ to change
4. Continue editing as needed

Platform-Specific Variations

While Vim is consistent across platforms, there are some GUI-specific variations that users should be aware of.

MacVim Specifics

In MacVim, you can use:

  • Command + Right Arrow to go to end of line in insert mode
  • Command + Left Arrow to go to beginning of line in insert mode
    These GUI shortcuts provide an alternative to the traditional $ command when working in a graphical Vim environment.

Terminal Emulator Shortcuts

Some modern terminal emulators offer additional navigation:

  • Ctrl + End may jump to end of file in some contexts
  • Shift + End in some terminals for line navigation
    However, these are terminal-specific and not standard Vim commands.

Configuration Considerations

If you find that $ doesn’t behave as expected, it might have been remapped in your .vimrc file. As one Stack Overflow answer notes: “I know of no setting to adjust its behaviour. Perhaps it’s been remapped by your .vimrc to g$?”


Sources

  1. Stack Overflow - How do I move to end of line in Vim?
  2. Alvin Alexander - vi end of line command
  3. LinuxTect - Move End Of Line In Vim/Vi
  4. LinuxHint - Navigating within a file in VIM
  5. Stack Overflow - Difference between $ and g_ in vim
  6. Reddit - What is the difference between “"and"g" and "g”?
  7. Super User - How to jump to the beginning or end of line in vim + OS X

Conclusion

Moving to the end of a line in Vim is a fundamental skill that every user should master. The commandisyourprimarytoolfornavigatingtothelogicalendofanyline,whileg** command is your primary tool for navigating to the logical end of any line, while **g provides screen-based navigation for wrapped lines. Understanding the difference between these commands and knowing when to use each will significantly improve your editing efficiency.

Key takeaways:

  • Use $ for moving to the actual end of the logical line
  • Use g$ for moving to the end of the current screen line (when text wraps)
  • Combine **withoperatorsliked** with operators like `d, c,ory`, or `y` for efficient text manipulation
  • Use A to move to end of line and immediately enter insert mode
  • Practice with numbered repetitions (5$, 10$) for rapid navigation

By mastering these end-of-line navigation techniques, you’ll be able to edit text more efficiently and take full advantage of Vim’s powerful movement commands. Start by practicing the $ command in your daily editing, and gradually incorporate the variations as you become more comfortable with them.