I wanted to make a first post about how to set up Jekyll, but it turns out that doing that without talking about Git is hard... and I am not yet ready to blog about git.

So, I hope that the following is useful.

I started using vim in Uni, having previously been happy with IDEs specialised to different (fairly specific) use cases and had avoided (and frankly, not even heard of) the popular 'hackers editors': emacs, vim and gedit (I'm almost kidding, but I do actually like Gedit). I'm so glad that I invested some time into learning vim as I think it's saved me a lot more, and also feels nicer to use that the other editors that I have experience with.

Plugin managers

First up, I find that having a plugin manager saves a tonne of time. Between Plug, Vundle, Pathogen and dein there's a lot of very good options, but personally I recommend Plug. I think it's the simplest and love that it runs asynchronously.

Colours || Colors

I find colour helps me to identify when I've messed up some syntax, misspelled a keyword or otherwise made a mistake.

For colour schemes I have to recommend badwolf which plays nicely with all my syntax highlighting plugins and Nvim a Vim fork that I really like. Here's how to install it:

Plugin
call plug#begin('~/.local/share/nvim/plugged') " Plugins go here
Plug 'sjl/badwolf'
call plug#end()

colorscheme badwolf

And then run :PlugInstall.

I'd also recommend that you use the following if you don't already:

syntax enable
set background=dark

This will turn on syntax highlighting and use a dark background (which I think has been minimizing the degradation of my eyesight).

Vim Tweaks

Have you ever edited a file, closed vim and then realised that the last thing you did wasn't so perfect, only to be unable to undo it easily? Version control helps, but isn't fine grained enough for my liking.

Fortunately, vim has a solution.

set undofile undodir=~/.config/nvim/undo-dir

undofile is the option to tell vim to store the edit history of the files you're working on. This means that when you start vim again, it'll load up all the recent changes you've made and let you undo them. This can be a super power when you add simnalamburt's Vim-mundo, a tool for showing your edit history as a tree, which makes it a lot easier to find a previous state of a file. The undodir is the file that it stores the history in, which you can put anywhere but I keep in my nvim config directory.

Write as sudo

Sometimes I edit a file only to discover that it's read only. I could quit, reopen the file with visudo or sudoedit but this is easier. p.s. You should probably be careful with sudo vim.

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

This maps :w!! to write via a program called tee which lets you more safely write to files as root.

I believe this tip may have been from this post by jovica but honestly I have no idea if this is where I found out about it.

Some plugins

tmhedberg's Matchit

This improves vim 'find next matching bracket' functionality (by default mapped to '%'). Normally, it can match brackets in C like languages but the plugin expands it to be able to find matching tags in more complex formats like html and vim.

tpope's vim-enuch

This adds a bunch of great commands to do 'unixy' things like moving, renaming and deleting files. Seriously it's amazing and includes some nice file search tools. Tpope has a bunch of other great plugins too and is the maintainer of pathogen. He even wrote a plugin for writing plugins... .

roxma's vim-window-resize-easy

Beautiful in it's relative simplicity, this makes vim splits easy to manage without resorting to dragging their edges around.

junegunn's fzf

Fzf is amazing. It is a fuzzy finder that makes finding files in large projects or just on your computer much easier. I highly recommend you install it for your terminal, editor and really anything you can get to support it. Installation was actually a little finicky for me but here's the vim config needed for Plug.

Plug 'junegunn/fzf', { 'dir': '~/.fzf', 'do': './install --all' }
Plug 'junegunn/fzf.vim'

AndrewRadev's switch

Provides :Switch which lets you switch statements like 'true' to 'false'. It supports a bunch more common replacements which will save you rewriting things (and more importantly, typos) and is even configurable. Here's my config for it.

Plug 'AndrewRadev/switch.vim'

...

let g:switch_custom_definitions =
    \ [
    \   ['0', '1'],
    \   ['[', ']', '(', ')', '{', '}'],
    \   ['TRUE', 'FALSE'],
    \   ['T', 'F'],
    \   ['.cc', '.h'],
    \ ]

nnoremap <leader>! :Switch<CR>

A final tip: Line 'shoving'

"Shove down: <A-j>
nnoremap ∆ :m.+1<CR>==
nnoremap <A-j> :m .+1<CR>==
inoremap <A-j> <Esc>:m .+1<CR>==gi
"Shove up: <A-k>
nnoremap ˚ :m .-2<CR>==
nnoremap <A-k> :m .-2<CR>==
inoremap <A-k> <Esc>:m .-2<CR>==gi

This maps Alt+j and Alt+k to swapping the current line with the line above or below it (the cursor moves with it). Useful for moving around debugging statements, re-ordering lists etc. The ∆ and ˚ are there because my Mac keyboard uses alt for inputting special characters and I like being able to write ∂x, but I also want the mapping consistent between my computers.


There we go. I have a fair bit more vim config, some of it I don't use and should clean up but mostly it's pretty helpful stuff that I think makes my life better.

Here's my current config, please don't copy paste without learning about what each part does (it'll make life harder when something goes wrong and you might miss out on some of the nice bits).

init.vim

Note: I haven't made any effort to make it vim compatible, or even particularly backwards compatible for older versions of neovim. Use with care.