" Sane vim defaults for ArchLabs " Arch defaults runtime! archlinux.vim " additional settings set modeline " enable vim modelines set hlsearch " highlight search items set incsearch " searches are performed as you type set clipboard^=unnamedplus " system clipboard (requires +clipboard) set number " enable line numbers set confirm " ask confirmation for some things, like save before quit, etc. set wildmenu " Tab completion menu when using command mode set expandtab " Tab key inserts spaces not tabs set softtabstop=4 " spaces to enter for each tab set shiftwidth=4 " amount of spaces for indentation set shortmess+=aAcIws " Hide or shorten certain messages let g:netrw_altv = 1 let g:netrw_liststyle = 3 let g:netrw_browse_split = 3 " ------ leader mapping ------ let g:mapleader = ',' map , " ------ enable additional features ------ " enable mouse set mouse=a if has('mouse_sgr') " sgr mouse is better but not every term supports it set ttymouse=sgr endif " syntax highlighting syntax enable if has('termguicolors') && $DISPLAY !=? '' && &t_Co == 256 set termguicolors " true colors in the terminal try let g:jinx_colors = 'night' " night or day, 'T' to change on the fly colorscheme jinx catch colorscheme evening endtry " fix true colors in some terminals (neovim doesn't seem to have this issue) if !has('nvim') && !($TERM =~? 'xterm' || &term =~? 'xterm') let $TERM = 'xterm-256color' let &term = 'xterm-256color' endif else try colorscheme jinx catch colorscheme evening endtry endif " change cursor shape for different editing modes, neovim does this by default if !has('nvim') if exists('$TMUX') let &t_SI = "\Ptmux;\\e[5 q\\\" let &t_SR = "\Ptmux;\\e[4 q\\\" let &t_EI = "\Ptmux;\\e[2 q\\\" else let &t_SI = "\e[6 q" let &t_SR = "\e[4 q" let &t_EI = "\e[2 q" endif endif set linebreak " wrap lines at full words rather than the last character to fit set breakindent " wrapped lines match the same indent level set list listchars=tab:>>,trail:~ if has('multi_byte') && $DISPLAY !=? '' set listchars=tab:»»,trail:• set fillchars=vert:┃ showbreak=↪ endif " ------ commands ------ command! D Explore command! R call ranger() command! Q call quitbuffer() command! -nargs=1 B :call bufferselect("") command! W execute 'silent w !sudo tee % >/dev/null' | edit! " ------ basic maps ------ " open ranger as a file chooser using the function below nnoremap r :call ranger() " match string to switch buffer nnoremap b :let b:buf = input('Match: ')call bufferselect(b:buf) " paste while in insert mode inoremap :set pastea+:set nopastea " change windows with ctrl+(hjkl) nnoremap nnoremap nnoremap nnoremap " alt defaults nnoremap 0 ^ nnoremap Y y$ nnoremap n nzzzv nnoremap N Nzzzv nnoremap ==j " re-visual text after changing indent vnoremap > >gv vnoremap < j v:count ? 'j' : 'gj' nnoremap k v:count ? 'k' : 'gk' " open a terminal in $PWD nnoremap tt :terminal " tab control nnoremap :tabmove -1 nnoremap :tabmove +1 nnoremap te :tabnew nnoremap tn :tabnext nnoremap tf :tabfirst nnoremap tp :tabprevious " close current buffer and/or tab nnoremap q :B:silent tabclosegT nnoremap tl :execute "tabn ".g:lasttab " open a new tab in the current directory with netrw nnoremap - :tabedit =expand("%:p:h") " split the window vertically and horizontally nnoremap _ s nnoremap v " ------ autocmd ------ let g:lasttab = 1 augroup save_last_tab autocmd! autocmd TabLeave * let g:lasttab = tabpagenr() augroup END " Reload changes if file changed outside of vim requires autoread augroup load_changed_file autocmd! autocmd FocusGained,BufEnter * if mode() !=? 'c' | checktime | endif autocmd FileChangedShellPost * echo "Changes loaded from source file" augroup END " when quitting a file, save the cursor position augroup save_cursor_position autocmd! autocmd BufReadPost * call setpos(".", getpos("'\"")) augroup END " when not running in a console or a terminal that doesn't support 256 colors " enable cursorline in the currently active window and disable it in inactive ones if $DISPLAY !=? '' && &t_Co == 256 augroup cursorline autocmd! autocmd VimEnter,WinEnter,BufWinEnter * setlocal cursorline autocmd WinLeave * setlocal nocursorline augroup END endif " ------ adv maps ------ " toggle line numbers, nn (no number) nnoremap nn \ :if &number \ set nonumber \ echo 'Line numbers disabled' \ else \ set number \ echo 'Line numbers enabled' \ endif " strip trailing whitespace, ss (strip space) nnoremap ss \ :let b:_p = getpos(".") \ let b:_s = (@/ != '') ? @/ : '' \ %s/\s\+$//e \ let @/ = b:_s \ nohlsearch \ unlet b:_s \ call setpos('.', b:_p) \ unlet b:_p " global replace vnoremap sw "hy \ :let b:sub = input('global replacement: ') \ if b:sub !=? '' \ let b:rep = substitute(getreg('h'), '/', '\\/', 'g') \ execute '%s/'.b:rep."/".b:sub.'/g' \ unlet b:sub b:rep \ endif nnoremap sw \ :let b:sub = input('global replacement: ') \ if b:sub !=? '' \ execute "%s//".b:sub.'/g' \ unlet b:sub \ endif " prompt before each replace vnoremap cw "hy \ :let b:sub = input('interactive replacement: ') \ if b:sub !=? '' \ let b:rep = substitute(getreg('h'), '/', '\\/', 'g') \ execute '%s/'.b:rep.'/'.b:sub.'/gc' \ unlet b:sub b:rep \ endif nnoremap cw \ :let b:sub = input('interactive replacement: ') \ if b:sub !=? '' \ execute "%s//".b:sub.'/gc' \ unlet b:sub \ endif " highlight long lines, ll (long lines) let w:longlines = matchadd('ColorColumn', '\%'.&textwidth.'v', &textwidth) nnoremap ll \ :if exists('w:longlines') \ silent! call matchdelete(w:longlines) \ echo 'Long line highlighting disabled' \ unlet w:longlines \ elseif &textwidth > 0 \ let w:longlines = matchadd('ColorColumn', '\%'.&textwidth.'v', &textwidth) \ echo 'Long line highlighting enabled' \ else \ let w:longlines = matchadd('ColorColumn', '\%80v', 81) \ echo 'Long line highlighting enabled' \ endif " local keyword jump nnoremap fw \ [I:let b:jump = input('Go To: ') \ if b:jump !=? '' \ execute "normal! ".b:jump."[\t" \ unlet b:jump \ endif " quit the current buffer and switch to the next " without this vim will leave you on an empty buffer after quiting the current function! quitbuffer() abort let l:bf = bufnr('%') let l:pb = bufnr('#') if buflisted(l:pb) buffer # else bnext endif if bufnr('%') == l:bf new endif if buflisted(l:bf) execute('bdelete! ' . l:bf) endif endfunction " switch active buffer based on pattern matching " if more than one match is found then list the matches to choose from function! bufferselect(pattern) abort let l:bufcount = bufnr('$') let l:currbufnr = 1 let l:nummatches = 0 let l:matchingbufnr = 0 " walk the buffer count while l:currbufnr <= l:bufcount if (bufexists(l:currbufnr)) let l:currbufname = bufname(l:currbufnr) if (match(l:currbufname, a:pattern) > -1) echo l:currbufnr.': '.bufname(l:currbufnr) let l:nummatches += 1 let l:matchingbufnr = l:currbufnr endif endif let l:currbufnr += 1 endwhile " only one match if (l:nummatches == 1) execute ':buffer '.l:matchingbufnr elseif (l:nummatches > 1) " more than one match let l:desiredbufnr = input('Enter buffer number: ') if (strlen(l:desiredbufnr) != 0) execute ':buffer '.l:desiredbufnr endif else echoerr 'No matching buffers' endif endfunction " open ranger as a file chooser function! ranger() let l:temp = tempname() execute 'silent !xterm -e ranger --choosefiles='.shellescape(l:temp).' $PWD' if !filereadable(temp) redraw! return endif let l:names = readfile(l:temp) if empty(l:names) redraw! return endif execute 'edit '.fnameescape(l:names[0]) for l:name in l:names[1:] execute 'argadd '.fnameescape(l:name) endfor redraw! endfunction