Should I dual ranger/cleric or wait for the THAC0 bonus?
[ranger.git] / examples / vim_file_chooser.vim
blobfb9b7e1be0c4ff7a953849ba1f7b31d0452f1f42
1 " Compatible with ranger 1.4.2 through 1.7.*
3 " Add ranger as a file chooser in vim
5 " If you add this code to the .vimrc, ranger can be started using the command
6 " ":RangerChooser" or the keybinding "<leader>r".  Once you select one or more
7 " files, press enter and ranger will quit again and vim will open the selected
8 " files.
10 function! RangeChooser()
11     let temp = tempname()
12     " The option "--choosefiles" was added in ranger 1.5.1. Use the next line
13     " with ranger 1.4.2 through 1.5.0 instead.
14     "exec 'silent !ranger --choosefile=' . shellescape(temp)
15     exec 'silent !ranger --choosefiles=' . shellescape(temp)
16     if !filereadable(temp)
17         redraw!
18         " Nothing to read.
19         return
20     endif
21     let names = readfile(temp)
22     if empty(names)
23         redraw!
24         " Nothing to open.
25         return
26     endif
27     " Edit the first item.
28     exec 'edit ' . fnameescape(names[0])
29     " Add any remaning items to the arg list/buffer list.
30     for name in names[1:]
31         exec 'argadd ' . fnameescape(name)
32     endfor
33     redraw!
34 endfunction
35 command! -bar RangerChooser call RangeChooser()
36 nnoremap <leader>r :<C-U>RangerChooser<CR>