A Better Way to Organize Your .vimrcPublished: - Last Modified:
Vim is a powerful customizable editor. Configuration is stored in custom text files often found in a Unix home directory (ie ~/.vimrc). This vimrc file can get to be rather large the longer one uses vim.
Break the file into parts
The number of files you use is infinite but it works best to keep things simple and organized. I use five files: plugin loader, general settings, leader key settings, custom functions, and plugin specific settings. Source each of these in your main vimrc
$HOME/.vim/init directory is not used by vim so I keep my files there.
" $HOME/.vimrc source $HOME/.vim/init/plug.vimrc " plugin loader source $HOME/.vim/init/general.vimrc " general settings source $HOME/.vim/init/leader.vimrc " leader key settings source $HOME/.vim/init/functions.vimrc " custom functions source $HOME/.vim/init/plugins.vimrc " plugin specific settings
Modern vim has many ways to load plugins. Vundle, vim-plug, and dein.vim use a list of plugins in a vimrc file to install plugins or packages. Create a single file that has the commands for the plugin manager. I use vim-plug so my file looks like this.
" $HOME/.vim/init/plug.vimrc call plug#begin('~/.vim/plugged') Plug 'tpope/vim-fugitive' Plug 'chriskempson/vim-tomorrow-theme' Plug 'vim-airline/vim-airline' Plug 'vim-airline/vim-airline-themes' call plug#end()
Make sure this file is sourced first. It will load all your plugins.
General settings are loaded in the general.vimrc file. This is were I store my tab settings, color schemes and other general settings. It works the same as any other vimrc file
" $HOME/.vim/init/general.vimrc set nocompatible filetype plugin on syntax enable set backspace=2 " Backspace behaves like other programs do set hidden " Undo persists even when switching to different open buffers " colors colorscheme Tomorrow-Night " searching set incsearch " search as characters are entered set hlsearch " hightlight matches set ignorecase " be smart about case in search set wildmenu " visual autocomplete for command menu
Leader key settings
I have a specific file to keep all my leader key shortcuts and settings
$HOME/.vim/init/leader.vimrc. ProTip: Setting functions to shortcuts before they are defined is fine because the function will not run until you input the shortcut.
" $HOME/.vim/init/leader.vimrc let mapleader="," " set the leader key to the comma key nnoremap <leader>h :noh<CR> " toggle search highlighting map <leader>vd :call ToggleThemeMode('dark')<CR> map <leader>va :call ToggleThemeMode('')<CR>
All my custom functions are added to the
" $HOME/.vim/init/functions.vimrc " Toggles the theme from light to dark function! ToggleThemeMode(style) if a:style == "dark" set background=dark AirlineTheme tomorrow colorscheme Tomorrow-Night else set background=light colorscheme Tomorrow AirlineTheme tomorrow en endfunction
Plugin specific settings
$HOME/.vim/init/plugins includes plugin specific settings. It helps to keep them separated from the general settings. It also helps to section this file by plugin so you can see all settings for a specific plugin together.
" $HOME/.vim/init/plugins.vimrc let g:airline_theme='tomorrow' let g:airline#extensions#tabline#enabled = 1 " Enable the list of buffers
These are only some of my settings. You can see all of them in my dotfiles repo.
There it is. I a better system to keep your vimrc configuration under control. Ever since I started using this system it has been much easier to manage plugins, settings, and functions. Each file has a purpose so I know where to look if something is acting up.