
Important VIM commands for daily usage
Important VIM commands is selected vim commands that every programmer shall have by heart.
By far important vim commands might be those we use in everyday programming life. But there more to it.
What is vim
Vim is a smart text editor for linux distros. It allows plugs ins for different usages. Basically, it pretty much knows most of programming languages you open with it.
Important vim commands will allow you to have a fast and productive usage of VIM while working on it.
Why should I use vim
You should use vim because it is cool. Really it is cool. Apart from being cool, it is light weight and almost available in most linux distributions.
Vim allows having third party plugins that will allow you to do multiple tasks from coloring to memory based copy and more.
Vim is programming language friendly. If you are working on java, it will highlight the keywords for you. Same for PHP or javascript or almost for lots of languages.
how it install vim
Usually VIM will come with most of the linux flavors:
If you are on Ubuntu installing vim on ubuntu will be
sudo apt-get install vim
If you are on redhat linux (RHEL), installing vim on redhat linux will be
sudo yum install vim
and installing vim on mac computer (machintosh) will be
brew install vim
How to open file with vim
Once vim is installed using either of the above or other methods, it can open any text based file using the following command:
vim /path/to/file
When the above command is issued, vim will open the file and by default the cursor will be at the first line.
How do I edit a file using vim
1. Open the file using vim /path/to/file
2. press the i key from your keyboard and the vim mode will be changed to edit mode.
Now you can move the cursor as you want and and type normally.
How do I save the edited file using vim
What command to use to save file in vim?
Once editing or adding new content to the file is done issue do the following:
First, exit from edit more by pressing ESC key.
Then issue the following
:w
the colon w will save your changes to the physical file.
How to exit from vim? What command to exit from vim
:q
How do I exit from vim editor?
When you press colon followed by ‘q’, it will quit or exit the editor.
Go to line in vim editor
vim doesn’t have scrollbars or anything that you can use with mouse. Every thing will be done using important vim commands
If that is the case, how do you jump to line that you want? For that using colon followed by the line number will land you to the line.
:45
The above command will take you to line number 45, for example.
Go to the end of file in vim
How to go to the end of the file, last line in vim? typing :g will take you to the end of the file. Make sure you are not in edit mode. If you are on edit mode, exit from it by pressing esc key.
:g
Going to the first line is also simple, you use color 1 to go to the first line in vim.
How to search in vim for a word
As an editor, vim provides a way to search for specific words. Searching is done while you are out of the edit mode. Use forward slash(/) followed by the word you are looking for. For example if you are looking for word “target” then you will do /target
.
The above will take you to the line containing the word target.
If you want to get the next occurrence of the word, keep typing the ‘n’ key. It will take you to the next “target” word in the search.
Search from bottom in vim
By default vim will begin the search from top, if you want to search from end of file up, then use :?
in place of question word.
If you want to get the last occurrence of the word “target” in the editor, then use :?target
How to copy paste single line in vim
Just like any editor, VIM allows to copy and paste both in the same editor or across another editor
To copy a line use yy
command (yank) being on the line itself.
First go the line, say you want to copy the line at 32 so do :32
Then press the key ‘y’ twice yy
Then to paste it, go to the line you want to paste and press ‘p’ (paste). This will past the line copied to on the current location.
more to come…