Skip to content

Instantly share code, notes, and snippets.

@srishanbhattarai
Created August 17, 2021 13:19
Show Gist options
  • Save srishanbhattarai/85cae136f485e2b1d8dab27b41fa1775 to your computer and use it in GitHub Desktop.
Save srishanbhattarai/85cae136f485e2b1d8dab27b41fa1775 to your computer and use it in GitHub Desktop.
vim
"vim:fileencoding=utf-8:ft=conf:foldmethod=marker
" General {{{
" These are general settings that work on both Vim/Neovim. They mostly relate
" to saner defaults like `hidden` buffers, `unnamed` clipboard etc. They've
" been heavily commented to make sense to the future reader (most likely me!).
set hidden "hidden buffers are allowed. This allows me to move around files without saving them.
set clipboard=unnamed "unnamed clipboard unifies the system clipboard and Vim's clipboard.
set ignorecase "ignorecases allows for case insensitive pattern matching during search
set hlsearch "hlsearch highlights search matches TODO: Search color fix
set splitbelow "splitbelow makes the default horizontal split be below the current buffer.
set splitright "splitright makes the default vertical split be to the right of the current buffer.
set noswapfile "noswapfile disables swap files entirely.
set dir=/tmp "/tmp is the first choice for a swap file container folder
set undolevels=100 "100 is the limit of undos to store for each file
set undofile "undofile combined with undodir allows for a large undo history to rewind each file
set undodir=~/.config/nvim/undo "undodir sets the path of the undo file
set noerrorbells "noerrorbells disables beeps and screen flashes for errors
set ttimeoutlen=50 "ttimeoutlen sets only 10 milliseconds to wait for a mapped sequence to complete
set foldmethod=marker
" }}}
" Neovim Specific {{{
" While I don't use Vim8, most of the config should be bidirectionally
" compatible except the stuff in here which is for Neovim specifically.
if (has('nvim'))
" Path to Python2 and Python3 binaries which are required by some plugins.
" A function that runs the shell `which` command and sanitizes the output.
function! s:which(cmd)
return substitute(system("which " . a:cmd), "\n", "", "")
endfunction
let g:python_host_prog = s:which("python2")
let g:python3_host_prog = s:which("python3")
" Normal mode in neovim's terminal
tnoremap <Leader><ESC> <C-\><C-n>
set inccommand=nosplit "Show the live results of a substituion but without opening a new buffer.
endif
" }}}
" Appearance {{{
" These have more to do with the out of the box Vim appearance settings and
" not custom plugins/appearances etc. Several of these things are interchangable with whats in the General section.
" Disable the tabline that shows up at the top (if you have a tab open)
set showtabline=0
set nolazyredraw " Make things smooth - draw stuff as you go.
set nowrap "nowrap disables long lines from wrapping around.
set smartindent "smartindent allows for saner indentation when starting a new line in C-like languages
set autoindent "autoindent automatically indents the next line based on the indentation of the previous line
set shiftwidth=2 "number of spaces for each auto-indent, also determines the number of spaces inserted when we enter a <TAB>, provided smarttab is enabled.
set number "number shows the absolute line number
set relativenumber "relativenumber shows the relative line number. If `number` is also enabled, it shows the absolute line number for the current line.
set scrolloff=3 "scrolloff is the offset number of lines to keep below the cursor when scrolling
set wildmode=longest,full "Complete longest common string then each full match in command mode
set shell=$SHELL " $SHELL is the default shell
" Tab settings - While the editorconfig config plugin is responsible for how
" stuff is formatted, these are some acceptable defaults when there isn't
" one present.
set expandtab "expandtab turns a <Tab> press into spaces
set smarttab "smarttab inserts blanks in front of a new line for each <Tab>, based on `shiftwidth`, and not `tabstop` or `softtabstop`
set tabstop=2 "tabstop is the number of spaces a <Tab> in a file counts for
set softtabstop=2 "softtabstop is the number of spaces a <Tab> creates during editing operations
set shiftround "shiftround makes tabs/spaces be a multiple of shiftwidth
" enable 24 bit color support if supported
if (has('mac') && has("termguicolors"))
set termguicolors
" this is not mac specific; maybe move it out?
let &t_8f="\<Esc>[38;2;%lu;%lu;%lum"
let &t_8b="\<Esc>[48;2;%lu;%lu;%lum"
set termguicolors
endif
" Highlights whatever you yanked for a little bit.
augroup highlight_yank
autocmd!
au TextYankPost * silent! lua vim.highlight.on_yank { higroup='IncSearch', timeout=200 }
augroup END
" }}}
" Abbreviations {{{
" The solution to dealing with typos isn't to make less of them - but have
" them autofixed by your editor.
iabbrev fucn func
iabbrev teh the
iabbrev fucntion function
iabbrev tehn then
iabbrev dont don't
" Math symbols
" Directly proportional
iabbrev mdp ∝
" Power of two
iabbrev mpow2 ²
" }}}
" General Mappings {{{
" These are my leader mappings and some (possibly bad) habits that I've gotten used to
" as I've been using Vim.
let mapleader=" " "mapleader assigns the leader key to <space>
" Use semicolon to avoid having to press Shift everytime.
nnoremap ; :
" Jumping around wrapped lines is more consistent when `gj` is `j` by default.
map j gj
map k gk
" Navigating splits
map <Leader>h <C-w>h
map <Leader>j <C-w>j
map <Leader>k <C-w>k
map <Leader>l <C-w>l
inoremap <C-c> <Esc>
" Easier search and replace
map <Leader>r :%s//<left>
" Jump to the alternate buffer
nnoremap <silent> <leader>3 :b#<CR>
" Edit the vimrc file in a new split
nnoremap <leader>ev :vsp $MYVIMRC<CR>
" Source the vimrc file
nnoremap <leader>sv :source $MYVIMRC<CR>
" }}}
" Functions {{{
" These are mappings/functions that rely on std functions or custom functions to do
" stuff.
" Clear search highlighting
noremap <silent> <leader>W :noh<cr>:call clearmatches()<cr>
" When searching for the word under cursor with asterisk (*), it's helpful
" to not move to the first match.
nnoremap <silent> * :let stay_star_view = winsaveview()<cr>*:call winrestview(stay_star_view)<cr>
" Fuzzy find the word under cursor
nnoremap <silent> <leader>f :Rg <C-R><C-W><CR>
" MacOS specific optimization to tell clipboard.vim to not search for clipboard
" provider.
if (has('mac'))
let g:clipboard = {
\ 'name': 'pbcopy',
\ 'copy': {
\ '+': 'pbcopy',
\ '*': 'pbcopy',
\ },
\ 'paste': {
\ '+': 'pbpaste',
\ '*': 'pbpaste',
\ },
\ 'cache_enabled': 0,
\ }
endif
" }}}
" Plugins {{{
call plug#begin('~/.config/nvim/plugins')
Plug 'junegunn/fzf', { 'dir': '~/.fzf', 'do': './install --all' }
Plug 'junegunn/fzf.vim'
Plug 'terryma/vim-multiple-cursors' " Select the same patterns and edit them in parallel
Plug 'matze/vim-move' " helps move regions of text around with <C-j>, <C-k>
Plug 'tpope/vim-surround' " surround region of text with parentheses
Plug 'rhysd/clever-f.vim' " subsequent 'f' after doing a 'f' or 't' navigates to next match. This is usually done with semicolon but I have another mapping for it.
Plug 'christoomey/vim-tmux-navigator'
Plug 'scrooloose/nerdtree', { 'on': ['NERDTreeToggle', 'NERDTreeFind'] }
Plug 'editorconfig/editorconfig-vim'
Plug 'google/vim-searchindex'
Plug 'srishanbhattarai/neovim-spotify', { 'do': 'bash install.sh' }
Plug 'fatih/vim-go', { 'for': 'go'}
call plug#end()
" }}}
" Autocmds {{{
augroup custom_autocmds
autocmd!
autocmd FileType go nnoremap <buffer> K :GoDef<CR>
autocmd FileType go nnoremap <buffer> <leader>t :GoCoverageToggle -short<cr>
autocmd FileType typescript nnoremap <buffer> K :ALEGoToDefinition<CR>
autocmd FileType make setlocal noexpandtab shiftwidth=8 softtabstop=0
augroup END
" }}}
" Colorscheme {{{
" Make the vertical split separator a bit more colorscheme agnostic and consistent with what tmux does.
colorscheme base16-gruvbox-dark-hard
" make the border bitween splits disappear
hi LineNr guibg=bg
set foldcolumn=2
hi foldcolumn guibg=bg
hi VertSplit guibg=bg guifg=bg
" highlight current line
set cursorline
" }}}
" EditorConfig: 'editorconfig/editorconfig-vim' {{{
let g:EditorConfig_exclude_patterns = ['fugitive://.*']
" }}}
" ProjectDrawer: 'scroolose/nerdtree' {{{
nnoremap <leader>1 :NERDTreeToggle<CR>
let NERDTreeMinimalUI=1
let NERDTreeIgnore=['\.o$', '\.gch$', '\~$']
" }}}
" vim-go: 'fatih/vim-go' {{{
let g:go_fmt_command="goimports"
let g:go_auto_type_info = 1
" }}}
" fzf {{{
let g:fzf_action = {
\ 'ctrl-t': 'tab split',
\ 'ctrl-x': 'split',
\ 'ctrl-v': 'vsplit' }
" Default fzf layout
" - down / up / left / right
let g:fzf_colors =
\ { 'fg': ['fg', 'Normal'],
\ 'bg': ['bg', 'Normal'],
\ 'hl': ['fg', 'Comment'],
\ 'fg+': ['fg', 'CursorLine', 'CursorColumn', 'Normal'],
\ 'bg+': ['bg', 'CursorLine', 'CursorColumn'],
\ 'hl+': ['fg', 'Statement'],
\ 'info': ['fg', 'PreProc'],
\ 'border': ['fg', 'Ignore'],
\ 'prompt': ['fg', 'Conditional'],
\ 'pointer': ['fg', 'Exception'],
\ 'marker': ['fg', 'Keyword'],
\ 'spinner': ['fg', 'Label'],
\ 'header': ['fg', 'Comment'] }
let g:fzf_layout = { 'window': '20split enew' }
" Customize fzf colors to match your color scheme
" Enable per-command history.
" CTRL-N and CTRL-P will be automatically bound to next-history and
" previous-history instead of down and up. If you don't like the change,
" explicitly bind the keys to down and up in your $FZF_DEFAULT_OPTS.
let g:fzf_history_dir = '~/.local/share/fzf-history'
nnoremap <silent> <C-p> :Files<CR>
nnoremap <silent> <C-b> :Buffers<CR>
command! -bang -nargs=* Rg
\ call fzf#vim#grep(
\ 'rg --column --line-number --no-heading --color=always --smart-case '.shellescape(<q-args>), 1,
\ <bang>0 ? fzf#vim#with_preview('right:50%')
\ : fzf#vim#with_preview('up:60%:hidden', '?'),
\ <bang>0)
" }}}
" vim-move {{{
" <C-j> and <C-k> move lines up and down.
let g:move_key_modifier = 'C'
" }}}
" Rust {{{
let g:racer_experimental_completer = 1
" }}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment