vim: shortcut to resize split windows - linux

When I have split windows in Vim, I can resize the windows using :resize +1/-1. I wanted to add a shortcut for it that worked like split windows in terminator. In terminator, if I have two windows on top of each other, CTRL Shift Up / Down moves the separator between the two windows, meaning, if I'm in the top window and press CTRL Shift Down, the top window increases. On the other hand, if I'm in the bottom window, CTRL Shift Down decreases the bottom window. So, it truly moves the separator.
With split windows in vim, I tried to remap like this:
:nnoremap <silent> <c-Up> :resize -1<CR>
:nnoremap <silent> <c-Down> :resize +1<CR>
This works fine if I'm on the top window (pressing CTRL UP decreases the size of the window and CTRL Down increases the size). But when I move to the bottom window it behaves correctly but it has a weird effect (CTRL UP also decreases the size of the window). So, I can't simulate moving the separator.
Is it possible o run a command depending on which window I'm located at?

Your code wasn't that wrong just needed a little changes.
Now you can resize all the panes in both horizontal and vertical way:
:nnoremap <silent> <c-Up> :resize -1<CR>
:nnoremap <silent> <c-Down> :resize +1<CR>
:nnoremap <silent> <c-left> :vertical resize -1<CR>
:nnoremap <silent> <c-right> :vertical resize +1<CR>

You can define a function in .vimrc
function! MoveSeparator(PlusMinus)
let num=tabpagewinnr(tabpagenr())
let pm=a:PlusMinus
if num == "2"
let pm = pm == '+' ? '-' : '+'
end
exec "resize " . pm . "1"
endfunction
nnoremap <silent> <C-UP> :call MoveSeparator("-")<CR>
nnoremap <silent> <C-DOWN> :call MoveSeparator("+")<CR>

Related

vimdiff and move among left and right pane

I am using vimdiff for the first time. Online I found written that to move from the left pane you use CTRL + w + Left or right arrow
This does not work for me. But I see that if I press just CTRL + w and press w for a sec and let it go, it switches pane after ~500ms.
Is this how it is supposed to work? Am I doing something wrong?
Ctrl+w and right and left arrow can be used to move between any split windows on vim, not only vimdiff splits.
These keys do work here on cygwin; also, Ctrl+w w also moves to the next window, but without the delay you mentioned.
It is possible that you have mapped these keys in your .vimrc or via some vim plugin. You can check this with :map w, :map <left> and :map <right>.
As moving between windows is something that you use often, you may consider using the following mappings:
nnoremap <C-J> <C-W>j
nnoremap <C-K> <C-W>k
nnoremap <C-H> <C-W>h
nnoremap <C-L> <C-W>l
Then you can use Ctrl+h and Ctrl+l to move left and right, without moving your hands from the home row. And the nnoremap will ensure that these works despite of any other mappings that you may have.
Press Ctrl + W and then (after releasing Ctrl + W) press the arrow keys to change the pane.
It is very useful to use set mouse=a in your .vimrc file. It gives you possibility to switch between windows using mouse. Additionally you can resize windows using it.
If you prefer to use keyboard I have also mapped arrow keys in .vimrc in this way:
map <C-Left> <C-W>j
map <C-Down> <C-W>k
map <C-Up> <C-W>h
map <C-Right> <C-W>l
To move among left and right pane, Press ctrl+w and then ctrl+r. This is both left and right vice-versa.
You can also use :wincmd w for next window, and :wincmd W for previous window.
The :wincmd is especially useful when ctrl+w is captured by the environment. For example see: https://stackoverflow.com/a/73749587/811335

Vim short binding for <C-w>| doesn't work

