Skip to content

Commit de0cf05

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_preamble = '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 9c0dc32 commit de0cf05

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
@@ -119,6 +119,11 @@ function! dirvish#shdo(paths, cmd) abort
119119
let f = !jagged && 2==exists(':lcd') ? fnamemodify(f, ':t') : lines[i]
120120
let lines[i] = substitute(cmd, '\V{}', escape(shellescape(f),'&\'), 'g')
121121
endfor
122+
123+
if !empty(g:dirvish_shdo_preamble)
124+
let lines = [substitute(g:dirvish_shdo_preamble, '\V{}', escape(shellescape(head),'&\'), 'g'), ''] + lines
125+
endif
126+
122127
execute 'silent split' tmpfile '|' (2==exists(':lcd')?('lcd '.dir):'')
123128
setlocal bufhidden=wipe
124129
silent keepmarks keepjumps call setline(1, lines)

doc/dirvish.txt

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

169183
==============================================================================
170184
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_preamble = get(g:, 'dirvish_shdo_preamble', '')
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)