Making vim a *comfy* editor

manfromth3m0on

Max S-T

Posted on December 6, 2019

Making vim a *comfy* editor

At first vim can seem somewhat daunting.
Coming from more traditional editors like notepad[++], VSCode or Sublime, the jump to an editor where you can't even just start typing immediately when you open a file can seem frustrating at first.
I understand and am completely sympathetic to this but once you start using vim to its full potential its pretty hard to go back.
I think the first thing that daunts new vim users is the lack of immediate guidance, this is not just a 'feature' of vim but in a large portion of the Linux eco-system overall.
The first big lesson to learn with vim and a lot of other Linux tools is manpages and help dialogues.

:help

The :help command is one of the most useful utilities in vim, if you are ever unsure of almost anything the :help command can, in pretty much every case, save you.
At this point you don't even need to open a web browser anymore!
The structure of a help command is very simple type :help and then whatever it is you are questioning. e.g.

:help g 

Entering this will bring up a list of command that start with :g.
Now knowing this you're ready to start making vim a little more comfy

vim-plug

Back in the day, adding plgins to vim was a somewhat arduous process but now, its as simple as adding a line to your vim config and issuing a command.

So lets install vim-plug

Run this command if you are using vim

curl -fLo ~/.vim/autoload/plug.vim --create-dirs \
    https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim

Or this on if you are using neovim

curl -fLo ~/.local/share/nvim/site/autoload/plug.vim --create-dirs \
    https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim

Now enter your vim config which is either in

~/.vimrc if you use standard vim

or

~/.config/nvim/init.vim if you use neovim

Then add

call plug#begin('~/.vim/plugged')
call plug#end()

Now we can add plugins in between those two lines and run :PlugInstall to install them.

Comfy plugins

Now we need to gather a list of plugins that will will make vim comfy.
For that we need to think about some of the things that we have in other editors that make them more comfy as opposed to stock vim
So the features we want are:

  • [More and better] Code highlighting
  • A file Tree
  • Writing focus mode
  • A flashy status bar
  • Auto-closed brackets
  • A wal based colorscheme
  • Intellisense/Auto completions

The good news is there are plugins to do all of these things which are as easy as one line in a config to install.

So lets begin:

File tree

For this we are going to use a fan favorite - NERDTree.
To install just put

Plug 'scrooloose/nerdtree'

in between the vim-plug begin and end lines.
Now close and re-open your vim config and issue the :PlugInstall command
Once that is done, to open NERDTree type :NERDTreeToggle to open the tree
Thats a bit of mouthful I think you can see so I like to bind the toggle to a hotkey.
Add

map <C-f> :NERDTreeToggle<CR>

to your config and now once you reopen vim you can open the tree with Ctrl+f
If you want to change the hotkey the anatomy of the binding is <> defines a key/key combo, and anytime you see C it means control then whatever comes after is a key.
So if you wanted Ctrl-e the binding would be <C-e>

Better Code highlighting

The tool for the task in this case seem to be cim-polyglot
Which makes some very bold claims

  • It won't affect your startup time, as scripts are loaded only on demand.
  • It installs and updates 120+ times faster than the 146 packages it consists of.
  • Solid syntax and indentation support (other features skipped). Only the best language packs.
  • All unnecessary files are ignored (like enormous documentation from php support).
  • No support for esoteric languages, only most popular ones (modern too, like slim).
  • Each build is tested by automated vimrunner script on CI. See spec directory.

As well as a very impressive list of supported languages (which can be found here)
And even better, the install is another one line in you config and a :PlugInstall
Just put

Plug 'sheerun/vim-polyglot'

In your Plug section and you're done

Writing focus mode

A great plugin for this is goyo.vim
And as always the install is super simple

  • vim-plug
    1. Add Plug 'junegunn/goyo.vim' to .vimrc
    2. Run :PlugInstall

Goyo makes non-coding in vim great.
It removes all the extraneos stuff from your screen so if you just want to sit and write you can.
Despite all this I think having to type :Goyo each time you wanted to switch in and out of focus mode would be a pain.

For this exact reason a wrote a bind to handle it for me:

map <C-g> :Goyo<CR>

So now whenever I hit Ctrl+g I enter focus mode

Auto close brackets

This was one of the first things to get in my way when I started writing code in vim. I'd miss a close bracket and then spend 15 mins looking for the thing. (yes, I know gcc errors show line numbers. I was tired ok?)
So to alleviate the pain or going through something similar use a plugin like auto-pairs

Same drill as usual:

  1. Plug 'jiangmiao/auto-pairs'
  2. :PlugInstall

And now it just works

Status bar

For this task I like vim-airline.
Note for this bar to work you'll need to be running a powerline patched font.

As per usual its a simple install:

  1. Plugin 'vim-airline/vim-airline' Plugin 'vim-airline/vim-airline-themes'
  2. :PlugInstall

Wal based colorscheme

For those who don't know wal is a way of generating and managing colorschemes on linux.
And if your syntx-highlighting colors dont match your system colors, lets be real, what kinda human are you.
So we are going to intergrate wal colors and vim colors with

  1. Plug 'dylanaraps/wal.vim'
  2. :PlugInstall
  3. colorscheme wal

Intellisense and Auto completion

This is achivable with the coc plugin.
To install it put

Plug 'neoclide/coc.nvim', {'branch': 'release'}

In your vim config
Then add

" For COC
20 set hidden
21 set nobackup
22 set nowritebackup
23 set cmdheight=2
24 set updatetime=300
25 set shortmess+=c
26 set signcolumn=yes

as well

Bonus round: Easy tab switching

To really make vim like something like VSCode efficient use of tabs is important.
Usually tabs are controlled with g commands e.g. gt for next tab

So what this next bind does is bind Ctrl+Tab to next tab

map <C-i> gt

I thought this was pretty useful so I'd include it.

Bonus round 2: Line Numbers

Line numbers are pretty important to programming (as expressed earlier) so how do you turn them on?
Well you can either use relatiave line number (rnu) or set line numbers. RNU sets line number relative to your cursor which looks pretty cool as you scroll around a document but set are certainly more useful.
To try either out use the :set numbers or :set rnu commands.
Once you've found which you like just put that respective command in your vim config

Closing remarks

Thank you all for reading and sticking with such a long article.
I'll cya in the next one.
~ M

💖 💪 🙅 🚩
manfromth3m0on
Max S-T

Posted on December 6, 2019

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related