
Getting Started with Vim
Table of Contents
🔰 Introduction
🔰 Modes
🔰 Movements
🔰 Insert
🔰 Find
🔰 Go to
🔰 Search
🔰 Remove and Delete
🔰 Replace and Repetition
🔰 Visual Mode
🔰 Practice
🔰 Introduction
Vim is one of the most powerful text editors I have ever used in CLI(Command Line Interface). There are so many files which need to be opened or edited. Vim can be the best choice because Vim is more than an editor. 😎
Installation commands on Ubuntu:
$ sudo apt update
$ sudo apt install vim🔰 Modes
There are two basic modes in Vim.
insertmode (In this mode you can edit texts in your own way).normalmode (In this mode you can manipulate and play with texts according to some instructions).
To switch between these modes you need to use Esc for normal mode and i for insert mode.
There is another mode in vim called visual mode. It is described here.
🔰 Movements
🚶 Basic Movements
You can move the cursor using following keys instead of uding arrow keys:
hfor moving towardsleft.lfor moving towardsright.kfor moving towardsup.jfor moving towardsdown.
🚶 Word Movements
Movement between words is possible via these keys:
worWfor moving to the beginning of the next word from current cursor and will continue.borBfor moving to the beginning of the previous word from current cursor and will continue.eorEfor moving to the end of the next word from current cursor and will continue.
Use number before these keys for moving fast e.g. 3w or 3W means cursor will move to the beginning of the 3rd next word.
🔰 Insert
📝 Insert Text Repeatedly
Suppose you want to insert yes 30 times consecutively. Steps will be as follows:
- Write 30 in normal mode.
- Then press
i,vimwill be switched fromnormal modetoinsert mode. - Write
yesthen pressEsc.
You will see that yes has been written 30 times consecutively.
📝 Insert New Line
By pressing o or O, a new line will be inserted and vim will be switched to insert mode as well.
🔰 Find
👓 Find a Character.
- Press
fqto find nextq. - Press
Fqto find previousq. - Press
3fqto find3rd nextoccurrence ofq. - Press
3Fqto find3rd previousoccurrence ofq.
👓 Find word under cursor
- Press
*to find the next occurrence of the word under current cursor. - Press
#to find the previous occurrence of the word under current cursor.
🔰 Go to
- Go to matching parentheses (
(), {}, []) by simply pressing%. - Press
0to go to the start of the line. - Press
$to go to the end of the line. - Press
ggto go to the beginning of the file. - Press
Gto go to the end of the file. - Press
Gwithnumber beforeto go to the specific linee.g.press3Gto go to the3rd line.
🔰 Search
You can search a specific text in a file i.e. for searching text in your file, steps will be as follows:
- Write
/textand then hitEnterbutton. - After that press
n, it will find the nexttextfrom the file and so on. - Press
N, it will find the previoustextfrom the file and so on.
🔰 Remove and Delete
✂️ Removing a Character
- By pressing
x, the character under the cursor will be removed. - By pressing
X, the character to the left of the cursor will be removed.
✂️ Deleting
The delete key is d. You can use it with movements' keys.
E.g. dw will delete the first word on the right side of the cursor. It also copies the deleted content so that you can paste it to another location by pressing p.
🔰 Replace and Repetition
🚜 Replace
For replacing a letter under cursor:
- Press
rand then write replaceable letter e.g.ato replace cursor’s letter bya.
🚜 Repetition
Repeat the previous command by pressing simply .
E.g.
d2wwill delete next two words of current cursor.- After that if you press
., the previous command i.e. next two words will also be deleted and so on.
🔰 Visual Mode
To switch to visual mode, just press v. In this mode you can select a text via movement keys.
E.g.
- Press
vand theneto select a word. - After that press
dto delete that word.
🔰 Practice
When you change the file in vim you have to know these commands as well:
:w(save):wq(save and quit):q(quit):q!(quit without saving)u(Forundo)ctrl+R(ForRedo):help(ForHelp)
This article is written according to this website. You can practice above commands in realtime. 🎉🎉🎉
Comments
Comments are not configured yet. Create the giscus identifiers for
shahnawaz-pabon/shahnawaz-pabon.github.ioand add them asrepoIdandcategoryIdinsrc/data/config.js.