Developer Aspirations

YAPB - Yet Another Programming Blog

Wednesday

03

June 2015

VIMtastic - Settings

by Colin Miller

A lot of the amazing things that Vim can do do not require plugins but are built in by default. Many of these things are enabled through various settings. There are dozens of useful settings you can use in Vim. Rather than typing them in each time you start it up, you can set your common configuration in your ~/.vimrc file. Here are some of the settings I personally use along with a note of what they do. You can copy and paste the following straight into your .vimrc and edit as you'd like.

syntax enable           "enable syntax highlighting
set nocompatible        " We're in vim mode instead of vi
set exrc                " use .vimrc file per project(directory)
set secure              " disable unsafe commands in local .vimrc
set hidden              " allows us to switch buffers without saving
set nowrap              " don't wrap lines
set t_Co=256            " 256 color vim
colors inkpot           " dark color scheme
set number              " show line numbers
set colorcolumn=120     " Line to determine end of column limit
" Toggle line numbers and fold column with F2 for easy copying:
nnoremap <F2> :set nonumber!<CR>:set foldcolumn=0<CR>
" A more informative status line
set statusline=%F%m%r%h%w\ [TYPE=%Y]\ [POS=%04l,%04v][%p%%]\ [LEN=%L]
" Set characters used for borders
set fillchars=stl:=,stlnc:-,vert:\|
set tabstop=4           " a tab is 4 spaces
set shiftwidth=4        " number of spaces to use for autoindenting
set smarttab            " insert tabs on the start of a line according to shiftwidth, not tabstop
set smartindent         " use smart autoindenting at the start of a new line
set autoindent          " always set autoindenting on
set lbr                 " if wrapping a line (for display only) break between words
set backspace=indent,eol,start  " allow backspacing over everything in insert mode
set autoread            " automatically read a file again if changed outside of vim
set cindent             " use C indenting rules
set wildmode=longest    " when using <tab> autocomplete on things like edit, complete to the longest common string
syntax on               " always use syntax highlighting


set foldmethod=syntax  " automatically create folds based on syntax
set foldcolumn=4       " use 4 characters for the fold margin
set foldlevelstart=20  " automatically unfold the first 20 levels

"Auto-indent entire file with F11
nmap <F11> 1G=G
imap <F11> <ESC>1G=Ga

"Shift-J and Shift-K for paging
nmap <S-J> 20j
nmap <S-K> 20k

"Search
set incsearch           " show search matches as you type
set ignorecase          " ignore case while searching
set smartcase           " ignore case if search pattern is all lowercase, otherwise use case
set showmatch           " set show matching parenthesis
set hlsearch            " highlight search terms
" clear search highlighting by pressing ,/
nmap <silent> ,/ :nohlsearch<CR>

" radom things
set history=1000
set undolevels=1000
set wildignore=*.swp,*.class,*.o,*.a,*.so  " ignore these patterns when expanding wildcards (since they're binary files)
set visualbell          " no beeping
set noerrorbells        " seriously, no beeping

" Use sudo to save when you forgot
cmap w!! w !sudo tee % >/dev/null

" Easy window navigation
map <C-h> <C-w>h
map <C-j> <C-w>j
map <C-k> <C-w>k
map <C-l> <C-w>l

Of these I believe my favortes are using Shift-J and Shift-K for faster navigation along with saving a file as root (if you didn't start vim as root but you already edited a root-writable file) by using :w!!. Navigating between windows using ctrl-h rather than ctrl-w h is also fairly convenient.

There are a ton of additional settings for Vim that can unlock even more useful things. Take a look at the in-app documentation sometime by typing :help. For further information on any of these settings, you can use :he <setting> to bring up that settings documentation.

comments powered by Disqus