Skip to content

Instantly share code, notes, and snippets.

@AlexJHayward
Last active April 18, 2023 16:45
Show Gist options
  • Save AlexJHayward/54d9587460ff246194545f77460be33b to your computer and use it in GitHub Desktop.
Save AlexJHayward/54d9587460ff246194545f77460be33b to your computer and use it in GitHub Desktop.
My Vim Configuration
  1. Add the below to ~/.vimrc
  2. You might need to install elixir-ls from source to get it all working properly.
  3. Install ripgrep brew install ripgrep
  4. Add the following to your .zshrc:
if type rg &> /dev/null; then
  export FZF_DEFAULT_COMMAND='rg --files'
  export FZF_DEFAULT_OPTS='-m'
fi
  1. Make sure to add .vim-bookmarks to the global gitignore
  2. Open vim in any dir with vim
  3. Run :PlugInstall
  4. Install the REST client plugin for Coc with :CocInstall coc-restclient
" -- plugins -- 

" This will load and install vim-plug if it isn't already installed.
" Load vim-plug
if empty(glob("~/.vim/autoload/plug.vim"))
    execute '!curl -fLo ~/.vim/autoload/plug.vim https://raw.github.com/junegunn/vim-plug/master/plug.vim'
endif

" begin vim-plug section
" This section is for vim-plug managed plugins.
" :PlugInstall to install them.

call plug#begin('~/.vim/plugged')

Plug 'tpope/vim-vinegar'
Plug 'MattesGroeger/vim-bookmarks'
Plug 'neoclide/coc.nvim', {'branch': 'release'}
Plug 'elixir-lsp/coc-elixir', {'do': 'yarn install && yarn prepack'}
Plug 'junegunn/fzf', { 'dir': '~/opt/fzf' }
Plug 'junegunn/fzf.vim'
Plug 'vim-airline/vim-airline'
Plug 'vim-airline/vim-airline-themes'
Plug 'elixir-editors/vim-elixir'
Plug 'c-brenn/mix-test.vim'
Plug 'tpope/vim-dispatch'
Plug 'rafi/awesome-vim-colorschemes'
Plug 'hashivim/vim-terraform'
Plug 'zivyangll/git-blame.vim'
" Add plugins to &runtimepath
call plug#end()

" -- plugins end --

colorscheme gruvbox    
set number          " show relative line numbers
set relativenumber  " show relative line numbers
set cursorline      " highlight current line
syntax on           " syntax highlighting
set autoindent      " obvs
set wildmenu        " visual autocomplete for command menu
filetype indent on  " load filetype-specific indent files
set showmatch       " highlight matching [{()}]
set incsearch       " search as characters are entered
set hlsearch        " highlight matches
set spell           " sets spellcheck to on

" code folding
setlocal foldmethod=syntax         
setlocal foldlevelstart=99
au BufReadPost,BufNewFile */doc/*.exs setlocal foldlevel=2 " fold up tests when in an Elixir test file/script

" scratch buffer 
function! Scratch()
    split
    noswapfile hide enew
    setlocal buftype=nofile
    setlocal bufhidden=hide
    setlocal nobuflisted
    "lcd ~
    file scratch
endfunction

command! NewScratch silent! execute "call Scratch()"

" highlight the visual selection after pressing enter.
xnoremap <silent> <cr> "*y:silent! let searchTerm = '\V'.substitute(escape(@*, '\/'), "\n", '\\n', "g") <bar> let @/ = searchTerm <bar> echo '/'.@/ <bar> call histadd("search", searchTerm) <bar> set hls<cr>

" move vertically by visual line
nnoremap j gj
nnoremap k gk

" make clipboard work with the macOS clipboard
set clipboard=unnamed

" simpler split navigation
nnoremap <C-J> <C-W><C-J>
nnoremap <C-K> <C-W><C-K>
nnoremap <C-L> <C-W><C-L>
nnoremap <C-H> <C-W><C-H>

" dimming inactive split
augroup BgHighlight
    autocmd!
    autocmd WinEnter * set cul
    autocmd WinLeave * set nocul
augroup END

" buffers 
set hidden
command! CloseAllOtherBuffers silent! execute "%bd|e#|bd#"
noremap <C-Q> :bdelete<CR> 
" move backups to the tmp directory
set backup
set backupdir=~/.vim-tmp,~/.tmp,~/tmp,/var/tmp,/tmp
set backupskip=/tmp/*,/private/tmp/*
set directory=~/.vim-tmp,~/.tmp,~/tmp,/var/tmp,/tmp
set writebackup

" bookmarks
let g:bookmark_save_per_working_dir = 1
let g:bookmark_auto_save = 1

" airline settings
let g:airline_theme='deus'
let g:airline_powerline_fonts = 1
let g:airline#extensions#tabline#enabled = 1

"indentation
:set shiftwidth=2
:set autoindent
:set smartindent

" heex file highlighting
au BufRead,BufNewFile *.ex,*.exs set filetype=elixir
au BufRead,BufNewFile *.eex,*.heex,*.leex,*.sface,*.lexs set filetype=eelixir
au BufRead,BufNewFile mix.lock set filetype=elixir

" run tests with dispatch
let g:mix_test_command = "Dispatch mix test {test}"
nmap <silent> mix_test <Plug>(MixTestCurrentFile())

" coc completion settings 
" use <tab> to trigger completion and navigate to the next complete item
function! CheckBackspace() abort
  let col = col('.') - 1
  return !col || getline('.')[col - 1]  =~# '\s'
endfunction

inoremap <silent><expr> <Tab>
      \ coc#pum#visible() ? coc#pum#next(1) :
      \ CheckBackspace() ? "\<Tab>" :
      \ coc#refresh()

inoremap <expr> <cr> coc#pum#visible() ? coc#pum#confirm() : "\<CR>"
" goto definition hotkey
nmap <silent> gd <Plug>(coc-definition) 
nmap <silent> gu <Plug>(coc-references) 

" fzf
nnoremap <silent> <C-S-p> :Files<CR>
nnoremap <silent> <C-f> :Rg<CR>
nnoremap <silent> <C-t> :Buffers<CR>
" avoid fzf esc key delayshttps://www.johnhawthorn.com/2012/09/vi-escape-delays
set ttimeout
set timeoutlen=1000 ttimeoutlen=0

" mix test
nnoremap <silent> <C-a> :call MixRunCurrentTestFile()<CR>
nnoremap <silent> <C-r> :call MixRunCurrentTest()<CR>

" sessions
set viewoptions+=localoptions

" Remove newbie crutches in Command Mode
cnoremap <Down> <Nop>
cnoremap <Left> <Nop>
cnoremap <Right> <Nop>
cnoremap <Up> <Nop>

" Remove newbie crutches in Insert Mode
inoremap <Down> <Nop>
inoremap <Left> <Nop>
inoremap <Right> <Nop>
inoremap <Up> <Nop>

" Remove newbie crutches in Normal Mode
nnoremap <Down> <Nop>
nnoremap <Left> <Nop>
nnoremap <Right> <Nop>
nnoremap <Up> <Nop>

" Remove newbie crutches in Visual Mode
vnoremap <Down> <Nop>
vnoremap <Left> <Nop>
vnoremap <Right> <Nop>
vnoremap <Up> <Nop>

" git blame 
nnoremap <Leader>s :<C-u>call gitblame#echo()<CR>

" use thin curosr in insert mode
let &t_SI = "\e[6 q"
let &t_EI = "\e[2 q"

" reset the cursor on start (for older versions of vim, usually not required)
augroup myCmds
au!
autocmd VimEnter * silent !echo -ne "\e[2 q"
augroup END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment