Clean vimrc with plugin files

If you start to work in Vim, you start to edit your .vimrc file. Or, in the case of a Neovim user you might be editing your init.vim file. Either way it can quickly grow unwieldy. By unwieldy, I mean a bunch of lines of Vim script that don't necessarily connect contextually with the lines around them. If this doesn't bother you, you can stop reading now. Come back to this article when it does.

An Example

I just started using Neovim on my computer and I've been configuring it the way I want it. It hasn't grown very large, so it is still a good example for a blog post.

filetype plugin indent on
syntax on
color industry
set nu
set encoding=utf-8 "need for YCM
set splitright
set tabstop=2 shiftwidth=2 expandtab
set statusline=2
let mapleader = ","

" Window navigation with Alt
inoremap <A-h> <C-\><C-N><C-w>h
inoremap <A-j> <C-\><C-N><C-w>j
inoremap <A-k> <C-\><C-N><C-w>k
inoremap <A-l> <C-\><C-N><C-w>l
nnoremap <A-h> <C-w>h
nnoremap <A-j> <C-w>j
nnoremap <A-k> <C-w>k
nnoremap <A-l> <C-w>l
" Terminal Mode mappings
tnoremap <F12> <C-\><C-n>
tnoremap <A-h> <C-\><C-N><C-w>h
tnoremap <A-j> <C-\><C-N><C-w>j
tnoremap <A-k> <C-\><C-N><C-w>k
tnoremap <A-l> <C-\><C-N><C-w>l
tnoremap <expr> <C-R> '<C-\><C-N>"'.nr2char(getchar()).'pi'

let g:python_host_prog='C:\Python27\python'
let g:python3_host_prog='C:\Python37\python'

noremap <leader>emu :!START /MIN E:\AppData\AndroidSDK\emulator\emulator -avd Pixel_2_API_27<cr>

function! Writing()
  setlocal nonumber " no linenumbers
  setlocal guioptions-=r
  setlocal guioptions-=L
  setlocal wrap
  setlocal linebreak
  setlocal display+=lastline
  setlocal spell

  " Work with wrapped lines
  nnoremap <buffer> j gj
  nnoremap <buffer> k gk
  nnoremap <buffer> 0 g0
  nnoremap <buffer> $ g$
endfunction

let wiki_1 = {}
let wiki_1.ext = ".md"
let wiki_1.syntax = "markdown"
let g:vimwiki_list = [wiki_1]

" Move this to the appropriate ftplugin
augroup RunWritingProgramForWritingFiles
  autocmd!
  autocmd BufNewFile,BufRead {*.txt,*.markdown} call Writing()
augroup END

What's in there right now?

  • Basic Vim configurations to make my workspace feel the way I like
  • A commented section that deals with window navigation
  • A few lines related to development. Only 1 I'm sure I need
  • A function called Writing
  • Configuration for the vimwiki plug-in
  • An auto command block that uses the Writing function
    • And notice it has a comment about ftplugin

So how should we approach cleanup of this file? You could start by moving the Writing function and the auto command block next to each other. That might be enough for a file this small, but let's go further.

We have 3 (probably 4) sections that could be pulled out into their own files

  • Window Navigation: move to winnav.vim
  • Writing: move to writing.vim
    • Include the auto command block
  • Wiki configuration: move to wiki_setup.vim

If you've moved the sections then restarted your (Neo)Vim session you've suddenly lost your configurations. What to do? What to do?

What to do Pooh

(Don't) Use the source Luke

You could add source lines to your .vimrc file so it looks like this

filetype plugin indent on
syntax on
color industry
set nu
set encoding=utf-8 "need for YCM
set splitright
set tabstop=2 shiftwidth=2 expandtab
set statusline=2
let mapleader = ","

let g:python_host_prog='C:\Python27\python'
let g:python3_host_prog='C:\Python37\python'

noremap <leader>emu :!START /MIN E:\AppData\AndroidSDK\emulator\emulator -avd Pixel_2_API_27<cr>

source winnav.vim
source writing.vim
source wiki_setup.vim

That would be fine. It does have the benefit of giving you easy breadcrumbs to follow. We could do that. Fun tip, you could change source to runtime if you want. However, it still seems a little messy and it isn't the point of this article anyway ;)

Plug it in, plug it in

There are lots of special folder names in Vim configurations. I hope to get to others in future articles. For now, let us discuss the plugin folder. More information can be found from :help standard-plugin, but basically this folder is where you put stuff you want automatically loaded on startup. These are the simplest plug-ins and that is what we are doing. Creating simple plug-ins.

A possible downside you might see to this (other than the afore mentioned lack of breadcrumbs) is turning it off if you don't want it. This is actually easier now. Before, these multi-line sections would require a surrounding if block or commenting out of each line. You can still comment out out each line as you'd like but you could also just put the command finish at the top of the file. This also makes it easy to wrap control commands to stop it from getting loaded in certain situations.

Conclusion

There are lots of ways to organize your files and you need to find what works for you. If you like putting files in the smallest contextual units, this is a pretty good way to do so without cluttering up your configurations.

This is what our .vimrc file looks like now.

filetype plugin indent on 
syntax on 
color industry 
set nu 
set encoding=utf-8 "need for YCM 
set splitright 
set tabstop=2 shiftwidth=2 expandtab 
set statusline=2 
let mapleader = ","

let g:python_host_prog='C:\Python27\python'
let g:python3_host_prog='C:\Python37\python'

noremap <leader>emu :!START /MIN E:\AppData\AndroidSDK\emulator\emulator -avd Pixel_2_API_27<cr>

We could probably move those last 3 lines out too.

We didn't get into a full plug-in with more idiosyncrasies. Hopefully this gets you part of the way there if you are interested.


This article written in Neovim with the vimwiki plug-in

Previous
Previous

Minecraft + Francophile = Outrageous Accents

Next
Next

Vim + Kotlin + Android App