Vim is an advanced text editor for Linux and Unix operating systems. Recently rated the #1 worldwide Linux editor, Vim is open-source and free. This article will show you how to create a great .vimrc profile.
What Is Vim?
First, if you are new to Vim: welcome! You have just started the great journey of learning one of the worlds best and most convenient editors.
Bram Moolenaar created Vim, the much loved Linux terminal based editor, in 1991. Vim stands for vi improved, and
vi was the classic old-school text editor for Unix, developed in 1976. Vim’s popularity has increased a lot, and the editor was recently named the #1 worldwide Linux editor by a major Linux forum. Vim is generally also preinstalled as the default editor in many Linux distributions.
Vim allows users to edit files in two sets of modes, the edit mode and the command mode, if you will, which likely makes navigation in Vim challenging for new users. Users are likely used to being in edit mode in most other popular text editors (OpenOffice writer etc.), but not in command mode. And command mode is the default mode that Vim will start in.
To give an example, if you open a file with Vim (using a command on your terminal prompt like
vi my_story.txt), you will enter Vim in command mode. You can now type the letter
i (a vi command) to enter edit (or specifically in this case insert) mode (
-- INSERT -- will usually appear on the last line of the terminal to highlight the mode you are in) and start typing your test. When you’re done, you press
ESC (the escape key) to return to command mode.
You can then again re-enter edit mode by for example tying
a (another vi command) to enter edit mode again (
a in this case stands for append and the cursor will jump one character to the right when entering edit mode using
a instead of
i). When done with editing (which allows full editing capabilities like pressing
cursor left/right etc.) you can again press the
ESC key to return to command mode.
When you’re done with editing the file in total, make sure you are in command mode (press the
ESC key again to be sure if you like) and type the key sequence
:wq!. This will write (
w) the file and quit (
q). If you have never used Vim before, then well done! This was your first Vim session 😉
The Vim editor is one of the few editors with such a sharp initial learning curve. However, after the initial learning curve, the knowledge of editor commands and usage continues to grow with a person over time in a more or less linear format. I write all of my scripts, articles and data wrangling in Vim, using it in only in combination with AutoKey to readily insert my template and HTML tags where needed.
If you would like to learn more about AutoKey, you may like to read our AutoKey: How to Replace Characters with Predefined Text Automatically in Linux article.
Defining a Great .vimrc Profile
If you have previously used Vim in various Linux distributions, you may have noticed how some distributions seem to fail setting up a correct Vim profile, leading to somewhat odd behavior when using Vim.
One of the things you may see (specifically on Mint 19 and some older Ubuntu versions) is that when you press cursor right you have to press another key for the cursor to actually jump, and this especially around highlighted/colored syntax (in shell scripts for example). Another oddity is strange behavior from mouse clicks, or when using tabs.
If you see any of these, or similar oddities, it’s really time to setup a great default .vimrc profile. The .vimrc file/profile contains all settings that Vim will read and use each time it starts. Over the years of using Vim, and especially around the time I logged a bug for one of these odd behavior items, I made a short .vimrc profile which negates these issues.
.vimrc file will and should be stored in your home directory. Let’s create the file using
vim itself. Note that you can generally use
vi to start
vim. First, we open the
.vimrc file in our home directory (which in Linux is represented by the tilde, i.e.
~, symbol) by executing:
At the command line. This should bring up the Vim editor. Now press the
i key to enter edit/insert mode. One you see
-- INSERT -- on the last line of your terminal (shown in most cases) you know you are insert mode. Then copy and paste the block of text below, and right-click with your mouse in the terminal window where Vim is running. Then click Paste.
set nocompatible colo torte syntax on set tabstop =2 set softtabstop =2 set shiftwidth =2 set expandtab
You should now see a screen similar to the following:
If you generally use four spaces instead of two (for your coding work), then simply replace all
ESC to leave edit mode, and next type the key sequence seen earlier:
:wq!. This will write out the file
~/.vimrc and exit the Vim editor. If you would like to verify the contents of the file, you can execute
cat ~/.vimrc at the command line.
The next time you enter Vim, all these settings will automatically be loaded by Vim from your
Besides setting a nice color scheme using the command
colo torte, we also turn on syntax highlighting with
syntax on. We set some better tab width handling options (generally indicating we want two, or four as described above, spaces as tabs).
Please note that the
torte color scheme does not seem to be available on Linux Mint 20.1, though it is available on some earlier Mint releases. If this is applicable to your system, you will receive a message to the same effect. Simply press enter and edit
~/.vimrc to remove this line.
set expandtab option specifically ensures that whenever we press tab it will be automatically translated to spaces instead. You can remove this line if you do not like this (tip: use Vim to do so and you can press
dd on a line, whilst in edit mode, to delete the entire line, even without entering edit mode).
However, the most important option is the one on the first line. The
set nocompatible ensures that a number of things – like the ones described above – work much better and without unclear behavior.
There is some discussion online about the perceived need to not set this option because this setting is deemed to be auto-enabled as soon as a .vimrc file is being used, however setting it will ensure this setting is active, and there is no negative side effect to setting it [again]. In other words, there is a potential upside to define it hard coded/statically whereas there is no downside to doing so.
This article placed Vim and Vi in historical context and described it’s popularity. After this we provided a small introduction to the edit and command modes in Vim and looked at creating a
~/.vimrc settings file for Vim which will ensure a much improved Vim user experience by reconfiguring tab widths, space settings, syntax highlighting, setting a color scheme and finally and most importantly avoiding buggy/unclear Vim behavior on various Linux distributions.