One of the features that I can't live without in Vim is the "Split-screens". For starters, the command :sp or :split will split the Vim window horizontally. And :vsp or :vsplit will split it vertically. It's common to supply a filename in the command. Imagine coding an HTML file in one split view and the respective CSS in the other all just in one window.
Navigating between split
Arbitrarily, you can have any number of split-screens in one window or tab. And navigating between them is pretty easy in Vim.
Ctrl-W w will switch between split-screens and
Ctrl-W j (
h or
k or
l ) will to navigate through split-screens in respective directions. For easier manipulation, I slap this in my
.vimrc remapping
Ctrl-W with just
Ctrl for basic movements between buffers.
nmap <C-j> <C-w>j
nmap <C-k> <C-w>k
nmap <C-h> <C-w>h
nmap <C-l> <C-w>l
Changing the default behavior
By default, vim will open the new vertical split window to the left of the old one and the new horizontal split windows will appear on the top of the old one. If you prefer otherwise,
set splitbelow
set splitright
Swapping Split Windows
While working with multiple split windows, I often find myself running into situations where I want to swap the positions of the two split windows. This can be done in with a combination of
Ctrl-W with basic movement commands. For instance,
Ctrl-W r will rotate the split windows. However, most of the time, I would end up screwing up the existing layout while rearranging the positions of the split windows. Looking for a better solution, I stumbled across
a neat little solutionfunction! MarkWindowSwap()
let g:markedWinNum = winnr()
endfunction
function! DoWindowSwap()
let curNum = winnr() "Mark destination
let curBuf = bufnr( "%" )
exe g:markedWinNum . "wincmd w"
let markedBuf = bufnr( "%" ) "Switch to source and shuffle dest->source
exe 'hide buf' curBuf "Hide and open so that we aren't prompted and keep history
exe curNum . "wincmd w" "Switch to dest and shuffle source->dest
exe 'hide buf' markedBuf "Hide and open so that we aren't prompted and keep history
endfunction
nmap <silent> <leader>mw :call MarkWindowSwap()<CR>
nmap <silent> <leader>pw :call DoWindowSwap()<CR>
<leader> represents the leader key which by default is
\ or whatever key that you have assigned to.
Move to the position of the split window and mark it with
<leader>mw and move the the destination window position and press
<leader>pw . Hola! you will have swapped buffers without screwing up the existing window layout.
Neat, isn't it?!
For more information on split windows, press :help window. Have fun, coding!
Categories:
productivity
vim
Tagged as:
productivity
vim
Leave a Comment