Fandom

Vim Tips Wiki

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

1,624pages on
this wiki
Add New Page
Talk0 Share

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.

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
    split
  endif
  exe "tag ". expand("<cword>")
  let curfile=expand("%:p")
  if curfile == oldfile
    let pos=getpos(".")
    if &modified
      " if we have split before:
      quit
    endif
    call setpos('.', pos)
  endif
endfun
nmap <C-]> :call SPLITAG()<CR>z.

CommentsEdit

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

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

with

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?


Alternatively

: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