A Better Way to Organize Your .vimrcPublished:
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 pieces
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. The
$HOME/.vim/init directory is not used by vim so I keep my files there. (You can use the
$HOME/.vim/autoload/ directory bit I have trouble with it)
" $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. VimPlug, Vundle, and NeoBundle 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 VimPlug.
" $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
$HOME/.vim/init/leader.vimrc file I keep all my leader key shortcuts and settings. The order doesn’t matter so functions and plugin settings can be set even before they exist (functions.vimrc loads after)
" $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.
" $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.