What is Vim?
A free text editor for writing code
Useful when you have to navigate around the screen with your keyboard instead of a mouse
Popular among engineers
Why Vim?
Because why not? 😛
Easier to perform these tasks efficiently -
edit files
navigate among text inside a file
and much more…
When is Vim most useful?
When you can’t use GUI to navigate around, and thus bound to use only command line to perform operations
Example -
Consider a scenario that you are using an Amazon EC2 instance (i.e. a remote machine).
Now, you have pulled in few repositories (repo) from GitHub, and you want to make few changes in these repo’s files.
You can’t use a GUI based code editor (like VS Code, Sublime, etc) - your only option to interact with these files is to use command line.
This is where Vim comes to help.
FYI, there are 3 modes in Vim:
Normal → The first mode after entering into Vim. You can navigate around the text.
Insert → While in normal mode, if we hit “i” key, we enter into this mode. We can add/edit text.
Command-line → Can enter commands like “:q!”, “:wq“, etc.
Helpful commands of Vim
Open a file using vim »»» vim blog.txt
Keys to navigate in normal mode:
Move one word forward »»» shift + w
Move one word backward »»» shift + b
Move to the beginning of a line »»» shift + 6
Move to the end of a line »»» shift + 4
Jump to the top of the file »»» :1
Jump to the end of the file »»» :$
To check the file status (how many total lines, which line/which column) »»» control + g
To enable the ruler (shows lines and columns) »»» :set ruler
Delete a text:
Delete character by character (keep cursor at the beginning of word) »»» x
Delete the whole line (keep cursor at the beginning of line) »»» shift + d
Cut and paste a line:
to cut »»» dd
to paste »»» p
Search a word »»» /{word} + n
To quit the file:
without saving your changes »»» :q!
with your saved changes »»» :wq
There you go. Go nuts! 🤘
Credits: Got to learn about these vim commands from a nice online course which I took on Udemy by Jason Cannon on “Vim Masterclass“.