Normal Mode: File Navigation
Normal Mode: File Navigation
One of vim
’s major ideals is that the user’s hands never need to leave the keyboard. Meaning various keys and keystrokes have been programmed to give the user navigation controls.
Most navigation occurs in Normal
mode.
This article will use the user-data.csv
used in the previous grep
and sed
sections. If you do not already have the file, you can re-download it with:
curl -s https://launchcodelearning.org/api/walkthrough/user?data_format=csv > user-data.csv
After running the command you should have a new copy of the user-data.csv
file:
The file should have 25001 lines in it (run wc -l user-data.csv
):
Open user-data.csv
with vim
First open the file:
vim user-data.csv
Output:
By default, vim
opens the file in Normal
mode.
Basic Navigation
Navigate Down j
In Normal
Mode the j
key will move the cursor down one line.
Upon pressing the j
key exactly one time it will move the cursor from over the f
in first_name
to the A
in Amy
immediately below it.
Try it out:
Notice the cursor moved over the A
in Amy
.
What happens if the j
key is pressed five more times? It should be over the W
in Wesley
:
Navigate Up k
In Normal
mode the k
key will move the cursor up one line.
As the cursor is currently over the W
in Wesley
pressing k
one time should place the cursor over the B
in Brian
:
Navigate Right l
In Normal
mode the l
key will move the cursor right one character.
The cursor is currently over the B
in Brian
pressing l
one time should place the cursor over the r
in Brian
:
Navigate Left h
In Normal
mode the h
key will move the cursor left one character.
The cursor is currently over the r
in Brian
pressing the h
key one time should place the cursor over the B
in Brian
:
Advanced Navigation
The Normal
mode basic navigation keys (h
, j
, k
, l
) are analogous to the direction keys in Insert
mode. Both suffer from the same drawback: tedious and slow navigation. Using the mouse would be a better experience than trying to navigate through a 25001 line file with the basic navigation commands.
Luckily, vim
has a ton of advanced Navigation commands in Normal
mode we will explore:
- go to specific line
- go to next word
- go to previous word
- go to beginning of line
- go to end of line
- go to top of file
- go to bottom of file
Go to specific line :[line-number]
+ <enter>
The cursor is currently on B
in Brian
typing:66
and hitting enter will navigate to the 66th line of the file:
This command took us to the first character of the 66th line E
in Ernest
.
Let’s navigate back to line 6 typing :6
and hitting enter:
Navigate Next Word w
In Normal
mode the w
key will move the cursor forward one word.
The cursor is currently on B
in Brian
(line 6) pressing w
one time will place the character on the next word (in this case the comma):
Pressing w
three more times should move the cursor to the i
in [email protected]
:
Navigate Previous Word b
In Normal
mode the b
key will move the cursor back one word.
The cursor is currently on the i
in [email protected]
pressing the b
should move the cursor back to the ,
preceding the word:
Navigate to Beginning of Current Line 0
In Normal
mode the 0
(zero) key will move the cursor to the beginning of the current line.
The cursor is currently located at 6,14
pressing the 0
key will move the cursor to 6,1
:
Navigate to End of Current Line $
In Normal
mode the $
(shift
+ 4
) key combo will move the cursor to the end of the current line.
The cursor is currently located at 6,1
pressing the $
key combination will move the cursor to 6,45
:
Navigate to Top of File gg
In Normal
mode the gg
key combo will move the cursor to the top of the file.
The cursor is currently located at 6,45
pressing the gg
key combo will move the cursor to 1,1
:
Navigate to Bottom of File G
In Normal
mode the G
(shift
+ g
) key combo will move the cursor to the bottom of the file.
The cursor is currently located at 1,1
pressing the G
key combo will move the cursor to 25001,1
:
Recap
Navigating in vim
can be accomplished in either Normal
or Insert
modes with basic navigation commands.
However, vim
Normal
mode provides many additional commands for navigating the file in an efficient manner.
There are additional navigation commands in vim
, but you will have to research those on your own!
You can provide a numeric prefix to the majority of the navigation commands like 5
and it will perform the provided navigation action the provided number of times.
For example:
- Navigate down five lines with
5j
- Navigate up 15 lines with
15k
- Navigate 32 characters to the right with
32l
- Navigate 6 characters to the left with
6h
- Navigate forward 3 words with
3w