Skip to content

Commit 5f71be7

Browse files
Merge pull request #44 from enthudave/master
add a config variable for the 'chosenfile'
2 parents 4f4ff45 + 7777a81 commit 5f71be7

File tree

2 files changed

+34
-14
lines changed

2 files changed

+34
-14
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,9 @@ If you want to see vim opening ranger when you open a directory (ex: nvim ./dir)
5454
let g:NERDTreeHijackNetrw = 0 // add this line if you use NERDTree
5555
let g:ranger_replace_netrw = 1 // open ranger when vim open a directory
5656
```
57+
Ranger.vim uses a temporary file to store the path that was chosen, `/tmp/chosenfile` by default.
58+
This can be a problem if you do not have write permissions for the `/tmp` directory, for example on Android.
59+
There is a configuration variable for this called `g:ranger_choice_file`, this must be set to the
60+
path for a file that doesn't yet exist (this file is created when choosing a file and removed afterwards).
61+
5762

plugin/ranger.vim

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,38 +23,53 @@
2323

2424

2525
" ================ Ranger =======================
26+
if exists('g:ranger_choice_file')
27+
if empty(glob(g:ranger_choice_file))
28+
let s:choice_file_path = g:ranger_choice_file
29+
else
30+
echom "Message from *Ranger.vim* :"
31+
echom "You've set the g:ranger_choice_file variable."
32+
echom "Please use the path for a file that does not already exist."
33+
echom "Using /tmp/chosenfile for now..."
34+
endif
35+
endif
36+
37+
if !exists('s:choice_file_path')
38+
let s:choice_file_path = '/tmp/chosenfile'
39+
endif
40+
2641
if has('nvim')
2742
function! OpenRangerIn(path, edit_cmd)
2843
let currentPath = expand(a:path)
2944
let rangerCallback = { 'name': 'ranger', 'edit_cmd': a:edit_cmd }
3045
function! rangerCallback.on_exit(id, code, _event)
3146
silent! Bclose!
3247
try
33-
if filereadable('/tmp/chosenfile')
34-
exec system('sed -ie "s/ /\\\ /g" /tmp/chosenfile')
35-
exec 'argadd ' . system('cat /tmp/chosenfile | tr "\\n" " "')
36-
exec self.edit_cmd . system('head -n1 /tmp/chosenfile')
37-
call system('rm /tmp/chosenfile')
48+
if filereadable(s:choice_file_path)
49+
exec system('sed -ie "s/ /\\\ /g" ' . s:choice_file_path)
50+
exec 'argadd ' . system('cat ' . s:choice_file_path . ' | tr "\\n" " "')
51+
exec self.edit_cmd . system('head -n1 ' . s:choice_file_path)
52+
call system('rm ' . s:choice_file_path)
3853
endif
3954
endtry
4055
endfunction
4156
enew
4257
if isdirectory(currentPath)
43-
call termopen('ranger --choosefiles=/tmp/chosenfile "' . currentPath . '"', rangerCallback)
58+
call termopen('ranger --choosefiles=' . s:choice_file_path . ' "' . currentPath . '"', rangerCallback)
4459
else
45-
call termopen('ranger --choosefiles=/tmp/chosenfile --selectfile="' . currentPath . '"', rangerCallback)
60+
call termopen('ranger --choosefiles=' . s:choice_file_path . ' --selectfile="' . currentPath . '"', rangerCallback)
4661
endif
4762
startinsert
4863
endfunction
4964
else
5065
function! OpenRangerIn(path, edit_cmd)
5166
let currentPath = expand(a:path)
52-
exec 'silent !ranger --choosefiles=/tmp/chosenfile --selectfile="' . currentPath . '"'
53-
if filereadable('/tmp/chosenfile')
54-
exec system('sed -ie "s/ /\\\ /g" /tmp/chosenfile')
55-
exec 'argadd ' . system('cat /tmp/chosenfile | tr "\\n" " "')
56-
exec a:edit_cmd . system('head -n1 /tmp/chosenfile')
57-
call system('rm /tmp/chosenfile')
67+
exec 'silent !ranger --choosefiles=' . s:choice_file_path . ' --selectfile="' . currentPath . '"'
68+
if filereadable(s:choice_file_path)
69+
exec system('sed -ie "s/ /\\\ /g" ' . s:choice_file_path)
70+
exec 'argadd ' . system('cat ' . s:choice_file_path . ' | tr "\\n" " "')
71+
exec a:edit_cmd . system('head -n1 ' . s:choice_file_path)
72+
call system('rm ' . s:choice_file_path)
5873
endif
5974
redraw!
6075
endfun
@@ -86,7 +101,7 @@ endfunction
86101
" Open Ranger in the directory passed by argument
87102
function! OpenRangerOnVimLoadDir(argv_path)
88103
" Open Ranger
89-
let path = expand(a:argv_path)
104+
let path = expand(a:argv_path)
90105
call OpenRangerIn(path, "edit")
91106

92107
" Delete the empty buffer created by vim

0 commit comments

Comments
 (0)