sponsor Vim development Vim logo Vim Book Ad

basic Tip #488: vimrc setting for wider vim diff window (gVim)

 tip karma   Rating 15/9, Viewed by 1282 

created:   June 16, 2003 15:57      complexity:   basic
author:   jaldri1 at gl dot umbc dot edu      as of Vim:   6.0

The Vim diff feature (fantastic as it is) opens with the default window width (gVim), and the two files each get a half width buffer windows with a vertical split between them.  When you resize, only one buffer window changes.  We might write function to automatically center the split and possibly trigger it automatically with an autocmd (advanced).  However, we can still have Vim open with a wider window when performing a diff.  The following code can be added to the vimrc file.  Be sure to adjust the numbers for the available screen real-estate and the other settings to taste.

"===============================================================
" Window settings
set lines=60           " Set window height
set columns=98         " Set window width 'co'
set guioptions+=b      " Add bottom scroll bar 'go'

" If comparing files side-by-side, then ...
if &diff;
    " double the width up to a reasonable maximum
    let &columns; = ((&columns;*2 > 172)? 172: &columns;*2)
endif
"===============================================================

Remember, the default for the columns setting is either 80 or the terminal width.  I wouldn't expect consistant results for non-gVim usage.  By the way, I still need to add a test to detect vim vs. gVim in my own vimrc (perhaps greping $VIM for gvim) so that I can adjust colors and other settings accordingly.  Note that the expression in the let statement can be replaced with a constant.  But, that expression may prove usefull if the columns setting is to vary (e.g. a filetype plugin).  For a general function (intermediate), someone would want to come up with some global names like MaxScreenColumns or MaxBufferColumns.

 rate this tip  Life Changing Helpful Unfulfilling 

<<jump to a file to a certain line number | Section jump in Latex >>

Additional Notes

niklas, June 18, 2003 3:32
To check if the GUI is enabled, just test with:
    if has("gui_running") ... endif

Another solution is to put GUI-specific code in your .gvimrc instead, which can make stuff a lot cleaner.
jaldri1 at gl dot umbc dot edu, June 18, 2003 3:38
Thanks to everyone for the feedback on testing for a GUI mode.  

For code that should only be executed when running gVim, wrap it in:

    if has("gui")
        " ...
    endif

You can also check the availability of options with exists("+guioptions").  Note that for exists(), '+' is different then '&'.
Anonymous, July 17, 2003 20:03
If you want exactly 80 columns in each half:

if &diff;
        let &columns; = 160 +  2*&foldcolumn; + 1
endif

The last 1 is for the vertical separator.
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 [email protected] after searching the archive. Help Bram help Uganda.
Sponsored by Web Concept Group Inc. SourceForge Logo