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

时间:2012-04-03 14:54:47

标签: vim

我有一个用户定义的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

1 个答案:

答案 0 :(得分:1)

我认为你需要“正常的``”:

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

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