Skip to content

Instantly share code, notes, and snippets.

@tastywheat
Last active June 3, 2024 23:32
Show Gist options
  • Save tastywheat/64caa3d5c4a98057623e5373a638ca35 to your computer and use it in GitHub Desktop.
Save tastywheat/64caa3d5c4a98057623e5373a638ca35 to your computer and use it in GitHub Desktop.
vimrc
filetype off
syntax on
" Vundle Install
" launch vim, run :PluginInstall
"
" set the runtime path to include Vundle and initialize
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
Plugin 'christoomey/vim-tmux-navigator'
Plugin 'vim-airline/vim-airline'
Plugin 'vim-airline/vim-airline-themes'
Plugin 'preservim/nerdtree'
Plugin 'ctrlpvim/ctrlp.vim'
call vundle#end()
" Plug (https://github.com/junegunn/vim-plug)
" Install
" curl -fLo ~/.vim/autoload/plug.vim --create-dirs https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
" :PlugInstall
call plug#begin()
Plug 'neoclide/coc.nvim', {'branch': 'release'} " requires nodejs
call plug#end()
" CoC extensions -- extensions will install on opening vim
" https://github.com/neoclide/coc.nvim#example-vim-configuration
" Create ~/.vim/coc-settings.json
" for better performance use { "codeLens.enable": false, "tsserver.log": "off", "tsserver.trace.server": "off" }
let g:coc_global_extensions = ['coc-tsserver']
" CoC
" GoTo code navigation.
nmap <silent> gd <Plug>(coc-definition)
nmap <silent> gy <Plug>(coc-type-definition)
nmap <silent> gi <Plug>(coc-implementation)
nmap <silent> gr <Plug>(coc-references)
" Use K to show documentation in preview window
nnoremap <silent> K :call <SID>show_documentation()<CR>
function! s:show_documentation()
if (index(['vim','help'], &filetype) >= 0)
execute 'h '.expand('<cword>')
else
call CocAction('doHover')
endif
endfunction
" Having longer updatetime (default is 4000 ms = 4s) leads to noticeable
" delays and poor user experience
set updatetime=300
filetype plugin indent on
" Airline
" extensions cause poor performance
let g:airline_extensions = []
set laststatus=2
" enable nerd tree relative line numbers
let NERDTreeShowLineNumbers=1
" show dot files/hidden files
let NERDTreeShowHidden=1
" set default sidebar size
let NERDTreeWinSize=60
" CtrlP to look at vim's working directory instead of git root
let g:ctrlp_working_path_mode = ''
let g:ctrlp_show_hidden=1
" Leader
let mapleader="\<Space>"
" requires: brew update; brew install vim;
set clipboard=unnamed
set runtimepath^=~/.vim/bundle/ctrlp.vim
" Ignore these directories
" https://stackoverflow.com/questions/47492658/ignore-custom-directories-in-ctrlp-vim
let g:ctrlp_custom_ignore = {
\'dir': '\.git\|dist\|build\|node_modules\|vendor\|deps$'
\}
set wildignore+=**/node_modules/**
set wildignore+=*/build/**
set wildignore+=*/bower_components/**
set wildignore+=*/vendor/**
set wildignore+=*/deps/**
set wildignore+=*/_build/**
set background=dark
set t_Co=256
" use if you want light color scheme
" set termguicolors
" settings to make colors work https://github.com/vim/vim/issues/993
let &t_8f = "\<Esc>[38;2;%lu;%lu;%lum"
let &t_8b = "\<Esc>[48;2;%lu;%lu;%lum"
" Solarized
" set background=light
" let g:solarized_termcolors=256
" colorscheme solarized
" Width of a tab is 4
set tabstop=4
" Indents width of 4
set shiftwidth=4
" Number of columns for a tab
set softtabstop=4
" Expand tabs to spaces
set expandtab
set nocompatible " use vim defaults
set scrolloff=3 " keep 3 lines when scrolling
set ai " set auto-indenting on for programming
" set path+=**
set wildmenu " visual auto cmplete for command menu
set wildmode=list:full
set lazyredraw " redraw only when we need to
set showcmd " display incomplete commands
set nobackup " do not keep a backup file
set number " show line numbers
set ruler " show the current row and column
set hlsearch " highlight searches
set incsearch " do incremental searching
set showmatch " jump to matches when entering regexp
set ignorecase " ignore case when searching
set smartcase " no ignorecase if Uppercase char present
set relativenumber
set visualbell t_vb= " turn off error beep/flash
set novisualbell " turn off visual bell
set backspace=indent,eol,start " make that backspace key work the way it should
" set runtimepath=$VIMRUNTIME " turn off user scripts, https://github.com/igrigorik/vimgolf/issues/129
set t_RV= " http://bugs.debian.org/608242, http://groups.google.com/group/vim_dev/browse_thread/thread/9770ea844cec3282
" Autocomplete
set omnifunc=syntaxcomplete#Complete
" fix weird typescript file performance issues
" 0 = let vim decide, 1 = legacy, 2 = modern
set re=0
" Customizations
" open new tab
nnoremap <Leader>t :tabnew<CR>
" toggle nerd tree directory
nnoremap <Leader>n :NERDTreeToggle<CR>
" open to current file
nnoremap <Leader>m :NERDTreeFind<CR>
" move to end of line
nnoremap <leader>l $
" move to start of line
nnoremap <leader>h 0
" copy selection to clipboard
vnoremap <leader>c "*y
" paste selection from clipboard
nnoremap <leader>v "*P
" escape and save
inoremap jj <esc>:w<cr>
" escape
inoremap jk <esc>
" Find from the current directory
nnoremap <leader>F :find <C-R>=getcwd().'/**/*'<CR>
" fold block
nnoremap <leader>fo v$%:fold<cr>
" ; to :
nnoremap ; :
" quick window crush and swipe
nnoremap <leader>z <c-w>_<c-w><bar>
" quick window restore
nnoremap <leader>b <c-w>=
" Prevent entering ex mode
nnoremap Q <nop>
" Write session and exit
nnoremap <leader>qq :wa<cr> :mksession! ./.vim_session.log<cr> :wqa<cr>
" Load session
nnoremap <leader>qw :source ./.vim_session.log <cr>
" console.log word under cursor
nnoremap <leader>cjs yiwoconsole.log('<C-r>"', JSON.stringify(<C-r>"));<esc>
" console.log word under cursor
nnoremap <leader>cl yiwoconsole.log('<C-r>"', <C-r>");<esc>
" console.log 'here'
nnoremap <leader>cc oconsole.log('here');<esc>
" reload vimrc
nnoremap <leader>sv :source $HOME/.vimrc<cr>
" edit vimrc
nnoremap <leader>ev :vsplit $MYVIMRC<cr>
" replace hovered word with yank
nnoremap <leader>rw "_diwP
" select previous visual selection
nnoremap <expr> gp '`[' . strpart(getregtype(), 0, 1) . '`]'
" restart node
nnoremap <leader>rs :call SendTmuxKeys("bottom-left", RunStart())<cr>
" run test
nnoremap <leader>rt :call SendTmuxKeys("bottom-right", RunTest())<cr>
" comment block
vnoremap <leader>/ Vc/*<cr>*/<esc>P
" grep operator
"nnoremap <leader>g :set operatorfunc=<SID>GrepOperator<cr>g@
"vnoremap <leader>g :<c-u>call <SID>GrepOperator(visualmode())<cr>
" vim search
nnoremap <expr> <leader>g ":vim " . expand("<cword>") . " "
vnoremap <leader>g y:vim '<c-r>"'<space>
" toggle quickfix
nnoremap <leader>o :call ToggleQuickFix()<cr>
" copy/paste relative line
nnoremap <expr> <leader>ab input("enter relative line", ":-") . "y<cr>p"
nnoremap <expr> <leader>af input("enter relative line", ":+") . "y<cr>p"
" snippets
" nnoremap ,fn :-1read $HOME/.vim/snippets/arrow-function.tpl<CR>o
" nnoremap ,func :-1read $HOME/.vim/snippets/function.tpl<CR>o
" nnoremap ,comp :-1read $HOME/.vim/snippets/component.tpl<CR>2jcit
" nnoremap ,redux :call GenerateSnippet("redux.tpl")<CR>
" nnoremap ,mw :-1read $HOME/.vim/snippets/middleware.tpl<CR>
" Go files, autocompelte when pressing '.'
" augroup GoAutoComplete
" autocmd!
" autocmd CompleteDone * pclose
" augroup END
" Hide cursor line in insert mode
augroup HideCursorLineInInsert
autocmd!
autocmd InsertEnter * setlocal nocursorline
autocmd InsertLeave * setlocal cursorline
augroup END
augroup CursorLineOnlyInActiveWindow
autocmd!
autocmd VimEnter,WinEnter,BufWinEnter * setlocal cursorline
autocmd WinLeave * setlocal nocursorline
augroup END
" automatically remove trailing whitespace on save (:w)
augroup AutoRemoveTrailingWhitespace
autocmd!
autocmd FileType javascript,css,scss,go,html autocmd BufWritePre <buffer> %s/\s\+$//eg
augroup END
" Functions
function! ToggleQuickFix()
if empty(filter(getwininfo(), 'v:val.quickfix'))
copen
else
cclose
endif
endfunction
function! s:GrepOperator(type)
let saved_unnamed_register = @@
if a:type ==# 'v'
normal! `<v`>y
elseif a:type ==# 'char'
normal! `[v`]y
else
return
endif
silent execute 'grep! -rnI --exclude="*.log" --exclude="*.swp" ' . shellescape(@@) . ' src app tests'
copen
redraw!
let @@ = saved_unnamed_register
endfunction
" GenerateSnippet('redux', 'thing')
function! GenerateSnippet(filename)
let key = input("enter key ")
let uckey = toupper(key[0]) . strpart(key, 1)
let cmd = 'cat ~/.vim/snippets/'.a:filename.' | sed "s/\%\%KEY\%\%/'.key.'/" | sed "s/\%\%UC_KEY\%\%/'.uckey.'/"'
" let str = system(cmd)
silent execute "-1:read!".cmd
redraw!
endfunction
function! SendTmuxKeys(pane, cmd)
" tmux send-keys -t {session}:{window}.{pane}
silent execute "!tmux send-keys -t ".a:pane." 'C-c' Enter"
silent execute "!tmux send-keys -t ".a:pane." '".a:cmd."' Enter"
redraw!
endfunction
function! RunStart()
let l:path = expand('%:p')
if l:path =~ 'contribula-core'
return 'npm run start:fast'
else
return 'npm start'
endif
endfunction
function! RunTest()
let l:path = expand('%:p')
if l:path =~ 'urban'
return 'npm run test-unit'
else
return 'npm test'
endif
endfunction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment