sponsor Vim development Vim logo Vim Book Ad

intermediate Tip #1484: When jumping on a tag, automatically split the window if the current buffer has been modified

 tip karma   Rating 21/8, Viewed by 1419 

Read and edit this tip on the Vim tip wiki. The wiki may have a more recent version of this tip.

created:   January 21, 2007 17:08      complexity:   intermediate
author:   alpt      as of Vim:  

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 inside 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 splitted before:
                        quit
                endif
                call setpos('.', pos)
        endif
endfun  
        
nmap <C-]>   :call SPLITAG()<CR>z.
-------------%<----------%<-------------
    
and enjoy ;)

 rate this tip  Life Changing Helpful Unfulfilling 

<< Show Last modified time (and others) in status bar | change the pink omnicomplete popup to a readable color >>

Additional Notes

Anonymous, January 28, 2007 9:54
Can something similar be done for the gf command?
Anonymous, January 28, 2007 12:33
To use this tip for 'gf' it should be sufficient to replace
exe "tag ". expand("<cword>")
with
exe "normal gf"

(not tested)
Anonymous, January 31, 2007 14:30
:set switchbuf=split
:help 'switchbuf
Anonymous, January 22, 2007 18:11
Ctrl-W Ctrl-]
Anonymous, January 22, 2007 23:03
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?
If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to the maillist. Help Bram help Uganda.
   
SourceForge.net Logo