How can you switch your current windows from horizontal split to vertical split and vice versa in Vim?
I did that a moment ago by accident but I cannot find the key again.
Vim mailing list says (re-formatted for better readability):
To change two vertically split
windows to horizonally split
Ctrl-w t Ctrl-w K
Horizontally to vertically:
Ctrl-w t Ctrl-w H
Explanations:
Ctrl-w t makes the first (topleft) window current
Ctrl-w K moves the current window to full-width at the very top
Ctrl-w H moves the current window to full-height at far left
Note that the t is lowercase, and the K and H are uppercase.
Also, with only two windows, it seems like you can drop the Ctrl-w t part because if you're already in one of only two windows, what's the point of making it current?
Ctrl-w followed by H, J, K or L (capital) will move the current window to the far left, bottom, top or right respectively like normal cursor navigation.
The lower case equivalents move focus instead of moving the window.
When you have two or more windows open horizontally or vertically and want to switch them all to the other orientation, you can use the following:
(switch to horizontal)
:windo wincmd K
(switch to vertical)
:windo wincmd H
It's effectively going to each window individually and using ^WK or ^WH.
The following ex commands will (re-)split any number of windows:
To split vertically (e.g. make vertical dividers between windows), type :vertical ball
To split horizontally, type :ball
If there are hidden buffers, issuing these commands will also make the hidden buffers visible.
In VIM, take a look at the following to see different alternatives for what you might have done:
:help opening-window
For instance:
Ctrl-W s
Ctrl-W o
Ctrl-W v
Ctrl-W o
Ctrl-W s
...
Horizontal to vertical split
Ctrl+W for window command,
followed by Shift+H or Shift+L
Vertical to horizontal split
Ctrl+W for window command,
followed by Shift+K or Shift+J
Both solutions apply when only two windows exist.
After issuing the window command Ctrl+W, one is basically moving the window in the direction indicated by Shift+direction letter.
Opening help in a vertical split by default
Add both of these lines to .vimrc:
cabbrev help vert help
cabbrev h vert h
cabbrev stands for command abbreviation.
:vert[ical] {cmd} always executes the cmd in a vertically split window.
Inspired by Steve answer, I wrote simple function that toggles between vertical and horizontal splits for all windows in current tab. You can bind it to mapping like in the last line below.
function! ToggleWindowHorizontalVerticalSplit()
if !exists('t:splitType')
let t:splitType = 'vertical'
endif
if t:splitType == 'vertical' " is vertical switch to horizontal
windo wincmd K
let t:splitType = 'horizontal'
else " is horizontal switch to vertical
windo wincmd H
let t:splitType = 'vertical'
endif
endfunction
nnoremap <silent> <leader>wt :call ToggleWindowHorizontalVerticalSplit()<cr>
Following Mark Rushakoff's tip above, here is my mapping:
" vertical to horizontal ( | -> -- )
noremap <c-w>- <c-w>t<c-w>K
" horizontal to vertical ( -- -> | )
noremap <c-w>\| <c-w>t<c-w>H
noremap <c-w>\ <c-w>t<c-w>H
noremap <c-w>/ <c-w>t<c-w>H
Edit: use Ctrl-w r to swap two windows if they are not in the good order.
Related
I have vsplit two windows and make them same width.
Now I want to decrease the left window by five characters and increase the right window by five characters,when the cursor is in the left window, how to input command in normal mode or ex mode?
In general Ctrl-W commands do it.
Specifically here N Ctrl-W < shrinks the current window by one (or N if specified) column and N Ctrl-W > extends by one column (or N if specified).
Type :help window-resize for all related commands.
There are ways to do this in normal and ex-mode.
Let us just show you for your example.
In normal mode: 5followed by Ctrl + w at the same time, this should be finalized by <.
For better experience, you might like to map it to some key,
nnoremap <F5> <c-w><
This will allow you to press F5 again and again to decrease the size and stop at desired size.
In ex mode: You can use :vertical res N, where N is a absolute number. More details at :h window-resize
I am using ctags with vim on linux.
As soon as I press enter on any tag, the focus goes to the source code window and in order to regain focus on taglist window I have to to do :TlistToggle twice (with variable 'Tlist_GainFocus_On_ToggleOpen' set to 1) - which basically closes the taglist window and opens it again with acquired focus.
I am just curious if we can toggle focus between taglist window and source window without closing the taglist window?
:help window
...gives the chapter on Vim windows, with
:help window-move-cursor
...being the sub-chapter you're looking for:
4. Moving cursor to other windows *window-move-cursor*
CTRL-W <Down> *CTRL-W_<Down>*
CTRL-W CTRL-J *CTRL-W_CTRL-J* *CTRL-W_j*
CTRL-W j Move cursor to Nth window below current one. Uses the cursor
position to select between alternatives.
CTRL-W <Up> *CTRL-W_<Up>*
CTRL-W CTRL-K *CTRL-W_CTRL-K* *CTRL-W_k*
CTRL-W k Move cursor to Nth window above current one. Uses the cursor
position to select between alternatives.
CTRL-W <Left> *CTRL-W_<Left>*
CTRL-W CTRL-H *CTRL-W_CTRL-H*
CTRL-W <BS> *CTRL-W_<BS>* *CTRL-W_h*
CTRL-W h Move cursor to Nth window left of current one. Uses the
cursor position to select between alternatives.
CTRL-W <Right> *CTRL-W_<Right>*
CTRL-W CTRL-L *CTRL-W_CTRL-L* *CTRL-W_l*
CTRL-W l Move cursor to Nth window right of current one. Uses the
cursor position to select between alternatives.
CTRL-W w *CTRL-W_w* *CTRL-W_CTRL-W*
CTRL-W CTRL-W Without count: move cursor to window below/right of the
current one. If there is no window below or right, go to
top-left window.
With count: go to Nth window (windows are numbered from
top-left to bottom-right). To obtain the window number see
|bufwinnr()| and |winnr()|. When N is larger than the number
of windows go to the last window.
*CTRL-W_W*
CTRL-W W Without count: move cursor to window above/left of current
one. If there is no window above or left, go to bottom-right
window. With count: go to Nth window, like with CTRL-W w.
CTRL-W t *CTRL-W_t* *CTRL-W_CTRL-T*
CTRL-W CTRL-T Move cursor to top-left window.
CTRL-W b *CTRL-W_b* *CTRL-W_CTRL-B*
CTRL-W CTRL-B Move cursor to bottom-right window.
CTRL-W p *CTRL-W_p* *CTRL-W_CTRL-P*
CTRL-W CTRL-P Go to previous (last accessed) window.
*CTRL-W_P* *E441*
CTRL-W P Go to preview window. When there is no preview window this is
an error.
{not available when compiled without the |+quickfix| feature}
If Visual mode is active and the new window is not for the same buffer, the
Visual mode is ended. If the window is on the same buffer, the cursor
position is set to keep the same Visual area selected.
*:winc* *:wincmd*
These commands can also be executed with ":wincmd":
:[count]winc[md] {arg}
Like executing CTRL-W [count] {arg}. Example: >
:wincmd j
< Moves to the window below the current one.
This command is useful when a Normal mode cannot be used (for
the |CursorHold| autocommand event). Or when a Normal mode
command is inconvenient.
The count can also be a window number. Example: >
:exe nr . "wincmd w"
< This goes to window "nr".
That being said, by enabling mouse support (set mouse=a), you could just left-click where you want your cursor to be (and drag window borders, and...)
Isn't it the purpose of TlistOpen ?
Otherwise, if the taglist window has a name which can be recognized, you can extract the window number with let b = bufwinnr(the_buffer_name_or_id) and jump to it with exe b.'wincmd w'. You may need to search the right buffer with something like let buf_id = filter(range(1,bufnr('$')), 'v:val =~ "THEPATTERN"'))
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
To switch from vertical split to horizontal split fast in Vim
If I have 2 horizontally split windows, how to rotate them to get 2 vertically split windows?
And how to switch buffers?
If you have them split vertically C-wJ to move one to the bottom
If you have them split horizontally C-wL to move one to the right
To rotate in a 'column' or 'row' of split windows, C-wC-r
The following commands can be used to change the window layout. For example,
when there are two vertically split windows, CTRL-W K will change that in
horizontally split windows. CTRL-W H does it the other way around.
Ctrl-w H or type :wincmd H to go from horizontal to vertical layout.
Ctrl-w J or type :wincmd J to go from vertical to horizontal layout.
Ctrl-w r or type :wincmd r to swap the two buffers but keep the window layout the same.
Ctrl-w w or type :wincmd w to move the cursor between the two windows/buffers.
You may wish to bind one or more of these sequences to make it faster to type. I put this in my .vimrc so that ,l moves the cursor to the next buffer in the current tab:
let mapleader = ","
nmap <Leader>l <C-w>w
CTRL-W SHIFT-H will rotate the orientation, CTRL-W H moves to the left window, CTRL-W L moves to the right. See
:help split
and
:help ^w
for more information.
The current answers all work great if you only have two windows open. If you have more than that, the logic for moving windows around can get hairy.
I have this in my .vimrc to allow me to 'yank' and 'delete' a buffer and then paste it into a window over the current buffer or as a [v]split.
fu! PasteWindow(direction) "{{{
if exists("g:yanked_buffer")
if a:direction == 'edit'
let temp_buffer = bufnr('%')
endif
exec a:direction . " +buffer" . g:yanked_buffer
if a:direction == 'edit'
let g:yanked_buffer = temp_buffer
endif
endif
endf "}}}
"yank/paste buffers
:nmap <silent> <leader>wy :let g:yanked_buffer=bufnr('%')<cr>
:nmap <silent> <leader>wd :let g:yanked_buffer=bufnr('%')<cr>:q<cr>
:nmap <silent> <leader>wp :call PasteWindow('edit')<cr>
:nmap <silent> <leader>ws :call PasteWindow('split')<cr>
:nmap <silent> <leader>wv :call PasteWindow('vsplit')<cr>
:nmap <silent> <leader>wt :call PasteWindow('tabnew')<cr>
I have two opened buffers (i.e. A and B) and one window. Currently I see A, I would like to open B in vertical window. To do it in horizontal window I can hit Ctrl+w ^, how to do it in vertical window? And how to change horizontal windows to vertical windows?
To display your two buffers vertically, you can use :
:vsplit #
or the short version
:vs #
To reorganize your layout from horizontal to vertical, you must use:
<C-W>K : will move the current windows to to the top with a horizontal split
<C-W>J : will move the current windows to to the bottom with a horizontal split
<C-W>H : will move the current window to the the left with a vertical split
<C-W>L : will move the current window to the the rigth with a vertical split
See :help windows.txt for more information
You may use Vertical Split
Ctrl+Wv
command line :vertical split
open a vertical split: :vs
go to that split (assuming it's on the right: CTRL-W l
open buffer B there: :b B
I recently discovered Ctrl+E and Ctrl+Y shortcuts for Vim that respectively move the screen up and down with a one line step, without moving the cursor.
Do you know any command that leaves the cursor where it is but moves the screen so that the line which has the cursor becomes the first line? (having a command for the last line would be a nice bonus).
I can achieve this by manually pressing Ctrl+E (or Ctrl+Y) the proper number of times, but having a command that somehow does this directly would be nice.
Any ideas?
zz - move current line to the middle
of the screen
(Careful with zz, if you happen to have Caps Lock on accidentally, you will save and exit vim!)
zt - move current line
to the top of the screen
zb - move
current line to the bottom of the
screen
Additionally:
Ctrl-y Moves screen up one line
Ctrl-e Moves screen down one line
Ctrl-u Moves cursor & screen up ½ page
Ctrl-d Moves cursor & screen down ½ page
Ctrl-b Moves screen up one page, cursor to last line
Ctrl-f Moves screen down one page, cursor to first line
Ctrl-y and Ctrl-e only change the cursor position if it would be moved off screen.
Courtesy of www.lagmonster.org/docs/vi2.html
Vim requires the cursor to be in the current screen at all times, however, you could bookmark the current position scroll around and then return to where you were.
mg # This book marks the current position as g (this can be any letter)
<scroll around>
`g # return to g
I'm surprised no one is using the Scrolloff option which keeps the cursor in the middle of the page.
Try it with:
:set so=999
It's the first recommended method on the Vim wiki and works well.
I've used these shortcuts in the past (note: separate key strokes i.e. tap z, let go, tap the subsequent key):
z t ...or... z enter --> moves current line to top of screen
z z ...or... z . --> moves current line to center of screen
z b ...or... z - --> moves current line to bottom
If it's not obvious:
enter means the Return or Enter key.
. means the DOT or "full stop" key (.).
- means the HYPHEN key (-)
For what it's worth, z. avoids the danger of saving and closing Vi by accidentally typing ZZ if the caps-lock is on.
More info: :help scroll-cursor
Here's my solution in vimrc:
"keep cursor in the middle all the time :)
nnoremap k kzz
nnoremap j jzz
nnoremap p pzz
nnoremap P Pzz
nnoremap G Gzz
nnoremap x xzz
inoremap <ESC> <ESC>zz
nnoremap <ENTER> <ENTER>zz
inoremap <ENTER> <ENTER><ESC>zzi
nnoremap o o<ESC>zza
nnoremap O O<ESC>zza
nnoremap a a<ESC>zza
So that the cursor will stay in the middle of the screen, and the screen will move up or down.
To leave the cursor in the same column when you use Ctrl+D, Ctrl+F, Ctrl+B, Ctrl+U, G, H, M, L, gg
you should define the following option:
:set nostartofline
my mnemonic for scrolling...
Adding to other answers also pay attention to ze and zs, meaning: move screen to the left/right of the cursor (without moving the cursor)
+-------------------------------+
^ |
|c-e (keep cursor) |
|H(igh) zt (top) |
| ^ |
| ze | zs |
|M(iddle) zh/zH <--zz--> zl/zL |
| | |
| v |
|L(ow) zb (bottom) |
|c-y (keep cursor) |
v |
+-------------------------------+
also look at the position of h and l and t and b and (with qwertz keyboard) c-e and c-y (also the "y" somehow points to the bottom) on the keyboard to remember where the screen is moving.
Enter vim and type:
:help z
z is the vim command for redraw, so it will redraw the file relative to where you position the cursor. The options you have are as follows:
z+ - Redraws the file with the cursor at top of the window and at first non-blank character of your line.
z- - Redraws the file with the cursor at bottom of the window and at first non-blank character of your line.
z. - Redraws the file with the cursor at centre of the window and at first non-blank character of your line.
zt - Redraws file with the cursor at top of the window.
zb - Redraws file with the cursor at bottom of the window.
zz - Redraws file with the cursor at centre of the window.
You can prefix your cursor move commands with a number and that will repeat that command that many times
10Ctrl+E will do Ctrl+E 10 times instead of one.
zEnter does exactly what this question asks for.
It works where strangely zz would not work (vim 7.4.1689 on Ubuntu 2016.04 LTS with no special .vimrc)
You may find answers to "Scrolling Vim relative to cursor, custom mapping" useful.
You can use ScrollToPercent(0) from that question to do this.
Sometimes it is useful to scroll the text with the K and J keys, so I have this "scroll mode" function in my .vimrc (also bound to zs).
See scroll_mode.vim.
I wrote a plugin which enables me to navigate the file without moving the cursor position. It's based on folding the lines between your position and your target position and then jumping over the fold, or abort it and don't move at all.
It's also easy to fast-switch between the cursor on the first line, the last line and cursor in the middle by just clicking j, k or l when you are in the mode of the plugin.
I guess it would be a good fit here.