" Basic settings ------------------------------------------------------------- set shell=/bin/bash " Set default shell set encoding=utf-8 " Set file encoding to UTF-8 set nomodeline " Disable modeline (run code on file open) set nocompatible " Reset vi compatibility presets set backspace=indent,eol,start " Enable backspace key in insert mode syntax on " Enable syntax highlighting set clipboard=unnamedplus " Use system clipboard set textwidth=79 " Column width to 79 set colorcolumn=79 " Set column to highlight for textwidth set wrap " Visual/soft line wrap set linebreak " Only insert breaks on breakat chars set nolist " Disable line break, see: set wrapmargin=0 " vim.wikia.com > tip 989 set autoindent " Auto-indent on line break set smartindent " Smart indent set shiftwidth=2 " Set indentation set tabstop=2 set softtabstop=2 set expandtab " Convert tab to spaces set shiftround " Indent in multiples of shiftwidth set cursorline " Highlight current line set title " Show file title set number " Show line numbers set ruler " Show cursor position set showbreak=↪ " Show line breaks set showmatch " Show matching opening/closing char set showcmd " Show command used (bottom bar) set showmode " Show current mode (bottom bar) set wildmenu " Enable menu for autocomplete options set wildmode=list:longest,full " List matches by longest common sections, all set wildignore=*.tmp,*~ " Exclude some filetypes from wildmenu set splitbelow " Horizontal split below set splitright " Vertical split to the right set hidden " Switch between buffers without save prompt " To autosave: :set autowrite or autowriteall set foldenable " Enable code folding set foldmethod=indent " See :h foldmethod for other options set foldlevelstart=10 " 0 = all folds closed, 99 = all folds open set lazyredraw " No redraw when running macros set cryptmethod=blowfish " Set default encryption method set history=1000 " Set no. of lines in undo history set viminfo= " Disable viminfo " To move viminfo to ~/.vim instead: " set viminfo+=n~/.vim/viminfo set noswapfile " Disable *.swp files set noerrorbells " Disable sounds set visualbell set t_vb= set incsearch " Start searching on input set nohlsearch " Disble search highlighting set ignorecase " Ignore case in search for lowercase input set smartcase " Case-sensitive search for mixed case input set spelllang=en_gb " Set spell check language set timeoutlen=2000 " Set timeout (ms) for key mappings set autochdir " Change into a file's directory on open colorscheme gruvbox " See :color for options set background=dark " Set light/dark bg for themes that use it " Highlight trailing whitespace " https://vi.stackexchange.com/q/8563 highlight ExtraWhitespace guibg=#870000 ctermbg=088 match ExtraWhitespace /\s\+$\|\t/ augroup ExtraWhitespace au! au BufWinEnter * match ExtraWhitespace /\s\+$/ au InsertEnter * match ExtraWhitespace /\s\+\%#\@ key let mapleader=',' " Move to beginning/end of line nmap b 0 nmap e $ " Move to prev/next visual row nmap j gj nmap k gk " Keep indent block selected " https://github.com/bling/dotvim vmap < >gv " Remap ctrl+d to toggle between shell and vim nmap :sh " Disable ctrl+z to avoid accidentally stopping vim nmap " Move page down/up nmap nmap " Buffer navigation — go to N, prev, next, left, right nmap bg :ls:b nmap bh :bprev nmap bl :bnext nmap [ h nmap ] l " Buffer width resize — decrease, increase nmap b- :vertical res -5 nmap b= :vertical res +5 " Buffer loading — close, new (vsplit), reload, new (same split) nmap bc :bw nmap bn :enew nmap br :e nmap bv v:enew " Backup file in current buffer nmap bk :call BackupFile() " Map delete to a black hole register (separate from cut/paste register) map d "_d " Map expression register (used to evaluate expressions) imap ee = " Code folding — collapse all, expand all, toggle current fold nmap fc zM nmap fe zR nmap ft za " Git commands nmap ga :!git add . nmap gb :!git branch -b nmap gc :!git commit -m nmap gca :!git commit nmap gco :!git checkout nmap gd :!git diff nmap gf :!git fetch nmap gg :!git grep nmap gl :!git log nmap gm :!git merge nmap gph :!git push nmap gpl :!git pull nmap gs :!git status " New markdown note nmap md :call AddNewFile('$HOME/', '', 'md') " Toggle netrw browser nmap nt :call ToggleNetrw() " Map OmniComplete imap o " Insert paste into file from cat input " https://stackoverflow.com/a/2545242 nmap pp :r! cat " Search for selection, prompt for replacement, replace all in file " https://stackoverflow.com/a/31172452 vnoremap sa "0y:%s/0//g " Prompt for search/replace text, replace all in selection vnoremap sr :s///g " Toggle spell check nmap sc :setl spell! " Search in current directory nmap sd :!grep -R " Toggle search term highlighting nmap sh :set nohlsearch! " Sessions — load (waits for file input), save nmap sl :source $HOME/.vim/sessions/ nmap ss :mksession! $HOME/.vim/sessions/ " Tab navigation — close, prev, next, new " To go directly to tab n: [n]gt nmap tc :tabc nmap th :tabp nmap tl :tabn nmap tn :tabe " Edit/refresh to apply vimrc changes nmap ve :tabe $MYVIMRC nmap vr :source $MYVIMRC " Show word count " https://unix.stackexchange.com/a/145293 nmap wc g vmap wc :s/\S\+//gn " Trim leading whitespace " https://unix.stackexchange.com/a/29619 " To reset cursor at first selected line: vmap wsl :%le vmap wl :normal 0dw " Trim trailing whitespace " http://oualline.com/vim-cook.html#trim " https://vim.fandom.com/wiki/Remove_unwanted_spaces nmap wst :1,$s/[ ]*$// vmap wst :s/\s\+$// " Netrw " ---------------------------------------------------------------------------- " Settings let g:netrw_banner = 0 " Hide info header let g:netrw_browse_split = 3 " 0: reuse window, 1: hsplit, 2: vsplit, " 3: new tab, 4: previous window let g:netrw_dirhistmax = 0 " 0: disable history/bookmarks let g:netrw_keepdir = 0 " Sync dir view and change dir paths let g:netrw_liststyle = 0 " 0: thin, 1: long, 2: wide, 3: tree let g:netrw_winsize = 25 " Set pane width " Set the default path for the scratchpad used by buffer functions " Default path: ~/.vim/scratchpad let g:scratch_dir = 'scratchpad' " Map keys within the file browser " https://vonheikemen.github.io/devlog/tools/using-netrw-vim-builtin-file-explorer/ fun! MapNetrwKeys() " Toggle hidden file visibility nmap . gh " Files — copy, delete, move, rename, select nmap fc mc nmap fd D nmap fm mm nmap fr R nmap v mf " Go back in history nmap h u " Close file preview buffer nmap P z endfun aug netrw_keymaps au! autocmd filetype netrw call MapNetrwKeys() augroup END " Toggle the file browser " https://stackoverflow.com/questions/5006950/setting-netrw-like-nerdtree fun! g:ToggleNetrw() Lexplore vertical resize 25 endfun " Functions ------------------------------------------------------------------ " Trim trailing whitespace (can be used on filetype) " e.g. au BufWrite *.* :call DeleteExtraWS() " https://amix.dk/vim/vimrc.html fun! g:DeleteExtraWS() exe 'normal mz' %s/\s\+$//ge exe 'normal `z' endfun " Create a new file fun! g:AddNewFile(path, name, ext) let date = strftime('%Y-%m-%d') let fn = '-' . a:name if a:name == '' let fn = '' endif exe 'tabe' a:path . '/' . date . fn . '.' . a:ext endfun " Backup the file in the current buffer " https://www.ibm.com/developerworks/library/l-vim-script-2/index.html fun! g:BackupFile() let b:timestamp = strftime('%Y%m%d%H%M%S') return writefile(getline(1,'$'), bufname('%') . '-' . b:timestamp) endfun " Wrap a word/selection in brackets " Based on http://learnvimscriptthehardway.stevelosh.com/chapters/09.html " and https://superuser.com/a/875160 let g:WrapWordChars = { \ '<':'>', '{':'}', '[':']', '(':')', '"':'"', "'":"'", '`':'`', \ } fun! g:WrapWord(prefix) " Temporarily disable Auto Pairs plugin if enabled if exists('b:autopairs_enabled') let l:isapenabled = b:autopairs_enabled let b:autopairs_enabled = 0 endif " Add key mappings to prefix + char for insert and select modes for c in keys(g:WrapWordChars) exe 'inoremap ' . a:prefix . c . ' viwa' . \ g:WrapWordChars[c] . 'bi' . c . 'ea' exe 'vnoremap ' . a:prefix . c . ' xi' . c . \ g:WrapWordChars[c] . 'P' endfor " Restore user settings if exists('b:autopairs_enabled') let b:autopairs_enabled = l:isapenabled endif endfun call g:WrapWord('w') " Filetypes ------------------------------------------------------------------ filetype plugin indent on " Group autocommand calls to avoid duplicates whenever writing to buffer " http://learnvimscriptthehardway.stevelosh.com/chapters/14.html " Set indentation by filetype (not set with other basic settings as they will " override filetype setl whenever vimrc is reloaded) " shiftwidth: indent/unindent width " tabstop: tab width in spaces (view) " softtabstop: tab width in spaces (edit) augroup Filetypes au! " CSS/Sass au FileType css,sass,scss setl shiftwidth=2 tabstop=2 softtabstop=2 \ omnifunc=csscomplete#CompleteCSS " Gophermap needs real to convert maps au BufRead,BufNewFile gophermap setl noexpandtab shiftwidth=4 tabstop=4 \ softtabstop=0 textwidth=70 colorcolumn=70 " HTML au Filetype htm,html setl \ foldmethod=indent indentkeys= shiftwidth=2 tabstop=2 softtabstop=2 " Markdown au FileType markdown,md,mkd setl spell shiftwidth=2 tabstop=2 softtabstop=2 " Vim-inspired app configs au BufRead,BufNewFile vifmrc set filetype=vim au BufRead,BufNewFile vimperatorrc set filetype=vim au FileType vim,vimrc,vimrc* set shiftwidth=2 tabstop=2 softtabstop=2 augroup END " Plugins -------------------------------------------------------------------- " vim-emoji-complete — insert emoji let g:emoji_complete_overwrite_standard_keymaps = 0 imap em (emoji-start-complete) " ALE — syntax check/linting " Run lint manually let g:ale_lint_on_enter = 0 let g:ale_lint_on_save = 0 let g:ale_lint_on_text_changed = 'never' nmap ld :call ale#cursor#ShowCursorDetail() nmap li :ALELint nmap lj (ale_next_wrap) nmap lk (ale_previous_wrap) " Auto Pairs — auto-close brackets let g:AutoPairs = { \ '<':'>', '{':'}', '[':']', '(':')', \ '"':'"', "'":"'", '`':'`', \ '', \ '{%':'%}', '{#':'#}', \ } " Remap/disable unneeded mappings let g:AutoPairsMapCR = 0 let g:AutoPairsMultilineClose = 0 let g:AutoPairsShortcutFastWrap = '' let g:AutoPairsShortcutJump = 'aj' let g:AutoPairsShortcutToggle = 'ap' " commentary.vim — code commenting imap / :Commentary vmap / :Commentary " snippet.vim — code/template expansion " Key mappings in snipmate.vim/after/plugin/snipMate.vim let g:snippets_dir = '~/.vim/snippets' imap . =TriggerSnippet()