Skip to content

Instantly share code, notes, and snippets.

@avesus
Last active June 19, 2022 04:09
Show Gist options
  • Save avesus/1954d9384d86cc1e39cb2b2eff7017b7 to your computer and use it in GitHub Desktop.
Save avesus/1954d9384d86cc1e39cb2b2eff7017b7 to your computer and use it in GitHub Desktop.
Vim with NERDTree Adequate Defaults
# I love super fast keyboard. Most of my friends and colleagues can't follow
# I use `atkbd.softrepeat=1` on the kernel command line.
# Even Visual Assist plugin in Visual Studio doubles keyboard repeat rate with _a reason_.
# I'm working on my laptop without X installed to avoid procrastination.
# I've spend a working day googling how to make `kbdrate` using slower delay than 250.
# Add this to your /etc/profile.d/kbdrate.sh: sudo kbdrate -r 82 -d 150 if you want it in console.
# Note that it will force you type password twice. I didn't find any workarounds.
xset r rate 150 82
# When exiting from Vim, just type
# v filename
# and Ctrl+k after Vim shows up
# to open that file from any directory or default location
v() {
vim_id=`jobs|sed -n "/vim/s/\[\([0-9]\)\]+.*/\1/p"`
vim_pid=`jobs -p $vim_id`
file_name=$@
file_path=`realpath $file_name`
echo "e $file_path"
if [ -n "$vim_id" ]; then
echo "e $file_path" > $HOME/.vim-swap-$vim_pid && fg $vim_id
else
vim $@
fi
}
" Enables Pathogen
execute pathogen#infect()
" Preserve scroll position when switching between buffers
au BufLeave * if !&diff | let b:winview = winsaveview() | endif
au BufEnter * if exists('b:winview') && !&diff | call winrestview(b:winview) | unlet! b:winview | endif
" Switch between NERDTree and opened file
:nmap \e :wincmd w<CR>
" Create new file in the same folder where current edited file is located
:nmap <C-N> :wincmd w<CR>ma
" Delete current file
:nmap <C-D> :wincmd w<CR>md
" Switch between opened windows
:nmap <Tab> :bp<CR>
" Close current buffer
:nmap <expr> \q len(filter(range(1, bufnr('$')), 'buflisted(v:val)')) == 1 ? ':qa<CR>' : ':bp<CR>:bd #<CR>'
" Prevent Tab on NERDTree (breaks everything otherwise)
autocmd FileType nerdtree noremap <buffer> <Tab> <nop>
" Restore cursor position
au BufReadPost *
\ if line("'\"") > 0 && line("'\"") <= line("$") && &filetype != "gitcommit" |
\ execute("normal `\"") |
\ endif
" calls NERDTreeFind iff NERDTree is active, current window contains a modifiable file, and we're not in vimdiff
function! s:syncTree()
let s:curwnum = winnr()
NERDTreeFind
exec s:curwnum . "wincmd w"
endfunction
function! s:syncTreeIf()
if (winnr("$") > 1)
call s:syncTree()
endif
endfunction
" Shows NERDTree on start and synchronizes the tree with opened file when switching between opened windows
autocmd BufEnter * call s:syncTreeIf()
" Automatically close vim if only NERDTree left
autocmd bufenter * if (winnr("$") == 1 && exists("b:NERDTree") && b:NERDTree.isTabTree()) | q | endif
" Focus on opened view after starting (instead of NERDTree)
autocmd VimEnter * call s:syncTree()
au VimEnter * :wincmd w
" Auto refresh NERDTree files
autocmd CursorHold,CursorHoldI * if (winnr("$") > 1) | call NERDTreeFocus() | call g:NERDTree.ForCurrentTab().getRoot().refresh() | call g:NERDTree.ForCurrentTab().render() | wincmd w | endif
" Show/Hide NERDTree
:nmap <expr> \a (winnr("$") == 1) ? ':NERDTreeFind<CR>' : ':wincmd o<CR>'
" Prevent this command activation in NERDTree
autocmd FileType nerdtree noremap <buffer> \a <nop>
" Load file specified in bash after switching to background using Ctrl+Z
" Works in conjuction with .bashrc script:
" v() {
" vim_id=`jobs|sed -n "/vim/s/\[\([0-9]\)\]+.*/\1/p"`
" vim_pid=`jobs -p $vim_id`
" file_name=$@
" file_path=`realpath $file_name`
" echo "e $file_path"
" if [ -n "$vim_id" ]; then
" echo "e $file_path" > $HOME/.vim-swap-$vim_pid && fg $vim_id
" else
" vim $@
" fi
" }
function! s:loadCtrlZFile()
let cs = $HOME . "/.vim-swap-" . getpid()
let quotedcs = "\"" . cs . "\""
if filereadable(cs)
exec 'source ' . cs
exec 'call delete(' . quotedcs . ')'
endif
endfunction
:nmap <C-k> :call <SID>loadCtrlZFile()<CR>
@eddiecooro
Copy link

The config for syncing the nerdtree works well in most of the cases but it messed up everything when I used coc.nvim go to references functionality. using BufRead event in place of the BufEnter fixed the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment