Skip to content

Commit 90032e3

Browse files
committed
Add option for preamble before shdo scripts
I want all of my scripts to start in the current dirvish working directory, so I use the example from the doc: let g:dirvish_shdo_before = 'cd {}' Other users may tend to create scripts with arguments, want their commands to be inside a function, or output some diagnostic data. Using the same escaping that is applied on lines since that seems like the safest thing to do.
1 parent 2ddd8ee commit 90032e3

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

autoload/dirvish.vim

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,11 @@ func! dirvish#shdo(paths, cmd) abort
151151
let f = !jagged && 2==exists(':lcd') ? fnamemodify(f, ':t') : lines[i]
152152
let lines[i] = substitute(cmd, '\V{}', escape(shellescape(f),'&\'), 'g')
153153
endfor
154+
155+
if !empty(g:dirvish_shdo_before)
156+
let lines = [substitute(g:dirvish_shdo_before, '\V{}', escape(shellescape(head),'&\'), 'g'), ''] + lines
157+
endif
158+
154159
execute 'silent split' tmpfile '|' (2==exists(':lcd')?('lcd '.dir):'')
155160
setlocal bufhidden=wipe
156161
silent keepmarks keepjumps call setline(1, lines)

doc/dirvish.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,20 @@ can be overridden by handling that event. Example: >
163163
\ gh :silent keeppatterns g@\v/\.[^\/]+/?$@d _<cr>:setl cole=3<cr>
164164
augroup END
165165
<
166+
g:dirvish_shdo_before = '' *g:dirvish_shdo_before*
167+
Inserts this text at the beginning of shell scripts generated with
168+
|:Shdo|. The current dirvish directory is inserted wherever {} appears in
169+
|g:dirvish_shdo_before|.
170+
To ensure scripts always cd to the current dirvish directory, you could
171+
add this to your vimrc: >
172+
173+
if has('win32')
174+
" work across hard drives
175+
let g:dirvish_shdo_before = 'pushd {}'
176+
else
177+
let g:dirvish_shdo_before = 'cd {}'
178+
endif
179+
<
166180

167181
==============================================================================
168182
FAQ *dirvish-faq*

plugin/dirvish.vim

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ if exists('g:loaded_dirvish') || &cp || v:version < 700 || &cpo =~# 'C'
33
endif
44
let g:loaded_dirvish = 1
55

6+
let g:dirvish_shdo_before = get(g:, 'dirvish_shdo_before', '')
7+
68
command! -bar -nargs=? -complete=dir Dirvish call dirvish#open(<q-args>)
79
command! -nargs=* -complete=file -range -bang Shdo call dirvish#shdo(<bang>0 ? argv() : getline(<line1>, <line2>), <q-args>)
810

0 commit comments

Comments
 (0)