Vim – essentials
If you are interested in using vim or you have already used it but don’t understand it, this post can help you to understand how to use it consciously and efficiently.
In this following I am focusing on the essentials of vim with a practical first approach. The best and fastest way to learn using it, is to get your hands dirty. Hit the console to open vim and play with the essential commands. This post helps you by showing the most essential commands of vim and explaining the basics you need to know in order to start using it.
Importance of learning vim
Using vim without the proper understanding of the basics takes plenty of time. Instead of wasting time, we should focus on learning vim appropriately and save much more time by using vim efficiently. Save time by spending a bit more time on the foundations.
Brief overview what vim is
The name of vim related to vi text editor as vim based on vi. Vim stands for Vi improved. Vi stands for visual.
Vim is an advanced text editor available for most of the operating systems such as Linux, Mac and Windows. Vim operates with different modes and features such as macros, searching, global substitutions, syntax highlighting, various shortcuts, command repetition and so on. Vim is extremely powerful and is super easy to use. Once you have learnt the basics of vim you can increase your productivity by using it.
If you are on a Unix based operating system, you might have vim installed already. If you are not sure whether you have vim installed or not you can simply type vim in your console. If you see vim started, you have vim installed, however, if you encounter with command not found error message, you do need to install vim. You can install vim in different ways depending on the operating system you use. In case of Linux, perhaps you can install vim from repository.
In order to install vim visit https://www.vim.org where you can find instructions for the installation.
Using vim one of your basic intention is to open files. In order to do so you can type in your console:
There are graphical versions of vim as well in which case you can use your GUI to open a file with vim.
Vim most essential modes
There are plenty of ways to switch between modes in vim, however, in this post we are going to focus on the easiest ways.
Please note that vim is case sensitive.
When you start vim you are in normal mode by default. You are not able to edit the content of the file directly in normal mode, however, you are able to manipulate the content.
Command line mode
When you type colon : in normal mode you switch to command line mode. In this mode you can run various commands. After typing the command, you need to hit enter to execute the command.
If you want to directly edit the file, you can do so by entering insert mode. One of the ways to switch to insert mode is to hit INSERT button on your keyboard or just simply hit i on your keyboard.
If you want to exit vim you need to switch to command line mode and type q
Although this is the default way to exit vim, there are other factors you may need to consider if you are about to exit. If you have edited the file but not saved the changes you can ignore saving the file and use q! command which will exit from vim without saving the changes you have made. If you want to exit vim with saving the changes you can use the following command: wq which writes the changes and quits from vim.
Vim cursor navigation
You can navigate your cursor in vim by simply using the arrow keys on your keyboard that can switch between lines or characters in a line. Instead of the arrow keys you can use:
To move the cursor up a line: k
To move the cursor down a line: j
To move the cursor by one character to the left: h
To move the cursor by one character to the right: l
The advantage of using hjkl keys instead of the arrow keys can help you speed up with your basic navigation as you do not need to leave your typing position.
However, it is not the most efficient way regarding navigation. Vim has lots of ways to navigate more efficiently than navigating line by line or character by character. Keeping it simple I am going to show you the easiest ways to improve vim navigation skills.
In order to navigate to the last line of the file use: G
In order to navigate to the first line of the file use: gg
You can also enter to command line mode and type the line number where you want to jump to. Then, just hit enter to execute the command which in this case will navigate you to the line number you’ve entered.
In order to navigate to the end of the line where your cursor currently is, you can use: $
If you are familiar with regular expressions you will find it easy to memorize.
In order to navigate to the beginning of the line where your cursor currently is, you can use: ^
You can also navigate by jumping with the cursor word by word. In order to do so, you can use the following keys:
To navigate forward to the next word: w
To navigate backwords to the previous word: b
To navigate forward to the next word (Does not interpret special characters as word breakers. Jumps over every characters apart from whitespaces.): W
To navigate backwords to the previous word (Does not interpret special characters as word breakers. Jumps over every characters apart from whitespaces.): B
You can also navigate through the file page by page. In order to use page navigation, you can use (pageUp or ctrl+f) and (pageDown or ctrl+b) keys on your keyboard.
If you really prefer to use your mouse for navigating cursor, you can use the following command to enable this feature: set mouse=a
You might also need to use the following command to enable mouse feature: set ttymouse=xterm2
Even though mouse navigation is a convenient way to navigate, it is also a productivity killer that can prevent you from mastering vim navigation that could be much more efficient than using the mouse one.
Manipulate content in normal mode
The language of vim is English. As you are able to read this post you speak English, which means you can manipulate the file contents in vim normal mode very easily. For example, use the following key to delete a word from the cursor position: daw that stands for delete a word. If you want to delete three words the keys you need to press look like: d3w and guess what, that stands for delete 3 words. Easy, isn’t it?
If you want to delete an entire line you can press: dd keys (d twice). In order to delete 3 lines, you might already know the keys: d3d