filebrowser.vim 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. " Netrw - Vim's built-in file browser.
  2. " Put errors in `:messages`
  3. let g:netrw_use_errorwindow = 0
  4. " 'o' to (o)pen
  5. autocmd FileType netrw map <silent> <buffer> o <CR>
  6. " Dirvish file browser.
  7. " Use relative paths if there is no `conceal` ability.
  8. let g:dirvish_relative_paths = (v:version <= 703 ? 1 : 0)
  9. augroup dirvishCustomisation
  10. autocmd!
  11. " Allow fugitive.vim commands in dirvish buffers.
  12. autocmd FileType dirvish call fugitive#detect(@%)
  13. " Add/Adjust mappings.
  14. " - 'o' to (o)pen and 'i' to spl(i)t.
  15. " - Add, Copy, Delete, Rename with 'ma', 'mc', 'md', 'mr'.
  16. autocmd FileType dirvish let s:nowait = (v:version > 703 ? '<nowait>' : '')
  17. \| execute 'nnoremap '.s:nowait.'<buffer><silent> o :<C-U>.call dirvish#open("edit", 0)<CR>'
  18. \| execute 'nnoremap '.s:nowait.'<buffer><silent> i :<C-U>.call dirvish#open("split", 1)<CR>'
  19. \| execute 'xnoremap '.s:nowait.'<buffer><silent> O :call dirvish#open("edit", 0)<CR>'
  20. \| execute 'xnoremap '.s:nowait.'<buffer><silent> I :call dirvish#open("split", 1)<CR>'
  21. \| execute 'nnoremap '.s:nowait.'<buffer><silent> ma :<C-U>.call FileAdd()<CR>'
  22. \| execute 'nnoremap '.s:nowait.'<buffer><silent> mc :<C-U>.call FileCopy()<CR>'
  23. \| execute 'nnoremap '.s:nowait.'<buffer><silent> md :<C-U>.call FileDelete()<CR>'
  24. \| execute 'nnoremap '.s:nowait.'<buffer><silent> mr :<C-U>.call FileRename()<CR>'
  25. augroup END
  26. " Create any directories required.
  27. function! CreateDirs(filename)
  28. if &ft != "dirvish" | return | endif
  29. let dirname = fnamemodify(a:filename, ':p:h')
  30. if dirname != expand('%:h') | call system('mkdir -p '.dirname) | endif
  31. endfunction
  32. " Create files/directories in dirvish buffers.
  33. function! FileAdd()
  34. if &ft != "dirvish" | return | endif
  35. let newfile = inputdialog('New File: ', expand('%'), 0)
  36. if newfile == "0" | return | endif
  37. call CreateDirs(newfile)
  38. if !isdirectory(newfile)
  39. execute 'edit' newfile
  40. else
  41. normal R
  42. endif
  43. endfunction
  44. " Rename files/directories in dirvish buffers.
  45. function! FileCopy()
  46. if &ft != "dirvish" | return | endif
  47. let current = getline('.')
  48. let newname = inputdialog('Copy: ', current, 0)
  49. if newname == "0" | return | endif
  50. call CreateDirs(newname)
  51. call system('cp '.current.' '.newname) | normal R
  52. endfunction
  53. " Delete files/directories in dirvish buffers.
  54. function! FileDelete()
  55. if &ft != "dirvish" | return | endif
  56. let current = getline('.')
  57. if current == '/' | return | endif " Do not do the silly thing.
  58. let delcheck = confirm('Delete `'.current.'`?', "&Yes\n&No", 2)
  59. if delcheck != 1 | return | endif
  60. let delcheck = confirm('Are you SURE?', "&Yes\n&No", 2)
  61. if delcheck != 1 | return | endif
  62. call system('rm -rf '.current) | normal R
  63. endfunction
  64. " Rename files/directories in dirvish buffers.
  65. function! FileRename()
  66. if &ft != "dirvish" | return | endif
  67. let current = getline('.')
  68. let newname = inputdialog('Rename: ', current, 0)
  69. if newname == "0" | return | endif
  70. call CreateDirs(newname)
  71. call rename(current, newname) | normal R
  72. endfunction
  73. " Fuzzy Finder
  74. " Set `<Leader>p` to FZF if possible. CtrlP is always available at `<C-p>`
  75. if executable('fzf')
  76. map <silent> <Leader>p :FZF<CR>
  77. else
  78. let g:ctrlp_map = '<Leader>p'
  79. endif
  80. " CtrlP uses `git ls-files` in git repos, otherwise `ag` then `find`.
  81. let g:ctrlp_user_command = [
  82. \ '.git',
  83. \ 'cd %s && git ls-files . -co --exclude-standard',
  84. \ executable('ag') ? 'ag %s -l --nocolor -g ""' : 'find %s -type f'
  85. \]