Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open editable file in vsplit but stay in original position in vimscript

Tags:

vim

I'm trying to write a plugin that will make a system call which generates a file based on the current buffer and then open the generated file in a vsplit, or if already open, it will update it when the source file is changed.

I've got the code to the point that it generates the file and opens/updates the split but the problem is that when it first opens the split the focus goes to the split and when it updates the cursor position on the source file jumps to the top of the page.

Here is what I'm trying, any help would be greatly appreciated.

execute 'keepjumps silent ! ' . s:cmd . ' ' . s:src_file . ' > ' . s:dst_file

if exists("s:outputbufnr") && bufexists(s:outputbufnr)
  execute 'keepjumps ' . bufwinnr(s:outputbufnr)
else
  " execute "keepjumps vnew " s:dst_file
  execute "keepjumps rightbelow vertical new " . s:dst_file
  let s:outputbufnr=bufnr(s:dst_file)
endif  

From what I though keepjumps should be returning the cursor to its previous position, however, that doesn't appear to be the case.

like image 380
JaredMcAteer Avatar asked May 03 '12 14:05

JaredMcAteer


2 Answers

This sounds like the perfect use of the preview window in vim. When you open a file in the preview window (:pedit /path/to/file) focus is not taken away from the current window. You also have the ability to jump directly to the preview window whenever you need to with wincmd P if you need to. You can also close the preview window from anywhere with :pclose. If the file has changed and you want to see the updates you can just :pedit /path/to/file again to get the updates.

Another small benefit, even if you have a file in the preview window you can still quit vim with a plain :q instead of :qa.

like image 170
Randy Morris Avatar answered Oct 20 '22 23:10

Randy Morris


I suspect you want to use wincmd p after the split, this will put you back in the previous window you were in.

like image 43
Benj Avatar answered Oct 20 '22 23:10

Benj