First steps in Vim
TL;DR: To quit Vim, use
During lockdown I’ve had plenty of time to mess around with new tools and change my terrible work habits; but most of all, I discovered Vim and started using it as my primary text editor and IDE.
In this article, I will explain how vim was born, and how you can start using it yourself with a few tips! I will mostly focus on how to use it properly rather than doing an in-depth tutorial.
It all starts with vi
vi is a lightweight & super fast text editor created by Bill Joy in 1976, and has become the standard text editor of Unix, which means you’ll be able to launch
vi on basically any linux machine you encounter.
Vim, or Vi IMproved, is a highly customizable fork of
vi which first version was released in 1991 by Bram Moolenaar. It features syntax highlight, many plugins and options, a bountiful documentation (
:help) and a significant amount of StackOverflow questions.
It also has its own language, vim script, made especially for customization and plugin development.
Since March 2006, Vim’s profit and donations are redistributed to help children in Uganda (see more here).
Neovim is the fork of Vim I’m currently using. Its primary goal is to improve Vim’s maintainability, code readability, and to encourage more developers to participate to the Vim project.
What’s so special about Vim?
That’s right, why not use a standard text editor like mousepad or gedit?
There are many advantages to using Vim:
- It’s open source;
- It’s very fast, both to load files and edit them;
- Uses very little system resources;
- It can be extended through plugins;
- Once you master it, you won’t really want to use something else because it’s so efficient (:
- Its configuration is portable, which means you only have to take your
init.vimwith you to use your config.
If you use someone else’s Vim config, there are chances that you won’t be able to use it, because that’s what makes Vim great: to be completely at ease with it, you have to make it your own!
This comes with some downsides: sometimes you will be frustrated by it, and you’ll have to spend a good amount of time practicing before being as fast as with your usual IDE, but it’s all worth it! The payoff is huge.
Vim is keyboard-based, meaning that your hand movements will be greatly reduced, and you won’t even need your mouse to do anything!
There are 4 “basic” modes in Vim (there’s actually plenty more):
The default mode in Vim. It enables you to switch between modes easily and use key bindings. If you are in any other mode, you can use the
<ESC>key to switch to normal mode;
Can be switched to using the
a(Append) keys. It’s the mode you’ll use to write stuff down;
This mode is useful for calling commands and scripts inside Vim. It can be switched to using the
:key. It’s also used to save your work and quit vim, which we’ll talk about later.
Primarily used for text selection, it features 3 different modes which will be detailed later on.
You usually switch modes all the time, so you should remember what all modes do, and be able to switch between them quickly. It will come with practice, and faster than you’d think!
Learning the basics
How do I quit??
The age-old question! How the hell am I supposed to exit Vim??
The answers are very simple. Make sure you’re in normal mode by pressing
<ESC>, and try the following:
||saves to file (the filename is optional)|
||quit and save|
||quit all tabs/panes and saves them|
||quit and save if modified|
How do I move?
There are LOADS of ways to move within Vim, the most common is the dreaded
In normal mode, the hjkl keys behave like directional arrows, with the following configuration:
The advantage here is that you don’t have to move your fingers from the home row. It might seem a bit impractical at first, but believe me it’s way better than moving your hands to the arrow keys and back.
But movement in Vim doesn’t stop here! You have many options to move forward or backwards, and you can even remap them to new keys. Below is a little list (use in normal mode):
||jumps to the end of a line|
||jumps to the first character of the line|
||jumps 30 lines down|
||jumps 30 lines up|
||jump to the last place your cursor has been to (restores last buffer|
||jumps to the end of the next non-whitespace word (
||jumps to the beginning of the previous non-whitespace word|
||jumps to the first occurence of
||jumps to the first occurence of
||jumps to the
||jumps to the beginning of the file|
||jumps to the and of the file|
||jump to the next place your cursor has been to (opposite of
Of course, you can enhance your movement within Vim with the help of plugins.
The thing you do most besides writing and moving is probably selecting text and editing it.
Like every feature in Vim, there are hundreds of ways to edit text, I made you a little list of the important basic ones:
||Deletes a character right on the cursor|
||Deletes a character right before the cursor|
||Replaces a single character on the cursor|
||Replaces multiple characters (enter REPLACE mode)|
||deletes motion characters|
||deletes the current line|
||deletes and goes into insert mode|
||yanks (copy) motion characters|
||yanks the current line|
||pastes the content of the clipboard right after the cursor|
||pastes the content of the clipboard right before the cursor|
||indent or unindent selection|
||replaces first with second (once)|
||replaces first with second (all of them)|
||prints the content of the command or file|
NOTE : when you delete something, it gets stored in your Vim clipboard, so you can paste it somewhere else!
And as any editor does, there are undo and redo commands. Please note that when you go to INSERT mode, write text and go back to NORMAL mode, it will count as one entry on the undo stack (see more here).
||undo the last edit command|
||undo the changes made to a line|
||redo the last undo|
Vim commands, operators and motions
Most commands and keybindings that transform text use an operator and a motion to work. Let’s take the example of the
d command key; it is used for deleting characters.
d<motion>, the deletion will be extended to the
djwill delete the current line and the next one;
d$will delete everything from the cursor to the end of the line;
dewill delete everything from the cursor to the next whitespace character.
You can also add numerals to your command:
d3jwill delete the current line, and 3 lines below that (this is also the same thing as
d4ewill delete the 4 next words.
Let’s learn text selection now. For this, you have to switch to
VISUALmode, here’s how it works:
VISUAL: selects one character at a time, can be toggled using the
vkey while in normal mode;
V-LINE: select one line at a time, using the
V-BLOCK: selects ‘blocks’ of text, using the
Every character that’s selected by the visual mode will be affected by the
d commands (we can say that the deletion is extended to the selection).
It would look like this:
Also, if you want to insert text in multiple lines at once, just select them and press
I. When you come back to insert mode, the lines will appear in all the lines you selected.
Some more commands!
Windows (or splits) are one of the features you just can’t live without if you’re working on big projects where you need to edit multiple files quickly.
For these, we will use the
Ctrl+w prefix, then one of the following commands:
vto split the view vertically;
lto move to the left, lower, upper or right window respectively;
Lto move the window to the left, bottom, top or right side of your terminal;
>to increase the vertical and horizontal size of the window;
<to decrease the vertical and horizontal size of the window;
=to reset the windows size to their default value;
:split filenameopens filename in a new window split;
_to maximize the current window;
Ok, windows are cool, but do you know what’s even cooler? TABS! In Vim, they are strictly controlled by commands.
You can choose to open multiple files in tabs by using the
-p vim option:
vim -p file1 file2
And move between the two by using the
You can close tabs by using
:tabclose, and create new ones with
Although it sounds simple, it remains VERY tedious to enter command mode and type this every time you want to change views! How would you solve this problem? The answer is keybindings!
As said before, customization is a big part of why you should use vim. Everything from colors, keybindings, modes, macros, etc. can be changed one way or another. The first way is by using the command mode, like when you type the following while in NORMAL mode (try it for yourself!):
Impressive! But when you quit Vim, the setting will be reset. 😔
To make the changes persistent, you have to write them in your configuration file. If you’re using Vim, it’s in
~/.vimrc, and if you use Neovim, it’s probably in
NOTE: if you write something in the configuration file, don’t forget to remove the
:, it’s only specific to the command mode).
Literally any action and command can be assigned to a key, and it is a big part of why Vim is said to be “programmable”. Let’s take a walk through the basics of what you need to know, starting with the
Mapping something to a key means that when you press that key in a specific mode, it will execute said thing. it follows the syntax:
map <key(s)> <commmand> unmap <key(s)>
map will bind the command to a key in a mode.
Here are some of the different
map types to put in your
A few useful examples:
map \| :r!ls
This line will bind the
| key to “printing whatever the
ls command puts out”. Note that the result will be printed inside the buffer you’re working on.
The “Leader key”
The leader key is something I discovered fairly recently. It will act as a prefix, and it can be almost any key. You can set it by writing the following line to your configuration file:
" Unmapping 'key' and mapping it to leader nnoremap <key> <Nop> let mapleader="<key>"
What it does is simply add a whole new way to make keybindings. Let’s take an example from my config file; the
yy command copies the entirety of the current line, including any linebreak
\n. If I want to be able to copy the current line without that linebreak, I will add the following:
" ^ goes to the beginning of the line " d$ deletes everything until the last printable character of the line map <leader>dd ^y$
When you press your leader key, followed by
dd, it will delete the line without deleting the newline!
I have it set to
<space> because I feel like the leader key should be accessible at all times, and the
<space> isn’t used for something important in NORMAL mode. Therefore it looks like this:
nnoremap <Space> <Nop> let mapleader=" "
You can make bindings with the leader key like this:
" Map <leader><tab> to 'switch to next tab' map <leader><tab> :tabn<CR>
It’s quite useful: if you happen to change your leader key, every binding made that include that key will be changed too, so don’t fret too much about which one you’ll use!
I won’t be getting too deep into plugin territory, because it’s easy to get lost and list them for hours.
Plugins are a quick way to add a considerable amount of new features to your Vim config, and some of them can completely change your use of Vim!
The simplest way to install plugins is through a plugin manager. I myself use vim-plug because it’s incredibly quick and you barely have to remember 2 or 3 commands to use it properly.
Check out how to install it here or find your own one.
When vim-plug is installed, all you have to do is add these lines to your configuration file:
" Plugins will be downloaded under the specified directory. call plug#begin('~/.vim/plugged') " Put your plugins list here Plug 'easymotion/vim-easymotion' Plug 'itchyny/lightline.vim' " List ends here. Plugins become visible to Vim after this call. call plug#end()
When you want to add a plugin, just add it to the list and use the
:PlugInstall command! On the contrary, if you need to remove a plugin, erase it from the list and run
:PlugClean to remove the downloaded files.
It’s that simple!
The very first thing you want to do is to put the following line into your configuration file:
With this on, everytime you open a filetype that support syntax highlight, Vim will color it for you! (thanks Vim!!).
You can change colorschemes by using the
:colorscheme command, for example:
Are you unsure about a binding? use
Do you still want to use your mouse every now and then? There’s an option for this!
set mouse=a, it will enable you to click anywhere in the buffer, and select text/move the cursor to a position.
Do you want to use the system keyboard with Vim?
Do you want something less “far away” from the home row to switch from INSERT to NORMAL mode?
map jj <ESC>
Organize your configuration files! if you use Neovim like I do, you can separate your
init.viminto multiple subfiles (keys.vim, plugins.vim …), it will make it way easier to quickly edit those.
Unbind your arrow keys by putting this in your configuration file (believe me it’s for you own good):
" Unmapping arrows nmap <Up> <Nop> nmap <Down> <Nop> nmap <Left> <Nop> nmap <Right> <Nop>
Remap window movements with the
<leader>key! my bindings look like this:
" mapping ALT + hjkl for vim window switch nmap <silent> <leader>k :wincmd k<CR> nmap <silent> <leader>j :wincmd j<CR> nmap <silent> <leader>h :wincmd h<CR> nmap <silent> <leader>l :wincmd l<CR> " mapping super + shift + hjkl for vim window move map <silent> <leader>L <C-w>L map <silent> <leader>K <C-w>K map <silent> <leader>H <C-w>H map <silent> <leader>J <C-w>J
- You can find my init.vim here, please keep in mind that it’s only a matter of preference!
Good luck! :q
And that’s it! I’ve never experienced such sudden interest for a tool than when I discovered Vim, and if I did my job right, you too should be scrolling through documentation and top plugins lists to create your own working environment, and as the years pass you will only get better at it :)
Other good ways to learn Vim
- Vimtutor, probably the best way to get started with it, a very hands-on approach with numerous examples.
I will probably make a follow-up article on the plugins I use, how I use them and why i use them.
Take care, and stay safe!