ฉันมีคำสั่ง vim ที่ผู้ใช้กำหนดซึ่งเรียก vim-Skript:
command! -complete=shellcmd -nargs=+ Shell call s:RunShellCommand(<q-args>)
ถ้าฉันดำเนินการมัน
:Shell echo "foo"
เคอร์เซอร์จะข้ามไปที่บรรทัดแรกของไฟล์ แต่ฉันอยากให้มันอยู่ที่เดิมก่อนที่ฉันจะเข้าคำสั่ง
ฉันเหนื่อย
command! -complete=shellcmd -nargs=+ Shell call s:RunShellCommand(<q-args>) | ''
ซึ่งดูเหมือนจะไม่ทำงาน
RunShellCommand คือ
" Shell command with output in vim scratch buffer
function! s:RunShellCommand(cmdline)
let isfirst = 1
let words = []
for word in split(a:cmdline)
if isfirst
let isfirst = 0 " don't change first word (shell command)
else
if word[0] =~ '\v[%#<]'
let word = expand(word)
endif
let word = shellescape(word, 1)
endif
call add(words, word)
endfor
let expanded_cmdline = join(words)
botright new
setlocal buftype=nofile bufhidden=wipe nobuflisted noswapfile nowrap
call setline(1, 'You entered: ' . a:cmdline)
call setline(2, 'Expanded to: ' . expanded_cmdline)
call append(line('$'), substitute(getline(2), '.', '=', 'g'))
silent execute '$read !'. expanded_cmdline
" close scratch buffer if successfull
if v:shell_error == 0
q
endif
1
endfunction