Vim Tips Wiki

When jumping on a tag, automatically split the window if the current buffer has been modified

1,623pages on
this wiki
Add New Page
Add New Page Talk0
Tip 1484 Printable Monobook Previous Next

created January 21, 2007 · complexity intermediate · author alpt · version n/a

Suppose you open file.c, you edit it and then you jump on a tag, defined in another file, using <C-]>.

If you didn't save the modified buffer you'll just get:

"E37: No write since last change (add ! to override)"

To overcome this, put the following code in your vimrc.

fun! SPLITAG() range
  let oldfile=expand("%:p")
  if &modified
  exe "tag ". expand("<cword>")
  let curfile=expand("%:p")
  if curfile == oldfile
    let pos=getpos(".")
    if &modified
      " if we have split before:
    call setpos('.', pos)
nmap <C-]> :call SPLITAG()<CR>z.


To use this tip for 'gf' it should be sufficient to replace

exe "tag ". expand("<cword>")


exe "normal gf"

:set switchbuf=split
:help 'switchbuf

Ctrl-W Ctrl-]

CTRL-W CTRL-] doesn't give the same level of laziness of this tip. Why should I remember to use CTRL-W CTRL-], when I can press CTRL-] all the time?


:set hidden

and you won't see "E37: No write since last change (add ! to override)"

Or, add "set confirm" and you will be prompted to save, abandon changes, or cancel.

Also on Fandom

Random Wiki