CIS120Linux Fundementals
A Gentle Guid to VIM (alias vi)
The vim
editor is a powerful text editor available in most Unix-like operating systems. To launch vim
, simply type vim
followed by the filename you wish to edit, such as vim myfile.txt
. When you open vim
, it starts in Command mode, which is the default mode. vim
operates in two primary modes: Command mode and Insert mode. Understanding how to switch between these modes is key to effectively using vim
.
In Command mode, you can navigate through the file, delete text, and perform various editing tasks. To switch from Command mode to Insert mode, you can press i
to insert text before the cursor, a
to insert text after the cursor, o
to open a new line below the current line, or O
to open a new line above the current line. For example, if your cursor is on the letter 'h' in "hello" and you press i
, you can start typing before 'h'. If you press a
, you start typing after 'h'. If you press o
, a new line opens below the current line and switches to Insert mode. If you press O
, a new line opens above the current line and switches to Insert mode. Once in Insert mode, you can type text as you normally would. To switch back to Command mode, press the Esc
key.
# Open a file
vim myfile.txt
# Insert text before the cursor
i
# Insert text after the cursor
a
# Open a new line below the current line
o
# Open a new line above the current line
O
# Switch back to Command mode from Insert mode
<Esc>
While in Command mode, basic navigation can be performed using the keys h
, j
, k
, and l
to move the cursor left, down, up, and right respectively. You can also use the arrow keys for navigation (this is much easier). For example, pressing 4j
will move the cursor down four lines. If you want to delete a character under the cursor, press x
. To delete an entire line, use the dd
command. For example, if you want to delete three lines starting from the current line, you can type 3dd
. Copying (or yanking) a line can be done with yy
, and you can paste it below the current line by pressing p
. For example, 3yy
will yank (copy) three lines, and p
will paste them below the cursor. If you make a mistake, you can undo your last change by pressing u
. To save (write) your changes to the file, use :w
, and to quit vim
, use :q
. If you want to save and quit simultaneously, you can use :wq
or the shortcut ZZ
.
# Move the cursor down four lines
4j
# Delete the character under the cursor
x
# Delete three lines starting from the current line
3dd
# Yank (copy) three lines
3yy
# Paste the copied lines below the cursor
p
# Undo the last change
u
To save your changes to the file, use the :w
command. To save and quit vim
simultaneously, you can use :wq
or the shortcut ZZ
. If you want to quit without saving, use :q!
.
# Save the file
:w
# Save and quit vim
:wq
# Save and quit vim (alternative)
ZZ
# Quit without saving
:q!
For more advanced navigation, vim
offers commands like gg
to go to the beginning of the file and G
to go to the end. You can go to a specific line by typing nG
, where n
is the line number. For example, 10G
takes you to line 10. Moving to the beginning of the current line can be done with 0
, and moving to the end of the line can be done with $
.
# Go to the beginning of the file
gg
# Go to the end of the file
G
# Go to line 10
10G
# Move to the beginning of the current line
0
# Move to the end of the current line
$
Editing in vim
can be very efficient with commands like r
to replace a character, cw
to change a word, and dw
to delete a word. For instance, if the cursor is on the letter 'h' in "hello" and you press rj
, the 'h' will be replaced with 'j', resulting in "jello". Using cw
on "hello" allows you to change the entire word, for example to "world", by typing cw world
. Using dw
will delete from the cursor position to the end of the current word. You can also select text visually by pressing v
and then moving the cursor to highlight the desired text. This selected text can be deleted, copied, or changed using d
, y
, or c
followed by a movement command. For example, d$
will delete from the cursor to the end of the line, and y2w
will yank (copy) two words.
# Replace the character under the cursor with 'j'
rj
# Change the word under the cursor to 'world'
cw world
# Delete from the cursor to the end of the word
dw
# Start visual mode to select text
v
# Delete from the cursor to the end of the line
d$
# Yank (copy) two words
y2w
Searching within vim
is straightforward. To search forward for a pattern, use /pattern
, and to search backward, use ?pattern
. For example, /hello
searches forward for the word "hello" and ?hello
searches backward. After performing a search, pressing n
will repeat the search in the same direction, while pressing N
will repeat it in the opposite direction.
# Search forward for 'hello'
/hello
# Search backward for 'hello'
?hello
# Repeat the search in the same direction
n
# Repeat the search in the opposite direction
N
When you are ready to exit vim
, you have a few options. To quit without saving, use :q!
. To save your changes and quit, use :wq
or the shortcut ZZ
, which performs the same function.
# Quit without saving
:q!
# Save and quit vim
:wq
# Save and quit vim (alternative)
ZZ
Practicing these commands will help you become proficient with vim
. Remember that pressing Esc
always returns you to Command mode, which is a useful fallback if you ever feel lost. As you get more comfortable, you can explore vim
's more advanced features, but mastering these basics will provide a solid foundation for effective text editing with vim
.
Working With Multiple Files in Vim
When working with multiple files in Vim, you can use buffers to keep several files open at once, while also taking advantage of split screens to view and edit files side by side. Let’s walk through how these tools make it easier to manage multiple files.
To start, you can open multiple files in Vim by listing them when you launch it. For example, to open file1.txt
and file2.txt
, type this at the command line:
vim file1.txt file2.txt
This command opens both files, each in a separate buffer. A buffer is where Vim stores each open file in memory, allowing you to switch between files easily. To move between buffers, use the :buffer
command followed by the buffer number. For example, if you’re viewing file1.txt
and want to switch to file2.txt
, type:
:buffer 2
You can find buffer numbers by typing:
:ls
The :ls
command shows a list of all open buffers, making it easy to see which files are available and their corresponding buffer numbers.
To open a new file in a buffer while in Vim, use:
:e file3.txt
This loads file3.txt
into a new buffer, which you can access with :buffer
commands or open in a new split screen if you want to view it alongside other files.
When you’re done with a file, you can close its buffer with the command:
:bd
This closes the buffer of the file you’re currently viewing. You can also delete a specific buffer by its number, like this:
:bd 2
You can also move forward and backward between bufferes with the :bn
(next buffer) and :bp
(previous buffer) commands.
If you want to view files side by side, you can use split screens. To open another file in a split screen, use the following command:
:split file2.txt
This command splits the window horizontally, placing file2.txt
above or below file1.txt
. To split vertically, you can use:
:vsplit file2.txt
Now, file1.txt
will be on the left and file2.txt
on the right. You can navigate between these split windows by pressing Ctrl + w
and then using the arrow keys. For example, Ctrl + w
followed by the right arrow key will move your cursor to the window on the right. This way, you can view and edit two files at the same time.
To close the split window where your cursor is currently active, use:
:q
This will close the current split. If there are unsaved changes, Vim will prompt you to save or discard them.
If you want to keep only the current split open and close all other splits, use:
:only
This command closes all other splits except the one you are currently working in, bringing you back to a single window view.
To force-close a split without saving any changes, use:
:q!
This will close the split immediately, discarding any changes.
These commands let you easily manage split screens in Vim and return to a single screen setup.
Using buffers and split screens allows you to handle multiple files efficiently in Vim. You can open files, switch between them with buffers, and view them side by side with split screens, giving you all the flexibility you need for multitasking on your files.
Table of popular vim Commands
Command | Description |
---|---|
vim myfile.txt |
Open a file |
i |
Insert text before the cursor (Insert mode) |
a |
Insert text after the cursor (Insert mode) |
o |
Open a new line below the current line (Insert mode) |
O |
Open a new line above the current line (Insert mode) |
<Esc> |
Switch back to Command mode |
h , j , k , l |
Move cursor left, down, up, right (also arrow keys) |
4j |
Move cursor down four lines |
x |
Delete character under the cursor |
3dd |
Delete three lines starting from the current line |
3yy |
Yank (copy) three lines |
p |
Paste copied lines below the cursor |
u |
Undo the last change |
:w |
Save the file |
:wq |
Save and quit vim |
ZZ |
Save and quit vim (alternative) |
:q! |
Quit without saving |
gg |
Go to the beginning of the file |
G |
Go to the end of the file |
10G |
Go to line 10 |
0 |
Move to the beginning of the current line |
$ |
Move to the end of the current line |
rj |
Replace the character under the cursor with 'j' |
cw world |
Change the word under the cursor to 'world' |
dw |
Delete from the cursor to the end of the word |
v |
Start visual mode to select text |
d$ |
Delete from the cursor to the end of the line |
y2w |
Yank (copy) two words |
/hello |
Search forward for 'hello' |
?hello |
Search backward for 'hello' |
n |
Repeat the search in the same direction |
N |
Repeat the search in the opposite direction |
Other vim commands that help with how vim appears
Command | Description |
---|---|
:set nu |
Set line numbers |
:set nonu |
Remove line numbers |
:colorscheme ctrl-d |
Colorscheme space then ctrl-d. This will provide a list of colorschemes |
:colorscheme color |
Colorscheme space then theme color. This will switch the color scheme to the color you entered |
:!bash % |
Run the current bash script in vim |
:noh |
Clear highlighting |