Skip to content

Commit

Permalink
vim - update config
Browse files Browse the repository at this point in the history
replace nnn with netrw

make netrw more user friendly

move some custom commands to their own files
  • Loading branch information
VonHeikemen committed Jan 6, 2021
1 parent 2c35f41 commit ef4bbf3
Show file tree
Hide file tree
Showing 7 changed files with 340 additions and 203 deletions.
6 changes: 6 additions & 0 deletions .vimrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,9 @@ source ~/my-configs/vim/theme.vim
source ~/my-configs/vim/rc.vim
source ~/my-configs/vim/custom-commands.vim
source ~/my-configs/vim/snippets.vim

" Homemade plugins
source ~/my-configs/vim/mini-plugins/guess-indentation.vim
source ~/my-configs/vim/mini-plugins/better-netrw.vim
source ~/my-configs/vim/mini-plugins/project-buffers.vim

200 changes: 0 additions & 200 deletions my-configs/vim/custom-commands.vim
Original file line number Diff line number Diff line change
@@ -1,29 +1,9 @@
" Search buffers below the project root
command! FindProjectBuffers
\ call fzf#run(fzf#wrap({
\ 'source': map(ProjectBuffers(getcwd()), 's:format_buffer(v:val)'),
\ 'sink*': function('s:bufopen'),
\ 'options': ['+m', '--expect', 'ctrl-t,ctrl-v,ctrl-x']
\ }))

" Show syntax id
command! SyntaxQuery call s:syntax_query()

" Get visually selected text
command! GetSelection call s:get_selection('/')

" Override tab settings
command! ForceTab call s:force_tab()
command! -nargs=+ UseSpaces call s:use_spaces(<f-args>)
command! UseTabs call s:use_tabs()

" Guess buffer indentation
command! GuessIndent call s:vs_detect()
command! AutoGuessIndent call s:guess_indent()

" See tab related settings
command! TabStatus call s:tab_indicator()

" Toggle terminal
command! -nargs=+ OpenTerm call s:show_term(<f-args>)

Expand Down Expand Up @@ -94,31 +74,6 @@ function! s:get_selection(cmdtype) "{{{
let @s = temp
endfunction "}}}

" Return formatted buffer name
function! s:format_buffer(val)
let name = fnamemodify(a:val, ':p:~:.')
let bufnum = bufnr(a:val)
let flag = bufnr('') == bufnum ? '%'
\ : bufnr('#') == bufnum ? '#'
\ : ' '

return printf("[%d] %s\t%s", bufnum, flag, name)
endfunction

" Handle open selected file
function! s:bufopen(lines)
let cmd = get({
\ 'ctrl-x': 'split',
\ 'ctrl-v': 'vsplit',
\ 'ctrl-t': 'tabe'}, a:lines[0], 'e')

let parts = split(a:lines[1], '\]')
let buf = substitute(parts[0], '^\[\s*', '', 'g')
let name = fnamemodify(bufname(buf + 0), ':p')

exe cmd name
endfunction

" Handle terminal state
let g:_terminal_open = 0
function! s:show_term(mode) abort
Expand Down Expand Up @@ -148,15 +103,9 @@ function! s:show_term(mode) abort
else
call feedkeys("\<Esc>")
endif

endfunction

" Test floating windows
if has('nvim-0.4')
if !exists(':FZF')
source /usr/share/vim/vimfiles/plugin/fzf.vim
endif

" Using the custom window creation function
let g:fzf_layout = { 'window': 'call FloatingFZF()' }

Expand Down Expand Up @@ -188,152 +137,3 @@ if has('nvim-0.4')
endfunction
endif

function! s:tab_indicator() abort
let sw = &shiftwidth
echo 'sw=' sw 'ts=' &tabstop
endfunction

function! s:use_spaces(size) abort
call setbufvar('', '&tabstop', a:size)
call setbufvar('', '&shiftwidth', a:size)
call setbufvar('', '&softtabstop', a:size)
call setbufvar('', '&expandtab', 1)
endfunction

function! s:use_tabs() abort
call setbufvar('', '&tabstop', 4)
call setbufvar('', '&shiftwidth', 0)
call setbufvar('', '&softtabstop', 0)
call setbufvar('', '&expandtab', 0)
endfunction

function! s:guess_indent() abort
" autocmd BufAdd * call feedkeys(":GuessIndent\<CR>", 'n')
autocmd BufAdd * if expand('<afile>') ==# '' | echo "" | else | call feedkeys(":GuessIndent\<CR>", 'n')
endfunction

" taken from Tim Pope's vim-sleuth
" source: https://github.com/tpope/vim-sleuth
function! s:vs_guess(lines) abort
let options = {}
let heuristics = {'spaces': 0, 'hard': 0, 'soft': 0}
let ccomment = 0
let podcomment = 0
let triplequote = 0
let backtick = 0
let xmlcomment = 0
let softtab = repeat(' ', 8)

for line in a:lines
if !len(line) || line =~# '^\s*$'
continue
endif

if line =~# '^\s*/\*'
let ccomment = 1
endif
if ccomment
if line =~# '\*/'
let ccomment = 0
endif
continue
endif

if line =~# '^=\w'
let podcomment = 1
endif
if podcomment
if line =~# '^=\%(end\|cut\)\>'
let podcomment = 0
endif
continue
endif

if triplequote
if line =~# '^[^"]*"""[^"]*$'
let triplequote = 0
endif
continue
elseif line =~# '^[^"]*"""[^"]*$'
let triplequote = 1
endif

