Skip to content

Instantly share code, notes, and snippets.

@avesus avesus/.bashrc
Last active Oct 18, 2019

Embed
What would you like to do?
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>
@avesus

This comment has been minimized.

Copy link
Owner Author

avesus commented Aug 4, 2017

Note that it breaks opening a directory in tree view.

For those who wants to try alternative approach,
can start managing jobs in your console.

Remember command Ctrl + Z, to return just type fg.
You can run a lot of processes this way. Just type jobs to get numbers, and fg 1 / fg 2 etc. to get back.

@avesus

This comment has been minimized.

Copy link
Owner Author

avesus commented Aug 4, 2017

With Ctrl + z it seems that you don't need NERDTree too much.
Interesting is how to send a file as a buffer from command line into already opened vim:
https://stackoverflow.com/questions/5999837/how-to-open-another-file-in-background-vim-from-bash-command-line

https://serverfault.com/questions/188936/writing-to-stdin-of-background-process

@alphaCTzo7G

This comment has been minimized.

Copy link

alphaCTzo7G commented Nov 21, 2017

Very helpful.. thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.