VIM is awesome
Table of Contents
What is Vim?
Vim (Vi IMproved) is a completely free, open-source, screen-based text editor developed by Bram Moolenaar.
Instructor
Dr. Ambrogi FrancescoWhy you should use Vim?
If you work/study in computational science, chances are you will need to perform numerical simulations on large super-computers by accessing them through Secure SHell connection. Most clusters use Linux as the main operating system and once you are connected to the cluster you will quickly realize that:
- There is NO graphic interface (in most cases), and all you will see is a terminal window.
- You will not be able to use your favourite text editor in graphic mode.
Do not dispair! Vim is here to save your life. Vim is, by default, present in almost all Linux distributions. Type vim on your command line, and this is the output you should see:
The first thing you will realize is that the vim welcome message pops up in the terminal window itself and that, from now on, you can take your mouse (or trackpad) and throw it in the garbage! You might not see it right now, but this is one of the hidden beauties of Vim.
Quitting Vim, not an intuitive task
Everyone who uses Vim on a daily basis has probably done this joke at least once: go to your mother (or best joke buddy), put them in front of an open Vim screen, and ask them a simple question “Please, can you quit?”. After a few seconds you will see the desperation in their eyes, not knowing what is going on, and 98% of the cases they will end up closing the terminal which will kill the entire process.
After this fun intro, here is how you quit Vim: press :q (colomn followed by q)
Give Vim a new look
Right off the bat you might think Vim looks pretty boring, especially if compared with recent very fancy text editors (e.g. Atom, VS code, etc.). To give you an idea, here is a simple function (witten in Julia) to calculate the volume of a spehere:
If you are ready to give up Vim because of its look (at first), I totally understand. However, let’s not judge the book from its cover, and take a look at this:
Much better, right?? We have now highlighted syntax for different programming languages, we have line numbers, automatic indentation, and it underlines where you currently are with the cursor (which in Vim is pretty crucial). So how do you make these changes in Vim? Again, this is not intuitive. There is no dropdown settings menu where you can edit all the perks.
At start-up vim automatically reads a config file called vimrc. This is the equivalent of your bashrc file for your terminal. If you just installed Vim or if you are a brand new user, chances are you do not have this file, so the first thing to do is to create the file at home: touch ~/.vimrc
Inside this file you can now put all your precious settings to make Vim look very fancy, probably even better than mine. Here is a selection of settings which I believe will be useful for any Vim user:
set nocompatible
filetype on
filetype indent on
syntax on
set number
set cursorline
From top to bottom: nocompatible fixes compatibility problems between Vi and Vim, filetype on makes sure Vim recognizes the file by the extension and syntax, indent on follows indentation rules set by the given programming language, syntax on highlights keywords and functions based on the given programming language, set number put the line number on the left side, and set cursorline underlines the location where the cursor is.
Four VIM modes
The most important thing to realize as soon as Vim starts up is understand in what mode is working. There are 4 MAIN modes:
- Normal: normal mode is the default start-up mode for Vim. This mode is read-only and you will be unable to edit the file. Very useful when consulting or studying a piece of code.
- Insert: upon pressing i you will enter insert mode. In this mode you will be able to freerly edit the file. Insert mode can also be activated by pressing INS on a full-size keyboard. Two very useful sub-commands are: a will turn on instert mode and move the cursor after the current character. o will turn on insert mode and move the cursor one line below.
- Visual: this mode allows you to visually highlight (slect) text areas and perform operation (cut, copy, move) on them. Press v to enter visual mode which will mark the beginning of the selection. You can use the arrow keys to highlight the desired text.
- Command line: this mode will allow you to perform operations such as: quit, save, replace, and search. Enter the command line mode by typing : within normal mode.
Example: let’s take the piece of code shown before, and let’s highlight only the formula for the volume of the sphere. You would put the cursor on that line, then press v and move the cursor to the next line pressing ↓. Here is the result:
The golden rule: if you do not know in which mode you are in, or if you want to go back to normal mode just press ESC. You will find that when you will be more used to using Vim, the ESC key will be on of the most used on your keyboard.
The status bar: a pretty awesome tool that will give you useful information on the file you are editing with Vim. The status bar is situated at the bottom of the file and (in my case) looks like this:
In this case the status bar tells me that: I am in insert mode, and I am modifying the file named test.md located in ~/Desktop/, and finally that the file is of type markdown. To setup the status bar like I did, here are the changes that must be included in the ~/.vimrc file:
set statusline=
set statusline+=\ %F\ %M\ %Y\ %R
set statusline+=%=
set laststatus=2
The 5 basics Vim commands to get by
Here are (in my opinion) the 5 basic commands to use Vim on a daily basis as a beginner.
-
The save command: suppose you are now in insert mode (after typing i, o, a, or INS in a full size keyboard) and say that you are happy and want to save the changes to your file, the command to save is :w and ENTER. If you want to save and quit Vim, you would just type :wq and press ENTER.
-
The undo command: suppose you made some changes, but want to go back, NOT A PROBLEM: within a session Vim keeps track of your changes and you can go back by pressing u until the oldest change.
-
Navigation keys: Vim is designed to improve your productivity while writing codes. You don’t need to use arrow keys (usually placed on the far right of your keyboard) to navigate your code. Withing normal mode you can move around using h (left), j (down), k (up), l (right). Practice using these keys instead of the arrows and you will never regret it!
-
Delete line command: you can delete an entire line by typing dd within normal mode. This command will delete the line the cursor is sitting at.
-
The search command: there are a few ways in which you can search in Vim. If you activated the line number, you can go directly to a specified line number # by typing:# and ENTER. You can also perform a word search by typing / followed by the word you are searching for. If you press ENTER Vim will go to the word you searched that is below and closest to the cursor. At that point if you type n repeatedly Vim will jump to every location in the code where that word appears. When you reach the bottom of the code Vim will pop a message in the status bar “search hit BOTTOM, continuing at TOP” and will move to the top of the file. By default, search words are not highlighted and the search is case sensitive, if you want to highlight the word and ignore the upper or lower case letters here are the changes you need to add to the ~/.vimrc file:
set incsearch
set ignorecase
set hlsearch
This is FAR beyond a complete course on Vim text editor, but just a quick showcase of useful commands that may spark your curiosity to use this wonderful tool. Keep checking as I will add more sections soon …