if backtick
if line =~# '^[^`]*`[^`]*$'
let backtick = 0
endif
continue
elseif &filetype ==# 'go' && line =~# '^[^`]*`[^`]*$'
let backtick = 1
endif

if line =~# '^\s*<\!--'
let xmlcomment = 1
endif
if xmlcomment
if line =~# '-->'
let xmlcomment = 0
endif
continue
endif

if line =~# '^\t'
let heuristics.hard += 1
elseif line =~# '^' . softtab
let heuristics.soft += 1
endif
if line =~# '^ '
let heuristics.spaces += 1
endif
let indent = len(matchstr(substitute(line, '\t', softtab, 'g'), '^ *'))
if indent > 1 && (indent < 4 || indent % 2 == 0) &&
\ get(options, 'shiftwidth', 99) > indent
let options.shiftwidth = indent
endif
endfor

if heuristics.hard && !heuristics.spaces
return {'expandtab': 0, 'shiftwidth': &tabstop}
elseif heuristics.soft != heuristics.hard
let options.expandtab = heuristics.soft > heuristics.hard
if heuristics.hard
let options.tabstop = 4
endif
endif

return options
endfunction

function! s:vs_apply(options) abort
if !has_key(a:options, 'expandtab') || !has_key(a:options, 'shiftwidth')
return 0
else
for [option, value] in items(a:options)
call setbufvar('', '&'.option, value)
endfor
return 1
endif
endfunction

function! s:vs_detect() abort
if &buftype ==# 'help' || bufname('%') ==# ''
echo ''
return
endif

let l:max = 16384 " float2nr(pow(2, 14))
let l:sample = getline(1, min([line('$'), l:max]))

let l:options = s:vs_guess(l:sample)

if s:vs_apply(l:options)
let l:msg = 'Apply settings: '
for [option, value] in items(l:options)
let l:msg .= option . ' ' . value . ' '
endfor
echo msg
else
echo 'could not guess indentation'
endif
endfunction

5 changes: 5 additions & 0 deletions my-configs/vim/init.vim
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,8 @@ source ~/my-configs/vim/rc.vim
source ~/my-configs/vim/custom-commands.vim
source ~/my-configs/vim/snippets.vim

" Homemade plugins
source ~/my-configs/vim/mini-plugins/guess-indentation.vim
source ~/my-configs/vim/mini-plugins/better-netrw.vim
source ~/my-configs/vim/mini-plugins/project-buffers.vim

120 changes: 120 additions & 0 deletions my-configs/vim/mini-plugins/better-netrw.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
if &columns < 90
" If the screen is small, occupy half
let g:netrw_winsize = 50
else
" else take 30%
let g:netrw_winsize = 30
endif

" Hide banner
let g:netrw_banner = 0

" Hide dotfiles
let g:netrw_list_hide = '\(^\|\s\s\)\zs\.\S\+'

" A better copy command
let g:netrw_localcopydircmd = 'cp -r'

" Delete a non-empty directory
function! NetrwRemoveRecursive()
if &filetype ==# 'netrw'
" Prepare the delete command.
" Make it so that is triggered by just pressing Enter
cnoremap <buffer> <CR> rm -r<CR>
" Unmark all files (don't want to delete anything by accident)
normal mu

" Mark the file/directory under the cursor
normal mf

" Show the prompt to enter the command
" In here you either press Enter to confirm
" or press ctrl + c to abort.
" Don't do anything else!
try
normal mx
catch
echo "Canceled"
endtry

" Undo the Enter keymap
cunmap <buffer> <CR>
endif
endfunction

" Better keymaps for Netrw
function! NetrwMapping()
" Close Netrw window
nmap <buffer> <leader>dd :Lexplore<CR>
nmap <buffer> <leader>da :Lexplore<CR>
" Go to file and close Netrw window
nmap <buffer> L <CR>:Lexplore<CR>
" Go back in history
nmap <buffer> H u
" Go up a directory
nmap <buffer> h -^
" Go down a directory / open file
nmap <buffer> l <CR>
" Toggle dotfiles
nmap <buffer> . gh
" Toggle the mark on a file
nmap <buffer> <TAB> mf
" Unmark all files in the buffer
nmap <buffer> <S-TAB> mF
" Unmark all files
nmap <buffer> <Leader><TAB> mu
" 'Bookmark' a directory
nmap <buffer> bb mb
" Delete the most recent directory bookmark
nmap <buffer> bd mB
" Got to a directory on the most recent bookmark
nmap <buffer> bl gb
" Create a file
nmap <buffer> ff %
" Rename a file
nmap <buffer> fe R
" Copy marked files
nmap <buffer> fc mc
" Move marked files
nmap <buffer> fx mm
" Execute a command on marked files
nmap <buffer> f; mx
" Show the list of marked files
nmap <buffer> fl :echo join(netrw#Expose("netrwmarkfilelist"), "\n")<CR>
" Show the current target directory
nmap <buffer> fq :echo 'Target:' . netrw#Expose("netrwmftgt")<CR>
" Set the directory under the cursor as the current target
nmap <buffer> fg mtfq
" Delete a file
nmap <buffer> FF :call NetrwRemoveRecursive()<CR>
" Close the preview window
nmap <buffer> P <C-w>z
endfunction

augroup netrw_mapping
autocmd!
autocmd filetype netrw call NetrwMapping()
augroup END

Loading

0 comments on commit ef4bbf3

Please sign in to comment.