Vim Essential Setup to boost your productivity

vim

Vim is an immensely popular and well aged text editor. Initially released in 1991 by Bram Moolenaar, it still rocks in Linux world. Being a very lightweight and fast, the popularity of vim lies in its keyboard efficiency. Many modern IDEs offer Vim keybindings these days along with emacs binding. Vim can be customized by key mappings. It also provides interface to extend functionalities using vimscript scripting language. Many many Vim plugins are available out there to boost your productivity. We can turn Vim into full fledged IDE using these plugins. But adding too many plugins can make it slow.

In this article we will see some basic configurations to increase your efficiency with Vim without making it slow.

Vimrc

Before moving to configuration, lets understand vimrc. rc files are common in unix world where startup information for particular command is stored. As history goes, nobody knows real meaning of rc , but it could be one of the following.

  • run configuration
  • run commands
  • run control

This is how we get different rc files in in Linux home directory e.g. bashrc. These files are generally hidden (means name starts with dot .). Vim also gives configuration capabilities using rc file called vimrc. During startup, vim reads this file and execute commands mentioned there.
All the configuration, we are mentioning below, will be added in this vimrc file. Final configuration file is available at end of article. Now lets create .vimrc
file in your home directory.

Very Basic Config

Add below lines in .vimrc.

set number
set smarttab
set smartcase
set ignorecase
set hlsearch

Lets see meaning of each.
set number – Show line numbers
set smarttab – To understand smarttab, first understand shiftwidth. When you hit <ENTER> key and go to new line, the cursor position is automatically indented to shiftwidth value, saving lot of hassle for you. smarttab uses this shiftwidth value to jump forward after pressing <TAB>, making life easy.
set ignorecase – Ignore case when searching
set smartcase – If search term has upper case, search will be case-sensitive.
set hlsearch – highlight all matching search patterns altogether, instead showing one by one.

Configuration based on filetypes (Optional)

We can tune Vim based on filetypes. Suppose you opened python file (with py extension), tabshift and shiftwidth values can be setup for 4-space indentation. While for ruby files (with extension rb), we can set indent of 2 spaces. See below examples.

filetype plugin indent on
autocmd Filetype ruby setlocal ts=2 sw=2 expandtab
autocmd Filetype python setlocal ts=4 sw=4 expandtab

We already saw use of shiftwidth. Here we specifying separate shiftwidth value based on filetype. expandtab will convert tabs into spaces. This way you can setup custom shiftwidth and tab/space preferences for different filetypes.

Sync with system clipboard

When you yank/delete (copy/cut) something inside Vim, Vim stores content in its own register called unnamed. So this data is not available outside of vim to paste. Vice versa is also true. If you copy/cut outside of Vim, you can’t paste it using p(put/paste) command inside Vim. To fix this, add below line in vimrc.

set clipboard=unnamedplus

Easy tab manipulation

Once you mastered Vim navigation, life just becomes breeze. We can set similar shortcuts to work with tabs. Add below lines in your vimrc.

nnoremap th :tabfirst
nnoremap tj :tabnext
nnoremap tk :tabprev
nnoremap tl :tablast
nnoremap tt :tabedit
nnoremap tm :tabm
nnoremap td :tabclose

These lines are self-explanatory.
tabfirst – Press th, for visiting first tab.
tabnext – Press tj, to jump on next tab.
tabprev – Press tk, to jump on previous tab.
tablast – Press tl, for visiting last tab.
tabedit – Press tt, to open new tab
tabm – Press tm, to move tabs.
tabclose – Press td, to close tab.

Auto save

Auto save is the essential feature for any editor today. We don’t want to keep ‘:w‘ing all the time. For this we will use vim-auto-save plugin. Before using this plugin, we will setup runtimepath or packpath manager called pathogen. runtimepath is path where vim looks for scripts and plugins. Pathogen helps us to maintain plugins and scripts in clean way. First setup pathogen, using below commands.

mkdir -p ~/.vim/autoload ~/.vim/bundle && \
curl -LSso ~/.vim/autoload/pathogen.vim https://tpo.pe/pathogen.vim

Now in your vimrc, add this line at very top.

execute pathogen#infect()

We all set. Lets install vim-auto-save plugin using below commands.

mkdir -p ~/.vim/bundle && cd ~/.vim/bundle && \
git clone https://github.com/vim-scripts/vim-auto-save.git

We need to tune this plugin for our use. Add below lines in your vimrc.

let g:auto_save = 1
let g:auto_save_in_insert_mode = 0

let g:auto_save = 1 – this line will enable auto-save on startup.
let g:auto_save_in_insert_mode = 0 – this line will make sure to not save in insert mode. Because this gives bad experience when typing.

Advanced keybindings

Lets add few more advanced keybindings to save a day.

Map semicolon to colon saving <SHIFT>

To enter in command line mode, we have to press colon(:). But pressing colon, takes 2 keys with <SHIFT>. We can avoid this by mapping semicolon to colon. So without pressing <SHIFT> but only semicolon, we can enter in command line mode. Add below line in your vimrc, if you want this functionality.

nnoremap ; :

Write read-only files with sudo

Many times we open read-only files without sudo and try to save some changes. As file is read-only, this fails and we end up opening file again with sudo writing all again. Below hack can save us from such situations. Add this line in your vimrc.

cmap w!! w !sudo tee > /dev/null %

So moving forward if you open read-only file and want to save some content, press w then ! in command line mode. This will ask for password and will write changes successfully.

TL;DR

Our final vimrc is below.

set nocompatible
execute pathogen#infect()

" basic config
set number
set smarttab
set ignorecase
set smartcase
set hlsearch

" map semicolon to colon
nnoremap ; :

" write with sudo
cmap w!! w !sudo tee > /dev/null %

" sync with system buffer
set clipboard=unnamedplus

" filetype specific config
filetype plugin indent on
autocmd Filetype ruby setlocal ts=2 sw=2 expandtab "tabstop, shiftwidth
autocmd Filetype python setlocal ts=4 sw=4 expandtab 

" tab navigation 
nnoremap th :tabfirst
nnoremap tj :tabnext
nnoremap tk :tabprev
nnoremap tl :tablast
nnoremap tt :tabedit
nnoremap tm :tabm
nnoremap td :tabclose

" vim-auto-save plugin
let g:auto_save = 1
let g:auto_save_in_insert_mode = 0

Setup pathogen using below commands.

mkdir -p ~/.vim/autoload ~/.vim/bundle && \
curl -LSso ~/.vim/autoload/pathogen.vim https://tpo.pe/pathogen.vim

And finally setup vim-auto-save, using below commands

mkdir -p ~/.vim/bundle && cd ~/.vim/bundle && \
git clone https://github.com/vim-scripts/vim-auto-save.git

We hope you like this article. Don’t forget to add your little Vim-hacks in comments.

Leave a Reply