Introduction
VIM is a text editor for writing code, where you navigate around the screen with the Keyboard instead of a Mouse.
It is based on original UNIX text editor named VI which came about in 1976 by Bill Joy. It was later improved by Bram Mooleenar in 1991 with bunch of improvements and named VIM.
Why VIM?
Why would you use VIM when we have modern IDEs? The answer is, when you write code, your fingers should be bound to keyboard all the time. Every time you touch mouse your work rate is interrupted.
Learning to code with VIM is same as learning something new from scratch, it will be painful at first but that pain will lead you to more precise and productive code editing in the future.
Installation
Linux/MacOS: VIM comes preinstalled.
Windows: Download VIM, Download the installer and install it.
How to use
Linux/MacOS: Type "vim filename.txt" in terminal (without quotations)
Windows: Open "gVim8.1" from desktop or search in start menu.
Vim Modes
VIM has many modes, but these four are commonly used:
Normal mode
If you see cursor thicker or little wide, means you're in normal mode.
Normal mode is Vim's default mode and you'll be using it most of the time. When you are in normal mode you can navigate and perform operations throughout the file.
You can move cursor between characters, words, lines, sentences, paragraphs. code blocks and more.
Characters
"h" (move left to character)
"l" (move right to character)
Words
"w" (move to next word)
"e" (move the end of current word)
"b" (move back to the previous word)
Lines
"j" (previous line)
"k" (next line)
Sentences
"(" (previous sentence)
")" (next sentence)
Paragraphs
"{" (previous Paragraph)
"}" (next Paragraph)
k (up)
j (down)
h (left)
l (right)
You can also use arrow keys to navigate but for best practice you must use (k,j,h,l).
Insert mode
Press i key in the normal mode to get into insert mode. The word INSERT will appear at the bottom of the screen.
You can think of it like edit mode, because now you can write/delete inside the file.
Keys to get into insert mode:
Make sure you're in normal mode then press,
i (insert from the right of the character)
a (insert from the left of the character)
I (insert from the start of the line)
A (insert from the end of the line)
Once you are done with editing your file, press Esc key to go back to the normal mode.
Command mode
Commands can only be typed in **Normal* mode.*
When you're in normal mode, you can write commands. Every command starts with ":".
Basic commands:
:w (save file)
:q (quit file. but the file must be saved first)
:wq (save and quit)
:q! (quit without saving)
There are many more commands, but these are the common and you'll need these all the time.
Visual mode
When editing text with Vim, visual mode can be extremely useful for identifying chunks of text to be manipulated.
Press the v key to enter visual mode. The word VISUAL will appear at the bottom of the screen.
Use the Arrow keys to highlight the desired text. You can use other navigation commands, such as w to highlight to the beginning of the next word or $ to include the rest of the line.
Once the text is highlighted, press the d key to delete/cut the text.
If you deleted too much or not enough, press u to undo and start again.
Move your cursor to the new location and press p to paste the text.
Tips and tricks
- To go forward 3 words, instead of www, we can do 3w.
- To go down 10 lines, instead of jjjjjjjjjj, we can do 10j.
- dw to delete a word.
- dd to delete the entire line.
- x to delete a character.
- r followed by new character to replace a character.
- To switch back to normal mode from any mode, press Esc.
There's still a lot to learn. Keep exploring. Happy coding.
If you learnt something from this article, please like, comment and share.
Follow me on Instagram.
Follow me on Facebook.
Follow me on Twitter.
Top comments (15)
That's super concise, I like that :) you should continue to write about it.
I've written a guide to learn Vim too, but it's more verbose. If somebody is interested: thevaluable.dev/vim-beginner/
Thank you.
I just went through your link, it's explained very well, I loved it. Great efforts man!
I've been using Vim for over 20 years.
I haven't figured out how to quit yet.
Hey, I have explained it in article, but here’s the quick solution:
When you’re in normal mode, just press,
“:w” (save file)
“:q” (quit file. but the file must be saved first)
“:wq” (save and quit)
“:q!” (quit without saving)
I hope it will help you. Thanks.
You missed ZZ, which is far less keystrokes and which tolerates everything
If you would like to improve the appearance of vim, the Enfocado theme will be very useful. Excellent guide! 😃
GET IT ON |
github.com/wuelnerdotexe/vim-enfocado
Thanks. I'll surely try this theme 😀
Thanks, really handy!
Awesome writeup bro! Keep 'em coming
Thanks man!
dd will delete an entire line instead of a word right?
yeah.
also if you want to yank a entire line you can use yy
Yes, thanks for the correction.
This is so helpful for beginners!
Thank you :D