2.4 β€” Screen and File Navigation

2.4 β€” Screen and File Navigation#

When working with large files, you need efficient ways to move beyond single lines. This lesson covers scrolling, screen positioning, and file-level navigation.

File Navigation#

Jump to Line Numbers#

CommandAction
ggGo to first line
GGo to last line
{n}GGo to line n
{n}ggGo to line n
:{n}Go to line n (command-line)

Jump by Percentage#

CommandAction
50%Go to 50% of file
25%Go to 25% of file
75%Go to 75% of file

File Information#

CommandAction
Ctrl-gShow file info and cursor position
g Ctrl-gDetailed position info (columns, bytes, etc.)

Screen Navigation#

Cursor to Screen Position#

CommandCursor moves to…
HHigh β€” top of screen
MMiddle of screen
LLow β€” bottom of screen

These are extremely useful for quick jumps within the visible area.

With counts:

Screen Scrolling#

CommandAction
Ctrl-fScroll forward (down) one full screen
Ctrl-bScroll back (up) one full screen
Ctrl-dScroll down half screen
Ctrl-uScroll up half screen
Ctrl-eScroll down one line (cursor stays)
Ctrl-yScroll up one line (cursor stays)

The half-screen scrolls (Ctrl-d and Ctrl-u) are often most useful because they maintain context.

Repositioning the Screen#

Sometimes you want to keep the cursor in place but reposition what’s visible:

CommandScreen positions so cursor is…
zzCentered on screen
ztAt top of screen
zbAt bottom of screen

Use case: You jump to a line and want to see context below it:

  1. 42G β€” jump to line 42
  2. zt β€” put line 42 at top, see what follows

With Line Start#

CommandAction
z<Enter>Like zt but cursor at first non-blank
z.Like zz but cursor at first non-blank
z-Like zb but cursor at first non-blank

Paragraph and Section Movement#

Paragraphs#

Paragraphs in Vim are separated by blank lines.

CommandAction
{Jump to previous blank line (paragraph start)
}Jump to next blank line (paragraph end)

Sections#

Sections are defined by { at the start of a line (common in C code).

CommandAction
[[Jump to previous section start
]]Jump to next section start
[]Jump to previous section end
][Jump to next section end

In practice, for most code, { and } (paragraph movement) are more useful.

Sentence Movement#

CommandAction
(Jump to previous sentence
)Jump to next sentence

A sentence ends with ., !, or ? followed by whitespace.

Matching Brackets#

CommandAction
%Jump to matching bracket

This works with (), [], {}, and more.

Example:

function test() {
    if (condition) {
        doSomething();
    }
}

With cursor on the first {, pressing % jumps to the matching }.

Jump List Navigation#

Vim maintains a jump list of significant cursor movements.

CommandAction
Ctrl-oJump to older position
Ctrl-iJump to newer position
:jumpsShow jump list

What counts as a β€œjump”:

What does NOT count as a jump:

Change List Navigation#

Vim also tracks where you’ve made changes:

CommandAction
g;Jump to previous change position
g,Jump to next change position
:changesShow change list

Incredibly useful for returning to where you were editing.

Practical Navigation Patterns#

Pattern 1: Quick File Overview#

  1. gg β€” go to start
  2. Ctrl-f repeatedly β€” page through file
  3. G β€” go to end
  4. Ctrl-b repeatedly β€” page back

Pattern 2: Jump to Function and Center#

  1. /function_name β€” search for function
  2. n β€” find the match
  3. zz β€” center it on screen

Pattern 3: Edit, Check, Return#

  1. Make an edit
  2. G β€” check end of file
  3. Ctrl-o β€” return to where you edited

Pattern 4: Navigate Large Function#

  1. Position on opening {
  2. % β€” jump to closing }
  3. % β€” jump back to opening {

Pattern 5: Read Through Code#

  1. { or } β€” move by paragraph/function
  2. zz β€” center current position
  3. Repeat

Configuring Scroll Behavior#

Scroll Offset#

Keep cursor away from screen edges:

:set scrolloff=5     " Keep 5 lines visible above/below cursor

With scrolloff=5, scrolling happens before the cursor hits the edge, keeping context visible.

Smooth Scrolling (Neovim)#

Neovim supports smooth scrolling with plugins or built-in options.

Practice Exercises#

Exercise 1: File Jumps#

  1. Open a long file (100+ lines)
  2. gg β€” go to start
  3. G β€” go to end
  4. 50% β€” go to middle
  5. :75 β€” go to line 75
  6. Ctrl-o β€” jump back

Exercise 2: Screen Movement#

  1. Press H to go to screen top
  2. Press L to go to screen bottom
  3. Press M to go to screen middle
  4. Note how the cursor moves

Exercise 3: Scrolling#

  1. Ctrl-d β€” scroll down half screen
  2. Ctrl-u β€” scroll up half screen
  3. Ctrl-f β€” scroll full screen down
  4. Ctrl-b β€” scroll full screen up
  5. Feel the difference between half and full

Exercise 4: Screen Repositioning#

  1. Go to any line in the middle of a file
  2. zt β€” watch screen reposition (line at top)
  3. zz β€” watch screen reposition (line centered)
  4. zb β€” watch screen reposition (line at bottom)

Exercise 5: Bracket Matching#

  1. Open a file with nested brackets
  2. Position cursor on an opening {
  3. Press % β€” jumps to matching }
  4. Press % again β€” jumps back
  5. Try with (, [, etc.

Exercise 6: Paragraph Movement#

  1. Open a file with blank lines separating sections
  2. } β€” jump to next blank line
  3. } β€” jump again
  4. { β€” jump back
  5. Use this to navigate between functions

Summary#

CategoryCommands
Filegg, G, {n}G, {n}%
Screen PositionH, M, L
ScrollingCtrl-f/b (full), Ctrl-d/u (half), Ctrl-e/y (line)
Repositionzz (center), zt (top), zb (bottom)
Paragraph{, }
Bracket%
Jump ListCtrl-o (back), Ctrl-i (forward)
Change Listg; (back), g, (forward)

With these commands, you can navigate any file efficiently, no matter how large.