Trying to map horizontal maximize to something two keystrokes less, but none of these works:
nnoremap <leader>\ <c-w>| " Maximize horizontally
nnoremap <c-\\> <c-w>| " Maximize horizontally
At the same time these two work fine:
nnoremap <leader>- <c-w>_ " Maximize vertically
nnoremap <leader>= <c-w>= " Restore default size
What am I doing wrong?
The bar character | is a command separator in Vim script. To use it in a mapping, use <Bar>:
nnoremap <Leader>\ <C-W><Bar>
The relevant documentation is at :h map_bar.

Vim mappings/command not working

I am working with split windows in vim, but I am having trouble navigating and resizing split windows.
I have added these mappings to my .vimrc text file:
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>
But still, when pressing <A-Left> it does not move window. Also I am using <C-w> +, but the windows do not resize! I also tried <C-w> <, but it resizes like 1 pixel at a time! Is there any faster way to resize split windows?
10 Ctrl-W+ increases the window size by 10 lines. To resize all Windows to the same size, you can use CTRL-W = and to increase a window to its maximum size, use Ctrl-W _.
To resize in different steps, you can create maps that will adjust the window size differently. For example to increase the window size by a factor of 1.5 and decrease the window size by 0.67, you can map this:
nnoremap <silent> <Leader>+ :exe "resize " . (winheight(0) * 3/2)<CR>
nnoremap <silent> <Leader>- :exe "resize " . (winheight(0) * 2/3)<CR>
Alternatively you can use the :resize command to change the height of the window, to change the window width, use the :vertical modifier. So to resize by 10 lines, use:
:res +10
:res -10
as for navigation try
nnoremap <C-h> <C-w>h
nnoremap <C-j> <C-w>j
nnoremap <C-k> <C-w>k
nnoremap <C-l> <C-w>l
The <C-W> commands resize by individual lines / columns (pixels wouldn't even be possible in console Vim), but you are supposed to prepend a number before them to change larger amounts at once. (A lot of Vim commands take such a [count], as it's called in the help.)
For the <A-Up> mappings, do they work in graphical GVIM, but not in the console?! The mapping definitions look fine (but you should use :nnoremap).
Due to the way that the keyboard input is handled internally, some key combinations, like Ctrl + non-alphabetic cannot be mapped, and Ctrl + letter vs. Ctrl + Shift + letter cannot be distinguished. (Unless your terminal sends a distinct termcap code for it, which most don't.) In insert or command-line mode, try typing the key combination. If nothing happens / is inserted, you cannot use that key combination. This is a known pain point, and the subject of various discussions on vim_dev and the #vim IRC channel.
Some people (foremost Paul LeoNerd Evans) want to fix that (even for console Vim in terminals that support this), and have floated various proposals, cp. http://groups.google.com/group/vim_dev/browse_thread/thread/626e83fa4588b32a/bfbcb22f37a8a1f8
But as of today, no patches or volunteers have yet come forward, though many have expressed a desire to have this in a future Vim 8 major release.

VIM auto resize focused window

I am slowly learning vim and its powerful capabilities.
I have a question in regards to splitting windows (mainly horizontal splits).
Is there a way to automatically resize the currently selected (focused) window? Let's say, for example, a setting so that the focused window will always take up 70% of the screen.
Using Ctrl-w (number) +/- every time is not really efficient, especially when I am jumping between a few files constantly.
Also it would be cool if there was a way to restrict it to only horizontally splitted windows.
The 'winheight' setting determines the minimal number of lines for the current window. Some users set this to 999 for "Rolodex mode". The following sets this to 70%:
:let &winheight = &lines * 7 / 10
For anything fancier, you can hook into the WinEnter event via an :autocmd, and then set the window height to N via :Nwincmd _. Stupid example:
:autocmd WinEnter * execute winnr() * 2 . 'wincmd _'
Sounds like you want golden-ratio : Resize windows automatically using the Golden Ratio.
Once I found out you could disable the "automatic" part of golden-ratio, I started using it myself with these settings:
" Don't resize automatically.
let g:golden_ratio_autocommand = 0
" Mnemonic: - is next to =, but instead of resizing equally, all windows are
" resized to focus on the current.
nmap <C-w>- <Plug>(golden_ratio_resize)
" Fill screen with current window.
nnoremap <C-w>+ <C-w><Bar><C-w>_
I use these mapping for split screens:
nnoremap <C-j> <C-w>j
nnoremap <C-k> <C-w>k
nnoremap <C-h> <C-w>h
nnoremap <C-l> <C-w>l
nnoremap c<C-j> :bel sp new<cr>
nnoremap c<C-k> :abo sp new<cr>
nnoremap c<C-h> :lefta vsp new<cr>
nnoremap c<C-l> :rightb vsp new<cr>
nnoremap g<C-j> <C-w>j<C-w>_
nnoremap g<C-k> <C-w>k<C-w>_
nnoremap g<C-h> <C-w>h<C-w>_
nnoremap g<C-l> <C-w>l<C-w>_
nnoremap d<C-j> <C-w>j<C-w>c
nnoremap d<C-k> <C-w>k<C-w>c
nnoremap d<C-h> <C-w>h<C-w>c
nnoremap d<C-l> <C-w>l<C-w>c
This way if you want to jump between splits you can use C-hjkl. If you want to "create" a split you can use cC-hjkl. If you want to maximize a split you can use gC-hjkl. And if you want to delete a split you can use dC-hjkl.
If you want specifically 70% instead of maximized that you could use
nnoremap g<C-j> <C-w>j:let &winheight = &lines * 7 / 10<cr>
for example.

easier way to navigate between vim split panes

I am using NERDTree on vim and usually open files with i
Is there an easy way to switch between different panes? Currently I use CTRL+W+W to move from one pane to another.
Long ago I found a tip (once on vim.org, now on wikia, apparently) that I've stuck with. Remap ctrl-[hjkl] to navigate splits. It has served me well.
" Use ctrl-[hjkl] to select the active split!
nmap <silent> <c-k> :wincmd k<CR>
nmap <silent> <c-j> :wincmd j<CR>
nmap <silent> <c-h> :wincmd h<CR>
nmap <silent> <c-l> :wincmd l<CR>
I prefer hitting single keys over hitting key-chords. The following maps pane movement to arrow keys:
" Smart way to move between panes
map <up> <C-w><up>
map <down> <C-w><down>
map <left> <C-w><left>
map <right> <C-w><right>
I know this is an old question, but I have a perfect way. Using the number of the split.
split_number C-w C-w
The panes are numbered from top-left to bottom-right with the first one getting the number 1.
for example to go to split number 3 do this 3 C-w C-w, press Ctrl-w twice.
Key mappings are definitely the way to go. I use the mappings mentioned by overthink. I also include the following mappings in my vimrc to move the splits themselves.
" Move the splits arround!
nmap <silent> <c-s-k> <C-W>k
nmap <silent> <c-s-j> <C-W>j
nmap <silent> <c-s-h> <C-W>h
nmap <silent> <c-s-l> <C-W>l
This makes it so that if the split opens in the wrong spot (lets say the left side and I want it on the right) I go to that split and hit <C-S-l> and the split moves where I want it to.
In order to be consistent with changing tabs via gt & gT, I'm currently trying out the g mappings for changing splits. I tend to hit the shift key as I go for the Ctrl key so this helps me avoid that mistake until I get better at not doing so.
nnoremap gh <C-W><C-H>
nnoremap gj <C-W><C-J>
nnoremap gk <C-W><C-K>
nnoremap gl <C-W><C-L>
I have mapped ctrl+w ctrl+w to <tab> (under normal mode as in normal mode tab does not have any use)and that's have made my life easier as now I can switch between panes easily by pressing <tab>.
For switching to a particular pane, I can press <i> + <tab> to switch between panes as split window panes also got their own number which can replace i.
Ex. i = 1,2...n.
Very easy way of achieving it. Type this shortcut twice, and that should work
ctrl+w ctrl+w

Resources