Skip to content

Instantly share code, notes, and snippets.

@sminez
Created October 21, 2016 15:00
Show Gist options
  • Save sminez/458257b09f67d9b2787df9e59028d1fe to your computer and use it in GitHub Desktop.
Save sminez/458257b09f67d9b2787df9e59028d1fe to your computer and use it in GitHub Desktop.
My Current .vimrc
"============================================================================
" .: I.D.A-M .vimrc :.
" --------------------
" I've tried to keep this in some sort of sane order with similar functionality
" being grouped together and a _lot_ of comments to explain what everything
" does!
" I'm also using my .vimrc as a place to keep notes on useful tips and tricks
" as and when I find them. (http://www.vimbits.com & http://vimawesome.com ftw!)
"
" NOTE: I am using NeoVim on my main machine but I am trying to keep this
" compatible with normal Vim as well.
"=============================================================================
" .: Notes, Tips & Tricks :.
" --------------------------
" :.! {shell command}
" Run a shell command and dumps the output to the current buffer
" :w !sudo tee %
" Allows you to save files that require root permissions
" :earlier [n]m
" Revert the buffer back that many minutes (if it hasn't been closed!)
" :later [n]m
" This will revert back to the current state
" :g/{string}/y A
" Yank all lines containing [string] into the a/@a register
" gf
" On a file name in Normal mode will try to open the file...!
" <C-r>=
" In Insert mode this will open the expression register. Type in a
" mathematical expression, hit enter and it gets pasted in!
"=============================================================================
" .: Global Settings :.
" ---------------------
filetype indent on " Indent settings based on file language
set clipboard+=unnamedplus " Always copy and paste to the sytem clipboard
set splitright splitbelow " Open new splits on the right and below current
let mapleader=" " " Leader is spacebar
set ttimeoutlen=0 " These two stop vim waiting for ages with multi
set ttimeout " character keymaps
set nosmarttab " Stop tabs being insane without warning!
set softtabstop=4 " Number of spaces inserted on tab key press
set shiftwidth=4 " Number of spaces to indent by in Normal mode
set laststatus=2 " Always show last command
set lazyredraw " Don't redraw screen in macros / bound
set noshowmode " mode already shown in airline
set autoindent " Indent when moving to the next line
set tabstop=4 " Set real tab characters to have a visual size of 4
set expandtab " Always insert spaces instead of tabs
set showmatch " Show matching partner for ({[]})
set incsearch " Search as you type when using /
set hlsearch " Auto highlight search term matches
set wildmenu " Command menu
set showcmd " Show the last command in the lower right
set mouse=a " scroll and select with the mouse
set number " Show line numbers
set ruler " show current column
set rnu " use relative line numbers
if !has('nvim')
" Not required for NeoVim but definately want when using Vim!
set encoding=utf-8 " always use utf-8
set nocompatible " use vim defaults instead of vi
endif
"=============================================================================
" .: Key bindings - Plugin specific bindings are with config below :.
" -------------------------------------------------------------------
" Smash escape to return to Normal mode
inoremap fd <esc>
vnoremap fd <esc>
nnoremap fd <esc>
" Make Y behave like other capitalsa: yank from cursor to EOL
map Y y$
" Leader alternatives for Visual mode
nnoremap <Leader>vv V
nnoremap <Leader>vc <C-v>
" Force saving for files that require sudo
" NOTE: This is a command mapping -> `:w!!`
cmap w!! %!sudo tee > /dev/null %
" Move vertically by *visual* lines not source lines
nnoremap j gj
nnoremap k gk
" Delete current buffer
nnoremap <Leader>bd :bd<CR>
" Cycle through the currently open buffers
nnoremap bn :bn<CR>
nnoremap bN :bp<CR>
" Cycle through the currently open tabs
nnoremap gt :tabn<CR>
nnoremap gT :tabp<CR>
" Open a new tab
nnoremap <Leader>n :tabnew<CR>
" Quick forced save
nmap <leader>fs :w!<CR>
" Quickly close the current window & Vim if this is the last window
nmap <leader>q :q<CR>
" Toggle search highlighting (defaults to on following a search)
nnoremap <leader>h :set hlsearch! hlsearch?<CR>
" Insert blank lines without needing insert mode
nmap t o<ESC>k
nmap T O<ESC>j
" Split right
nnoremap <Leader>w/ :vsp<CR>
" Split below
nnoremap <Leader>w- :sp<CR>
" Move between splits with Alt-hjkl
nnoremap <A-h> <C-w>h
nnoremap <A-j> <C-w>j
nnoremap <A-k> <C-w>k
nnoremap <A-l> <C-w>l
" Reselect visual blocks when (un)indenting
vnoremap < <gv
vnoremap > >gv
" Open .vimrc in a new tab
nnoremap <Leader>R :tabnew $MYVIMRC<CR>
" .: NeoVim Terminal mode - :terminal :.
if has('nvim')
" Retain split navigation when in a NeoVim terminal buffer
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
endif
"=============================================================================
" .: VimPlug - Plugin Manager :.
" ------------------------------
" To use VimPlug you need to place the plug.vim file in the .vim/autoload
" directory. The following curl command will help:
" curl -fLo ~/.vim/autoload/plug.vim --create-dirs \
" https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
function! DoRemote(arg)
UpdateRemotePlugins
endfunction
call plug#begin('~/.vim/plugged')
" .: Language / Mode :.
Plug 'davidhalter/jedi-vim' " Python completion
Plug 'fatih/vim-go', {'for': 'go'} " Golang mode for go files
Plug 'gonzaloserrano/vim-markdown-todo' " Add todo boxes to markdown files
" .: Utility :.
Plug 'junegunn/fzf' " Fuzzy completion framework
Plug 'junegunn/fzf.vim' " ...that annoyingly comes in two parts!
Plug 'ervandew/supertab' " Tab completion
Plug 'tpope/vim-fugitive' " Improved git interaction
Plug 'ctrlpvim/ctrlp.vim' " !Because fzf cant cd to parent dirs
Plug 'scrooloose/syntastic' " Syntax checking
Plug 'airblade/vim-gitgutter' " Show file diffs as you edit
" .: Additional editing commands :.
Plug 'tpope/vim-surround' " quick surround with parens and quotes
Plug 'tpope/vim-commentary' " Quick comment/uncomment
" .: UI :.
Plug 'morhetz/gruvbox' " Theme
Plug 'junegunn/goyo.vim' " Distraction free mode
Plug 'lilydjwg/colorizer' " colour preview for #rrggbb
Plug 'junegunn/seoul256.vim' " Alternate theme
Plug 'junegunn/limelight.vim' " Focus on current paragraph (used in goyo)
Plug 'vim-airline/vim-airline' " Status bar with lots of integrations
Plug 'dhruvasagar/vim-table-mode' " Quick creation and layout for tables
Plug 'vim-airline/vim-airline-themes' " Themes for airline
Plug 'junegunn/rainbow_parentheses.vim' " Colour matching parens
" .: Only works with NeoVim :.
if has('nvim')
" NeoVim specific completion framework
Plug 'Shougo/deoplete.nvim', { 'do': function('DoRemote') }
endif
call plug#end()
"=============================================================================
" .: Plugin Setup :.
" ------------------
" .: Language / Mode :.
" >>> Jedi - Python mode
let g:jedi#goto_command = "<leader>d"
let g:jedi#goto_assignments_command = "<leader>g"
let g:jedi#goto_definitions_command = ""
let g:jedi#documentation_command = "K"
let g:jedi#usages_command = "<leader>u"
let g:jedi#completions_command = "<C-Space>"
let g:jedi#rename_command = "<leader>r"
let g:jedi#use_splits_not_buffers = "right"
let g:jedi#force_py_version = 3
let python_highlight_all = 1
" ----------------------------------------------------------------------------
" >>> Vim-go
let g:go_fmt_command = "goimports"
let g:go_fmt_fail_silently = 0
let g:go_highlight_functions = 1
let g:go_highlight_methods = 1
let g:go_highlight_structs = 1
let g:go_highlight_interfaces = 1
let g:go_highlight_operators = 1
" These bindings will only run on .go files
au! FileType go nmap <Leader>i <Plug>(go-info)
au! FileType go nmap <Leader>s <Plug>(go-implements)
au! FileType go nmap <Leader>e <Plug>(go-rename)
" ----------------------------------------------------------------------------
" >>> Vim-markdown
let g:markdown_fenced_languages = ['html', 'python',
\'bash=sh', 'javascript', 'sql', 'lisp']
" Abbreviation to insert a new coloured todo
ia :~ - [ ]
" ----------------------------------------------------------------------------
" .: Utility :.
" >>> Fzf
" Jump to the existing window if the file is already open
let g:fzf_buffers_jump = 1
" All work Files: Change to a directory of your choosing or add more
nnoremap <Leader>fo :Files ~/Work/<CR>
" Files in current git repo
nnoremap <Leader>fp :GFiles<CR>
" Files in current directory
nnoremap <Leader>ff :Files<CR>
" Recently edited files
nnoremap <Leader>fh :History<CR>
" Currently open buffers
nnoremap <Leader>fb :Buffers<CR>
" Run Ag in the current directory
nnoremap <Leader>a :Ag<CR>
" Search lines in current buffer
nnoremap <Leader>bl :BLines<CR>
" ----------------------------------------------------------------------------
" >>> Deoplete - autocompletion framework
let g:deoplete#enable_at_startup = 1
" ----------------------------------------------------------------------------
" >>> Syntastic - linting
nnoremap <Leader>sf :SyntasticCheck flake8<CR>
nnoremap <Leader>se :Errors<CR>
nnoremap <Leader>sc :lclose<CR>
let g:syntastic_python_python_exec = '/usr/bin/python3'
let g:syntastic_always_populate_loc_list = 1
let g:syntastic_auto_loc_list = 0
let g:syntastic_auto_jump = 0
let g:syntastic_check_on_wq = 0
let g:syntastic_loc_list_height=5
" ----------------------------------------------------------------------------
" .: Additional editing commands :.
" >>> Commentary
" Toggle commenting out the current line
nnoremap <Leader>; :Commentary<CR>
vnoremap <Leader>; :Commentary<CR>
" ----------------------------------------------------------------------------
" >>> Surround
" cs<a><b> --> change surrounding <a>s for <b>s
" ds<a> --> delete surrounding <a>s
" ysiw<a> --> surround current word with <a>s
" yss<a> --> surround the current line with <a>s
" ----------------------------------------------------------------------------
" .: UI :.
" >>> Vim-airline
if !exists('g:airline_symbols') " Powerline fonts seem to
let g:airline_symbols = {} " mess up without this...
endif
let g:airline_symbols.space = "\ua0"
let g:airline_powerline_fonts = 1
let g:airline#extensions#tabline#enabled = 1
let g:airline#extensions#tabline#show_buffers = 1
" ----------------------------------------------------------------------------
" >>> Table Mode
" <Leader>tm --> enable/disables table mode
" <Leader>tr --> realign cells after an edit
" <Leader>ts --> sort the table by the current column
" <Leader>tt --> attempt to convert selection to a table
" <Leader>tdd --> delete a row
" <Leader>tdc --> delete a column
" <Leade>rtfa --> add a formula to the current cell
" <Leader>tfe --> evaluate all formulae for the current table
" [| ]| {| }| --> move one cell left, right, up, down (wraps)
" i| a| --> text objects for in and around a cell
" ----------------------------------------------------------------------------
" >>> Rainbow Parentheses
au! VimEnter * RainbowParentheses
" ----------------------------------------------------------------------------
" >>> Goyo
nnoremap <C-g> :Goyo<CR>
let g:goyo_width = 120
let g:goyo_linenr = 0
" ----------------------------------------------------------------------------
" >>> Limelight
" Toggle limelight mode
nnoremap <Leader>l :Limelight!! 0.5<CR>
let g:limelight_conceal_ctermfg = 'DarkGray'
" Auto enable for Goyo mode
autocmd! User GoyoEnter Limelight
autocmd! User GoyoLeave Limelight! | hi Normal ctermbg=none
"=============================================================================
" .: Set colour-scheme and other UI related things :.
" ---------------------------------------------------
colorscheme gruvbox
let g:gruvbox_italic=1
let g:gruvbox_contrast_dark='medium'
set background=dark
filetype plugin on
syntax enable
let g:airline_theme='tomorrow'
hi Normal ctermbg=none
hi Comment ctermfg=8 ctermbg=none
"=============================================================================
" .: Auto-Commands :.
" -------------------
" Reload vimrc when it's saved
if has('nvim')
autocmd! BufWritePost ~/.vim/init.vim source ~/.vim/init.vim
else
autocmd! BufWritePost .vimrc source ~/.vimrc
endif
" Change the working directory to git root or file directory on buffer load
function! SetProjectRoot()
" Default to the current file's directory
lcd %:p:h
let git_dir = system("git rev-parse --show-toplevel")
" See if the command output starts with 'fatal' (if it does, not in a git repo)
let is_not_git_dir = matchstr(git_dir, '^fatal:.*')
" If git project, change local directory to git project root
if empty(is_not_git_dir)
lcd `=git_dir`
endif
endfunction
autocmd! BufRead,BufEnter,BufCreate * call SetProjectRoot()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment