Wikia

Vim Tips Wiki

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

Talk0
1,610pages on
this wiki
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.
Advertisement | Your ad here

Around Wikia's network

Random Wiki