02-VIM
Material¶
What's Vim?¶
Vim is an acronym for Vi IMproved
. It is a free and open-source cross-platform text editor.
How to Install Vim¶
Vim comes pre-installed on most *nix operating systems. But if it's not installed on your system, you can install it with a package manager of your choice.
Here's the installation command for based operating systems:
sudo apt-get update sudo apt-get install vim
To ensure that it's installed properly, run
which vim
and you should get/usr/bin/vim
in your output.
Get Started with Vim¶
- enter
vim
into the editor
Vim Modes¶
There are many modes in Vim. But, we'll be looking at the 4 most important modes.
- Command Mode
- This is the default mode (also called Normal mode) in
Vim
. - Basically, to switch from one mode to another, you have to come to Command Mode first and then navigate to the other mode.
- The commands that you run without any prefix (colon) indicate that you're running the command in command mode.
- This is the default mode (also called Normal mode) in
- Command-Line Mode
- You can use this mode to play around with some commands.
- commands in this mode are prefixed with a colon (
:
). - You can switch to this mode by pressing
: (colon)
incommand mode
.
- Insert Mode
- This mode is used to edit the contents of the file.
- You can switch to insert mode by pressing
i
fromcommand mode
. - You can use the
Esc
key to switch back tocommand mode
- Visual Mode
- You can switch to this mode by pressing
v
from thecommand mode
.
- You can switch to this mode by pressing
Common Operations in Vim¶
Create a New File¶
run
vim
to get into Vimrun
:edit sample.txt
to create a new fileAfter running that command, you'll be in
command mode
To add some text to the created file, press
i
toinsert mode
- In this mode, you can type whatever you want into the file.
Save a File¶
To save a file, you have to switch from Insert mode to Command Line mode.
- To switch to Command mode from Insert mode, you have to press the
Esc
key.
- To switch to Command mode from Insert mode, you have to press the
To save the file, use command
:W
Exit Vim¶
The proper way to close the file and the Vim editor is by using the command:
:q
Alternatively, you can use a command that's the combination of the above 2 commands (save & quit) to quickly save and quit Vim.
:wq
Edit a File in Vim¶
- To edit a file, you have to open the file using Vim and switch to Insert mode.
- Let's open the sample.txt file using
vim sample.txt
- pressing
i
from the Command mode will switch to Insert mode. - Type anything you want
- Press
Esc
on the keyboard and type:w
to save the file and:q
to quit Vim.
Close the file without saving the changes¶
To close the file without saving the changes, you have to run :
q!
from Command mode.
Cut, Copy, and Paste Text from a File using Vim¶
Enter Visual mode by pressing the
v
key on the keyboard.Move your cursor, Visual mode highlights the text from the beginning up to your cursor.
hit
y
to copy the text or hitd
to cut the text.Move the cursor to the place where you want to paste the text.
Press
p (in lowercase)
to paste the text after the cursor andP (in uppercase)
to paste the text before the cursor.Press
:wq
to save and close the file
Other useful shortcut¶
yy
: copy current line8yy
: copy 8 lines, start from current liney$
: copy from the current position to the end of the liney^
: copy from the start of the line to the current positionyw
: copy by word (vocabulary)w
: jump to next word (start)e
: jump tp next word (end)
x (lower case)
: cut current character (backwards)X (upper case)
: cut current character (forwards)
dd
: delete current line3dd
: delete 3 lines, start from current line
u
: retrieve previous modificationr(lower case)
: replace current characterShift + ^
: Move to the start of the lineShift + $
: Move to the end of the line
gg
: Move to the first lineShift + H
: Move to the first lineL
: Move to the last line
:set nu
: show the line numberset nonu
: drop the line number
/[keyword]
: search keywordn
: jump to next search result
:s/[keyword]/[replace keyword]
: replace the first matched keyword as second input:s/[keyword]/[replace keyword]/g
: replace all matched keywords as second input (same line):%s/[keyword]/[replace keyword]/g
: replace all matched keywords as second input (whole file)