Terminus by Warp
Go To Top of File in Vim

Go To Top of File in Vim

Glory Kim
Glory Kim
Software Engineer, Loom

The short answer

When in normal and visual mode, pressing [.inline-code]gg[.inline-code] goes to the beginning of the file.

While [.inline-code]gg[.inline-code] is the most commonly used command, there are other shortcuts to bring the cursor to the first line of a file. 

  • [.inline-code]1G[.inline-code]
  • [.inline-code]:1[.inline-code]
  • [.inline-code][[[.inline-code]

[#go-to-first-top-column]Navigating to the first top column[#go-to-first-top-column]

All aforementioned commands will place the cursor on the first non-whitespace character.

To navigate to the first column of the first line instead, you can use:

[.inline-code]gg0[.inline-code] or [.inline-code]1G0[.inline-code]. The addition of the [.inline-code]0[.inline-code] brings the cursor to the beginning of the line.

[#highlight-sections-from-top]Highlight sections of the code[#highlight-sections-from-top]

In Vim, there are two ways you can highlight text starting from the first line of the file.

To highlight one or more lines starting from the top of the file, you can:

  1. Press the [.inline-code]ESC[.inline-code] key twice to enter Normal mode.
  2. Press [.inline-code]gg[.inline-code] to bring the cursor to the first non-whitespace character at the top.
  3. Press the [.inline-code]v[.inline-code] key to enter Visual mode.
  4. Use the arrow keys, the mouse, or the [.inline-code]<line-number>gg[.inline-code] command to widen your selection.

To highlight every line from the current cursor position to the top of the file, you can:

  1. Press the [.inline-code]ESC[.inline-code] key twice to enter Normal mode.
  2. Press the [.inline-code]v[.inline-code] key to enter Visual mode.
  3. Press [.inline-code]gg[.inline-code] to bring the cursor to the first non-whitespace character of the file.

[#delete-sections-from-top]Delete sections of the code[#delete-sections-from-top]

To delete everything from the top to the current line, you can press [.inline-code]dgg[.inline-code].

To delete everything from the top to the bottom of the file, you can run the [.inline-code]:%d[.inline-code] command.

To delete everything from your cursor location to the top of the file, you can press [.inline-code]d[[[.inline-code].

[#search-and-replace-keywords-from-top]Search and replace keywords from the top of the file[#search-and-replace-keywords-from-top]

To find and replace keywords from the top of the file, you can provide a range to the substitute command as follows:

[.inline-code]:1,<line number>s/<search-key>/<replace-key>[.inline-code]

For example, [.inline-code]:1,7s/hello/world[.inline-code] replaces any text from lines 1 to 7 matching [.inline-code]hello[.inline-code] with [.inline-code]world[.inline-code]. 

In addition to navigating from the top of the file, read more about how to move to the end and to a specific line of a file.

Experience the power of Warp

  • Write with an IDE-style editor
  • Easily navigate through output
  • Save commands to reuse later
  • Ask Warp AI to explain or debug
  • Customize keybindings and launch configs
  • Pick from preloaded themes or design your own
brew install --cask warp
Copied!
Join the Windows waitlist:
Success! You will receive an email from Warp when the release is available to download.
Oops! Something went wrong while submitting the form.
Join the Linux waitlist:
Success! You will receive an email from Warp when the release is available to download.
Oops! Something went wrong while submitting the form.
Join the Linux waitlist or join the Windows waitlist
Join the Windows waitlist:
Success! You will receive an email from Warp when the release is available to download.
Oops! Something went wrong while submitting the form.