sponsor Vim development Vim logo Vim Book Ad

intermediate Tip #173: Switch between splits very fast (for multi-file editing)

 tip karma   Rating 570/229, Viewed by 9735 

created:   December 4, 2001 11:48      complexity:   intermediate
author:   Jonathan McPherson      as of Vim:   5.7

I am a Web developer and I use Vim as my primary editor.

Most programming projects (and Web programming projects, in particular) are spread out over multiple files, which you often want to have open concurrently. If you don't already know, Vim supports this very well! Just use:

:sp name-of-another-file-to-edit

My problems were that (1) it took too long to move between files, and (2) the files were taking up too much room on the screen.

(1) In order to move to the file in the split above my current window, I was typing Ctrl-W, Up (move up a window) Ctrl-W, _ (maximize the menu). That's four keystrokes (more if you count Ctrl and Shift), and they are all over the keyboard. To help avoid this problem, I created this mapping in my .vimrc:

map <C-J> <C-W>j<C-W>_
map <C-K> <C-W>k<C-W>_

Now I can hold down Ctrl and move between windows with the standard Vim movement keys. Much, much quicker!

(2) By default, Vim displays the current line of each minimized file, which (to me) isn't much help and takes up too much screen real estate. I use this line in my .vimrc:

set wmh=0

This sets the minimum window height to 0, so you can stack many more files before things get crowded. Vim will only display the filename.

Hope this helps those of you who are working on projects with large numbers of files you're constantly flipping through. Happy Vimming!

 rate this tip  Life Changing Helpful Unfulfilling 

<<Using Ispell on a highlighted region | Footnotes >>

Additional Notes

[email protected] - NOSPAM, December 14, 2001 13:25
One may also do the dual thing for vertical splits/window changes:
set wmw=0
nmap <c-h> <c-w>h<c-w><bar>
nmap <c-l> <c-w>l<c->w<bar>
[email protected], February 11, 2002 7:35
It should be

nmap <c-l> <c-w>l<c-w><bar>

of course...
[email protected], May 14, 2002 17:51
wonderful wonderful wonderful wonderful....
thanks so much, great tip!
Anonymous, August 26, 2002 14:36
This is so useful, perhaps it can be made part of "standard" VIM!  (Although I would probably leave out the "maximize" portion: <c-w>_)
[email protected], September 2, 2002 0:38
I have solved the same problem by setting:
set winminheight=0
set winheight=999

winheight is always bigger than my window so
the current window is maximized.

I do not have to set mapping and I can use other
commands to change buffer, fi C-W C-W, mouse click
on status line of the buffer etc.
[email protected], September 10, 2002 6:46
very useful tip, thanks
Mohit Kalra, October 6, 2002 7:19
Damn neat trick.  Thanks for the one on vertical splits too. (I prefer the vertical split tip without the <bar>.)  
[email protected], January 22, 2003 1:16
I use gvim *.cpp at the dos prompt to open-up all source files in a project and I have to use ":n" and ":prev" to roll over them. How can I map ":n" and ":prev" on the CTRL-foo or the ALT-foo ?
[email protected], February 21, 2003 9:09
This should do the trick
map <C-N> <ESC>:n <CR>
map <C-P> <ESC>:prev <CR>
[email protected], May 23, 2003 12:07
I would like to use
map <C-N> <ESC>:bn <CR>  
map <C-P> <ESC>:bp <CR>
instead to switch between previous and next buffers.
[email protected], November 11, 2003 13:59
I created the SwitchWindow function which uses the ALT left/right/up/down cursor keys to easily move between windows.

" Switch window mappings /*{{{*/
nnoremap <A-Up>     :normal <c-r>=SwitchWindow('+')<cr><cr>
nnoremap <A-Down>   :normal <c-r>=SwitchWindow('-')<cr><cr>
nnoremap <A-Left>   :normal <c-r>=SwitchWindow('<')<cr><cr>
nnoremap <A-Right>  :normal <c-r>=SwitchWindow('>')<cr><cr>

function! SwitchWindow(dir)
    let this = winnr()

    if '+' == a:dir
        execute "normal \<c-w>k"
    elseif '-' == a:dir
        execute "normal \<c-w>j"
    elseif '>' == a:dir
        execute "normal \<c-w>l"
    elseif '<' == a:dir
        execute "normal \<c-w>h"
    else
        echo "oops. check your ~/.vimrc"
        return ""
    endif
endfunction
" /*}}}*/


This was an easy extension to the function I use for resizing windows:
See the note I added to:
Tip #427: Fast window resizing with +/- keys
https://www.vim8.org/tips/tip.php?tip_id=427
[email protected], November 12, 2003 5:31
Why have a whole function when you can simply:

nmap <silent> <A-Up> :wincmd k<CR>
nmap <silent> <A-Down> :wincmd j<CR>
nmap <silent> <A-Left> :wincmd h<CR>
nmap <silent> <A-Right> :wincmd l<CR>
[email protected], March 28, 2004 4:23
another very useful mapping for me is:

:imap <C-w> <C-o><C-w>

this allows all window commands in insert mode and i'm not accidentally deleting words anymore :-)
http://www.cs.albany.edu/~mosh, April 16, 2004 20:13
No keypressing -- focus-follows-mouse for gvim

> cat ~/_gvimrc  

:set mousefocus      


  
[email protected], July 20, 2004 12:51
How can we do split window in solaris?
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