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#
| Command | Action |
|---|---|
gg | Go to first line |
G | Go to last line |
{n}G | Go to line n |
{n}gg | Go to line n |
:{n} | Go to line n (command-line) |
Jump by Percentage#
| Command | Action |
|---|---|
50% | Go to 50% of file |
25% | Go to 25% of file |
75% | Go to 75% of file |
File Information#
| Command | Action |
|---|---|
Ctrl-g | Show file info and cursor position |
g Ctrl-g | Detailed position info (columns, bytes, etc.) |
Screen Navigation#
Cursor to Screen Position#
| Command | Cursor moves to⦠|
|---|---|
H | High β top of screen |
M | Middle of screen |
L | Low β bottom of screen |
These are extremely useful for quick jumps within the visible area.
With counts:
3Hβ 3 lines from top of screen5Lβ 5 lines from bottom of screen
Screen Scrolling#
| Command | Action |
|---|---|
Ctrl-f | Scroll forward (down) one full screen |
Ctrl-b | Scroll back (up) one full screen |
Ctrl-d | Scroll down half screen |
Ctrl-u | Scroll up half screen |
Ctrl-e | Scroll down one line (cursor stays) |
Ctrl-y | Scroll 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:
| Command | Screen positions so cursor is⦠|
|---|---|
zz | Centered on screen |
zt | At top of screen |
zb | At bottom of screen |
Use case: You jump to a line and want to see context below it:
42Gβ jump to line 42ztβ put line 42 at top, see what follows
With Line Start#
| Command | Action |
|---|---|
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.
| Command | Action |
|---|---|
{ | 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).
| Command | Action |
|---|---|
[[ | 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#
| Command | Action |
|---|---|
( | Jump to previous sentence |
) | Jump to next sentence |
A sentence ends with ., !, or ? followed by whitespace.
Matching Brackets#
| Command | Action |
|---|---|
% | 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.
| Command | Action |
|---|---|
Ctrl-o | Jump to older position |
Ctrl-i | Jump to newer position |
:jumps | Show jump list |
What counts as a βjumpβ:
G,gg,{n}G%(bracket matching)(,),{,}(sentence/paragraph)H,M,L- Search (
/,?,n,N) - Marks
What does NOT count as a jump:
j,k,h,lw,b,ef,t,F,T
Change List Navigation#
Vim also tracks where youβve made changes:
| Command | Action |
|---|---|
g; | Jump to previous change position |
g, | Jump to next change position |
:changes | Show change list |
Incredibly useful for returning to where you were editing.
Practical Navigation Patterns#
Pattern 1: Quick File Overview#
ggβ go to startCtrl-frepeatedly β page through fileGβ go to endCtrl-brepeatedly β page back
Pattern 2: Jump to Function and Center#
/function_nameβ search for functionnβ find the matchzzβ center it on screen
Pattern 3: Edit, Check, Return#
- Make an edit
Gβ check end of fileCtrl-oβ return to where you edited
Pattern 4: Navigate Large Function#
- Position on opening
{ %β jump to closing}%β jump back to opening{
Pattern 5: Read Through Code#
{or}β move by paragraph/functionzzβ center current position- 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#
- Open a long file (100+ lines)
ggβ go to startGβ go to end50%β go to middle:75β go to line 75Ctrl-oβ jump back
Exercise 2: Screen Movement#
- Press
Hto go to screen top - Press
Lto go to screen bottom - Press
Mto go to screen middle - Note how the cursor moves
Exercise 3: Scrolling#
Ctrl-dβ scroll down half screenCtrl-uβ scroll up half screenCtrl-fβ scroll full screen downCtrl-bβ scroll full screen up- Feel the difference between half and full
Exercise 4: Screen Repositioning#
- Go to any line in the middle of a file
ztβ watch screen reposition (line at top)zzβ watch screen reposition (line centered)zbβ watch screen reposition (line at bottom)
Exercise 5: Bracket Matching#
- Open a file with nested brackets
- Position cursor on an opening
{ - Press
%β jumps to matching} - Press
%again β jumps back - Try with
(,[, etc.
Exercise 6: Paragraph Movement#
- Open a file with blank lines separating sections
}β jump to next blank line}β jump again{β jump back- Use this to navigate between functions
Summary#
| Category | Commands |
|---|---|
| File | gg, G, {n}G, {n}% |
| Screen Position | H, M, L |
| Scrolling | Ctrl-f/b (full), Ctrl-d/u (half), Ctrl-e/y (line) |
| Reposition | zz (center), zt (top), zb (bottom) |
| Paragraph | {, } |
| Bracket | % |
| Jump List | Ctrl-o (back), Ctrl-i (forward) |
| Change List | g; (back), g, (forward) |
With these commands, you can navigate any file efficiently, no matter how large.