Last Updated on January 5, 2025 by Nathan Bowen
Why is it essential to know how to navigate and edit files using Vim?
By default, most Linux operating systems use Vim as the default editor. One use case is when doing certain configurations in your system files.
Getting started
Create or open a file in the Linux terminal using the Vim command
vi <filename>
Vim Modes
There are three modes.
Command mode. – once you open the file, you will be in this mode. This mode allows you to change to other modes and operations.
Note: When you are in another mode, press `esc
` to come back to the command mode.
Insert mode – you can change to this mode by pressing `i` when you want to manipulate text
Last-line mode. – you use this mode when Saving or Exiting a file by pressing :
in command mode.
Navigating through the contents of the file
While in command mode, the following keys will help you:
- Use arrow keys or:
h
for leftj
for downk
for upl
for right0
to start of the linew
to next wordb
to the previous word
- Go to the first line at the top of the file:
gg
- Go to the last line at the bottom of the file:
G
Searching through the contents of the file.
While in command mode, you can search for a word or pattern by pressing / and typing the pattern. and pressing enter.
/pattern_or_Word
After getting the first result of the searched pattern, you can press:
- n – to get the next result of the pattern or word
- N – to get the previous result of the pattern or word.
Deleting Lines
While in command mode, if you want to delete a single or multiple lines, you can use:
- dd – to delete a single line from where the pointer is.
- 5dd – to delete 5 lines (you can put any number of lines before the dd)
Adjusting the Display to show line numbers
For ease of reference, you can add a display line number column to your file by invoking this command:
:set number #adds the line number column
:set nonumber # removes the added line number column
Saving and exiting
Below are some tips on saving and exiting.
:x #saves file
:wq #saves and exits the file
:q! #Exits file without saving
Summary
Learning about these basics will make Vim a priceless tool for your editing needs in system files. Use it more frequently and you will increasingly familiarize yourself with these commands and discover that it has a lot more to offer.
Leave a Reply