Skip to main content

Configuring VIM

Let's configure VIM

In this post, we will look at the basics for configuring Vim. Vim has some reasonable defaults, but it is malleable, so you can shape it to your needs. Master craftsmen all have a favorite tool which is custom-made and tailored to their needs. That is what Vim is for writers and programmers. 

Let's get down to business. Locate your configuration file.

  • If you're using Vim
    • ~/.vimrc in your home directory is the file you will use to configure Vim. 
  • If you're using Neovim
    • ~/.config/nvim/init.vim in your home directory is the file you will use to configure Neovim. 

The standard workflow is to edit the vimrc/init file, exit Vim, and launch it again so the configuration take effects. (Optionally, you could source the file in command mode.)

If vimrc is open in your terminal, just enter the following command: so %. "so" is the shorter form for "source" and "%" refers to current buffer.  This configuration is the same for both Vim and Neovim.

In order to provide some clarity, I'll separate the configuration into three (3) sections. 

  • Options
  • Plugins
  • Configurations and shortcuts

 

Options

Vim has a lot of internal variables and switches which you can use to achieve some special effects. You can set an option with the "set" keyword. While all the options can be enabled in command line mode, you also can write them once in the vimrc file. 

These are the options which make my life a little easier with Vim. So here are all the options. 

set noswapfile
set noerrorbells
set encoding=utf-8
set number
set relativenumber
set hidden
  • noswapfile: The swap files are a little annoying! Vim really wants to protect your work and creates a .swp file for your file. If the buffer is dirty - for example, if your computer crashes in the middle of a Vim session, - you'll get a pesky message the next time you launch Vim asking if you want to restore the original file from the .swp. I trust my gut and don't use this feature. 
  • noerrorbells: I dislike error bells with a passion. Set this option and you'll have one less distraction in your life!
  • encoding=utf-8: I always want UTF-8 encoding for all my files. 
  • number: Enables line numbering for the buffer. 
  • relativenumber: Enables relative line numbering
  • hidden: Allows you to have buffers with unsaved changes when opening a new file. Normally under default settings this won't work and you will get a message "E37: No write since last change(add ! to override)"

 

Plugins

Plugins give Vim some super powers by extending it. Most of the time, the purpose is to emulate IDE type behavior. All Vim plugins are hosted on Github. I recommend using a plugin manager called vim-plug. Each time you update plugins, the latest fixes go straight to your Vim. So install vim-plug to follow this section.

Here are the plugins that I use in my vimrc. 

""" Vim-Plug
call plug#begin('~/config/plugged')
Plug 'majutsushi/tagbar'
Plug 'scrooloose/nerdtree'
" Fuzzy finder. Use Ripgrep with <leader>rg
Plug 'junegunn/fzf', { 'do': { -> fzf#install() } }
Plug 'junegunn/fzf.vim'
" Very simple status line.Does not make vim slow :)
Plug 'itchyny/lightline.vim'
" Show most recently opened files. No mapping yet. Open with :Mru
Plug 'lvht/mru'
Plug 'vim-test/vim-test'
call plug#end()
  • tagbar: If you develop code in Vim and want a nice outline of all your methods, use the tagbar plugin. 
  • nerdtree: nerdtree gives you tree navigation of the directory structure. (Optional: You can use Vim's default "netrw," which works equally well. netrw is built in, so it doesn't need a plugin.) 
  • fzf: fzf is the plugin that will change your workflow. In combination with ripgrep it gets a lot of super powers.
  • lightline: The status line that shows the Vim mode, file name, encoding and various other things. It's fast and does not slow your editor.
  • mru: "Most recently used." Use this plugin to if you want Vim to remember and show your most recently used files use this plugin.
  • vimtest: Use this plugin to run tests. It works with most popular languages and frameworks.   
  • telescope: If you are Neovim user, I recommend using the telescope plugin. This is by far the most awesome plugin. 
  • git: For git, I recommend vim-fugitive. 
  • debugging: Yes, you can debug with Vim too! Use the vim-spector plugin for this. In my opinion, this helps you to understand legacy code bases with bad unit test coverage. You won't need debugging as much otherwise. 

Syntax highlighting: By default, Vim has bare minimum syntax highlighting and you can do with command "syntax on" in your vimrc. However you can install the plugin GruvBox. For Neovim, hands down use tree-sitter

Code auto completion: This is a complex topic and there are various solutions based on the language. 

If you use Neovim 0.5+ you are in luck and have support for almost all popular languages using built in LSP(language server protocol). The LSP was originally built by Microsoft. 

For Java I would recommend using CoC. If you use Vim CoC is what you need. 

Configurations 

I've never met a Vimer who doesn't love to configure their editor. This is where the Vim help system comes to rescue. Mostly users just copy configurations from the internet without knowing what a setting does. Vim help system comes to the rescue! Just type "help" in command mode followed by a keyword. The keyword can be a Vim option or Vim keyword, or some Vim variable. 

:help nmap or :h nmap

There are two (2) broad classifications of mappings

  • map: This is the recursive kind
  • noremap: This is the non recursive kind.
:map j gg           (moves cursor to first line)
:map Q j            (moves cursor to first line)
:noremap W j        (moves cursor down one line)

In the example above, "j" is mapped to "gg" and "Q" to "j." Executing "Q" will go to first line. However executing "W" will go to one line below. 

Depending on the mode there are various mappings. 

  • nmap: mapping in normal mode
  • vmap: mapping in visual mode
  • imap: mapping in insert mode. 
There are many others type :h map-modes to learn about them 

For brevity, let's focus on nmap

Let's toggle a tagbar in normal mode. You an map it to w. \ is the leader key by default in Vim. 

nmap <leader>w :TagBarToggle 

Let's say you want to map a command to open init file. 

nmap <leader>i :e ~/.vimrc<CR> 

So nmap is just mapping LHS to RHS. 

Conclusion

Today we learned a lot about how you can configure Vim. Use the Vim help system to explore more and configure. Give a try and please let me know what your favorite plugins and configurations are. Thank you for reading, and happy configuring! 


Comments

Popular posts from this blog

Command line tools for a happy life

Hello Readers! I'm back and better than ever with another article about developer productivity. I love the Terminal. After all that's where everything comes to an end.  Or begins. (Only nerds get that joke.)  Today I'm going to discuss four (yes, just 4 ) cool *nix tools , utilities that help you get things done faster once you integrate them into your workflow. tmux tree find ripgrep   tmux tmux is a multiplexor. Do you log in to remote servers using ssh clients, like ssh and/or putty? Do you have to spawn multiple of those just to get more done? Does that slow you down? Of course it does. Well, look no further.  Spawn one terminal and invoke the tmux . That's all you need! Spawn it with: $tmux  Note:  tmux may not be available by default. To install it, use your favorite package manager. This screen cast demos how to use tmux.    Tmux, click for demo tree tree provides a nice layout of the directory structure ...

Chat GPT and our(dev) future

 There's a lot of hype and anxiety over large language models like Chat GPT. Companies are scrambling to come up with their next AI offering as soon as possible to counter GPT. I've been thinking about how it will affect us as engineers and our company in the near future and in the longer term. Please read what follows with a grain of salt, as this is my opinion only. What is GPT capable of? GPT is an intelligent chat bot backed by a language model which can answer your questions contextually and make it appear creative.  It allows you to do your job faster.  Will GPT help us get to MVP faster? Maybe. It can write code but only in snippets. You're still responsible for gluing the code together.  Today it’s a fast stack overflow.    Will GPT replace you?  Certainly not in the near term. Software engineering is about communication. A stakeholder won’t be able to communicate with GPT and get an MVP.  As an engineer, you...