在vim中执行命令后跳转到最后一个光标位置



我有一个用户定义的vim命令,它调用vim Skcript:

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

我认为您需要"normal``":

command! -complete=shellcmd -nargs=+ Shell call s:RunShellCommand(<q-args>) | normal ``

另请参阅此处描述的winsaveview/winrestview函数:https://stackoverflow.com/a/9989348/85371

最新更新