简体   繁体   中英

Jump to last cursor position after command in vim

I have a user defined vim command which calls a vim-Skript:

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

if I execute it

:Shell echo "foo"

the cursor jumps to the first line of the file. But I want it to stay in the same place where it was before I entered the command.

I tried

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

which does not seem to work.

The RunShellCommand is

" 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

I think you need "normal ``":

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

Also see the winsaveview / winrestview functions described here: https://stackoverflow.com/a/9989348/85371

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM