Start with a wide window for diff

From Vim Tips Wiki

Jump to: navigation, search
Tip 488 Printable Monobook Previous Next

created 2003 · complexity basic · author jaldri1 · version 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)

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.

[edit] Comments

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.

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

if has("gui")
  " ...

You can also check the availability of options with exists("+guioptions"). Note that for exists(), '+' is different then '&'.

If you want exactly 80 columns in each half:

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

The last 1 is for the vertical separator.

For win32 users, this will maximize the gvim window and split the two windows equally:

if has("gui_running")
  if &diff
    autocmd VimResized * wincmd =
    autocmd GuiEnter * simalt ~x

"wincmd =" is the same as Ctrl-= and makes split windows equal sized. See Maximize or set initial window size for "simalt ~x". --waywardson

Personal tools