Vim vertical resize shortcut - vim

I would like to set a shortcut to resize the width of two panes:
:vertical resize +2
I would like to map this to [option] + (in other words, shift + option + "+). I am on a mac. I tried doing the following. How would I add in this shortcut for a nnoremap ?

you can do this without having to switch to the ex command line:
To change vertical height:
[n] --> ctrl + w --> [+ | -]
And horizontal:
[n] --> ctrl + w --> [< | >]

Related

Resize window in vim with a keyboard map

I am trying to set the + and _ keys to increase or decrease the size of a window pane in vim. Here is what I have so far in my .vimrc:
nnoremap + :res +5
nnoremap _ :res -5
However it doesn't appear to work. What would be the proper way to map the resize pane in vim? Also, is there a way to press [enter] automatically after entering the command so that it executes automatically?
The :res commands are fine, but you need to append <CR> (for Carriage Return) to the mappings to actually execute them when you press + or _ . So, your mappings should look like this:
nnoremap + :res +5<CR>
nnoremap _ :res -5<CR>
It should be noted that there are built-in hotkeys in Vim to increase and decrease the window height and width, with the default number being 1:
Increase height (by 1): Ctrl-W +
Decrease height (by 1): Ctrl-W -
Increase width (by 1): Ctrl-W >
Decrease width (by 1): Ctrl-W <
To use the above hotkeys with values other than 1, simply prepend the hotkey with the value:
Increase height by 5: 5 Ctrl-W +
Alternative: use a "mode" where normal arrow keys can resize the current window.
either map to + or
:call ToggleResizeMode()
"
" + toggle remap arrow keys to resize windows
"
nnoremap + :call ToggleResizeMode()<CR>
let s:KeyResizeEnabled = 0
function! ToggleResizeMode()
if s:KeyResizeEnabled
call NormalArrowKeys()
let s:KeyResizeEnabled = 0
else
call ResizeArrowKeys()
let s:KeyResizeEnabled = 1
endif
endfunction
function! NormalArrowKeys()
" unmap arrow keys
echo 'normal arrow keys'
nunmap <Up>
nunmap <Down>
nunmap <Left>
nunmap <Right>
endfunction
function! ResizeArrowKeys()
" Remap arrow keys to resize window
echo 'Resize window with arrow keys'
nnoremap <Up> :resize +2<CR>
nnoremap <Down> :resize -2<CR>
nnoremap <Left> :vertical resize -2<CR>
nnoremap <Right> :vertical resize +2<CR>
endfunction
Or better: someone already implemented a window submode, there is probably a plugin for it somewhere.
https://ddrscott.github.io/blog/2016/making-a-window-submode/

Prevent opening fold under the cursor when moving the cursor to left or right in Vim

Whenever I move the cursor left or right using h or l, the fold under the cursor opens automatically. By the way, moving top or down does not have this problem. Is there any way to prevent automatically opening fold when moving horizontally?
You can create the auto command:
autocmd! CursorMoved * if foldclosed('.') != -1 |
\ nnoremap h <nop>|
\ nnoremap l <nop>|
\ else |
\ silent! unmap h|
\ silent! unmap l|
\ endif
Here foldclosed('.') returns -1 if current line is unfolded. Instead of using this auto command just avoid pressing h or l over folds.
The default value of :help 'foldopen' has hor in it; this causes horizontal movements (like l, h) to open folds.
To disable this, simply add this to your ~/.vimrc:
set foldopen-=hor

VIM - map # (number sign) to "search selected" in visual mode

I'm struggling to map # to search selected in visual. What I've tried so far was vnoremap <silent> <#> y<ESC>/^R0<CR> (y ank selected, search / from ^R egister 0 and hit CR ), but it does not work. What is the right way to do this?
vnoremap # y/<C-r>0<CR>
# instead of <#>, <Esc> is redundant after y, and ^R should be <C-r>.

Setting the cursor to a vertical thin line in vim

I am trying to set the cursor in insert mode to be a thin vertical line and I am unable to. I have tried this in my .vimrc file:
set guicursor+=i:ver100-iCursor
It does not set the cursor to a vertical bar on insert mode.
What am I missing and how do I do this?
This code in my /home/el/.vimrc worked for my console:
if $TERM_PROGRAM =~ "iTerm"
let &t_SI = "\<Esc>]50;CursorShape=1\x7" " Vertical bar in insert mode
let &t_EI = "\<Esc>]50;CursorShape=0\x7" " Block in normal mode
endif
Which does this for me:
Source:
https://hamberg.no/erlend/posts/2014-03-09-change-vim-cursor-in-iterm.html
For gnome terminal version>3.15
Add this to your ~/.vimrc.
if has("autocmd")
au VimEnter,InsertLeave * silent execute '!echo -ne "\e[2 q"' | redraw!
au InsertEnter,InsertChange *
\ if v:insertmode == 'i' |
\ silent execute '!echo -ne "\e[6 q"' | redraw! |
\ elseif v:insertmode == 'r' |
\ silent execute '!echo -ne "\e[4 q"' | redraw! |
\ endif
au VimLeave * silent execute '!echo -ne "\e[ q"' | redraw!
endif
You will get a block cursor in normal mode and a thin one in insert mode.
This did the trick:
set guicursor=i:ver25-iCursor
I had to reduce the 100 to 25
I use iTerm2 on mac and none of the above worked. Silly (vim and interface ain't right) but works. To switch between vertical bar or box. Profiles -> Open Profiles... -> Edit Profiles... -> Text

How to increase the vertical split window size in Vim

:vsplit (short form: :vs) split the Vim viewport vertically. :30vs splits the viewport, making the new window 30 characters wide. Once this 30 char window is created, how would one change it's size to 31 or 29?
With horizontal windows Ctrl-W + increases the number of lines by one. What is the equivalent command to increase the columns by one?
CTRL-W >
and
CTRL-W <
to make the window wider or narrower.
And Ctr-W =
will make them equal
In case you need HORIZONTAL SPLIT resize as well:
The command is the same for all splits, just the parameter changes:
- + instead of < >
Examples:
Decrease horizontal size by 10 columns
:10winc -
Increase horizontal size by 30 columns
:30winc +
or within normal mode:
Horizontal splits
10 CTRL+w -
30 CTRL+w +
Vertical splits
10 CTRL+w < (decrease)
30 CTRL+w > (increase)
Another tip from my side:
In order to set the window's width to let's say exactly 80 columns, use
80 CTRL+W |
In order to set it to maximum width, just omit the preceding number:
CTRL+W |
I have these mapped in my .gvimrc to let me hit command-[arrow] to move the height and width of my current window around:
" resize current buffer by +/- 5
nnoremap <D-left> :vertical resize -5<cr>
nnoremap <D-down> :resize +5<cr>
nnoremap <D-up> :resize -5<cr>
nnoremap <D-right> :vertical resize +5<cr>
For MacVim, you have to put them in your .gvimrc (and not your .vimrc) as they'll otherwise get overwritten by the system .gvimrc
Along the same lines, I use the following in my .vimrc to let me move through the splits, automatically expanding the one I'm moving to to its full size and shrinking all the rest to their minimum height or width:
" Switch between window splits using big J or K and expand the split to its
" full size.
"
" Move vertically in the window through the horizontal splits...
map <C-J> <C-w>j<C-w>_
map <C-K> <C-w>k<C-w>_
" Move horizontally in the window through the vertical splits...
map <C-H> <C-w>h<C-w>\|
map <C-L> <C-w>l<C-w>\|
This is what I am using as of now:
nnoremap <silent> <Leader>= :exe "resize " . (winheight(0) * 3/2)<CR>
nnoremap <silent> <Leader>- :exe "resize " . (winheight(0) * 2/3)<CR>
nnoremap <silent> <Leader>0 :exe "vertical resize " . (winwidth(0) * 3/2)<CR>
nnoremap <silent> <Leader>9 :exe "vertical resize " . (winwidth(0) * 2/3)<CR>
For changing width use "vertical resize" and for changing height use "resize".
I have done following mapping in my .vimrc
ALT→ will increase width of the selected split
ALT← will decrease width of the selected split
ALT↓ will increase height of the selected split
ALT↑ will decrease height of the selected split
My .vimrc code:
nmap <M-Right> :vertical resize +1<CR>
nmap <M-Left> :vertical resize -1<CR>
nmap <M-Down> :resize +1<CR>
nmap <M-Up> :resize -1<CR>
Vim Resize Splits more quickly
I am using numbers to resize by mapping the following in .vimrc
nmap 7 :res +2<CR> " increase pane by 2
nmap 8 :res -2<CR> " decrease pane by 2
nmap 9 :vertical res +2<CR> " vertical increase pane by 2
nmap 0 :vertical res -2<CR> " vertical decrease pane by 2
I am using the below commands for this:
set lines=50 " For increasing the height to 50 lines (vertical)
set columns=200 " For increasing the width to 200 columns (horizontal)

Resources