switching windows with <leader> instead of ctrl - vim

I recently start using leader key in vim, and mapped to space
Earlier I use window switching with ctrl + {h, j, k, l}
nnoremap <C-h> <C-w>h
nnoremap <C-j> <C-w>j
nnoremap <C-k> <C-w>k
nnoremap <C-l> <C-w>l
And now I was trying something like
nnoremap <leader>h <C-w>h
nnoremap <leader>j <C-w>j
nnoremap <leader>k <C-w>k
nnoremap <leader>l <C-w>l
I already remove any prior mapping with the above mentioned key.
Now issue I am facing is, with ctrl key i can switch the window panes without lifting finger from ctrl While with space I have to press Leader and {h, j, k, l} simultaneously and then I have to press leader and {h, j, k, l} if want to go to some other window pane.
What I am trying to say is, let say my window vertically split b/w two and I wish to go to second window and come back to original.
With ctrl key: ctrl + l + h
with leader key: space + l, space + h
I want my leader key work exact same as ctrl, since it is convenient to use
Is this possible?
Also If you have some advice for newbie like me, I will be glad to hear it.

What you call "leader key" is not a special key at all and certainly not a modifier, like Ctrl, Shift, or Alt so you can't make it work like one. Unless they involve one of those modifiers, Vim mappings are always sequential: you press the first key, then the second one, etc.
If you absolutely want chord-like mappings, you could try arpeggio.

Related

How do I rebind `hjkl` to `jkl;`

I've been trying to rebind to proper touch typing keys, and
it's actually more complicated I expected. This is my init.vim:
" Normal mode
nmap ; <Right>
nmap l <Up>
nmap k <Down>
nmap j <Left>
nnoremap h ;
" Visual mode
vmap ; <Right>
vmap l <Up>
vmap k <Down>
vmap j <Left>
vnoremap h ;
" Rebind the window-switching movements
nnoremap <C-w>; <C-w>l
nnoremap <C-w>l <C-w>k
nnoremap <C-w>k <C-w>j
nnoremap <C-w>j <C-w>h
nnoremap <C-w>h <C-w>;
Looks fine, right? Except it's not. By default in vim, when you press Ctrl + W + k, your window will switch, regardless if you pressed k with Ctrl + W already being pressed down or in succession to Ctrl + W. However, with my key rebinds, the movement key must be pressed after releasing Ctrl + W. This ruins my workflow, as sometimes, I try to quickly switch window, and I fail because I didn't release Ctrl + W quick enough.
How can I achieve a proper keybind without making window-switching less convenient? Thanks.
If you look at :help CTRL-W_j and its friends, you will see that they all have a bunch of alternatives. The important one is <C-w><C-j>, which is what lets you keep your left pinky on Ctrl while you press j with the right index or keep your left pinky and index on Ctrl and w while you press j with the right index.
Therefore:
[...]
nnoremap <C-w>j <C-w>h
nnoremap <C-w><C-j> <C-w>h
[...]

Alert sound in Vim after placing comments after key mapping in .vimrc

I'm trying map the following keys to switch splits in Vim.
nnoremap <s-j> <c-w>j " Shift + j to switch the split below
nnoremap <s-k> <c-w>k " Shift + k to switch the split above
nnoremap <s-h> <c-w>h " Shift + h to switch the split on the left
nnoremap <s-l> <c-w>l " Shift + l to switch the split on the right
This works well except I hear an alert sound when performing the switch. For example, there is no alert sound when pressing Ctrl+w and then j, but there is an alert for Shift+j.
I suspect there is an error happened, and don't like just turning off the alert sound without understanding.
Maybe the remapping does more than just Ctrl+w + j?
Does any vim expert have some thoughts?
Removing your comments will likely fix this problem. See :help :quote.
Also…
Why <s-j> when you could simply use J?
:help J, :help L, :help H, and :help K are all very useful commands. Are you sure you want to override them?

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

Remapping of vim direction keys is not honored by viewport navigation

I am using colemak and thus I have remapped my direction keys like so
noremap n h
noremap e j
noremap i k
noremap o l
noremap h n
noremap j e
noremap k i
noremap l o
The problem is that these keybindings are not honored by the viewport navigation. Eg. when I press ^w+n I want to shift focus to the viewport to the left but instead a new empty viewport is opened.
How can I help rebinding all the viewport navigation keys individually?
This subject has been somewhat beaten to death over at the colemak forums:
http://colemak.com/forum/viewtopic.php?id=50 (5 pages, with active contributions by the designer of colemak, who (used to be) an avid vim user too).
There are a number of listed links to
keymaps
plugin scripts
I feel Shai's own script is going way too far - doing way more than just integrate Colemak into vim (remapping C-w to Close Tab e.g., showing some bias towards tab-oriented editors, people have complained about in-/exclusive motions (diw, daB etc) not working any more etc).
I guess the best advice, as has been given before is to stick with the defaults and do:
Keyboard bindings for Vim
Vim is an extremely efficient text editor that I use for writing emails and
editing any sort of text file. Vim's commands are all controlled from the
keyboard: 'd' for delete, 'w' to move the cursor forward one word, 'dw' to
delete the text moved over by 'w', '2dw' to delete two words, etc.
Left/Down/Up/Right navigation is located on the QWERTY keys H/J/K/L so that
the typist's hands never need to reach for arrow keys or the mouse.
Unfortunately, these navigation keys are not so intuitive under Colemak and
so I needed to find some more appropriate mappings. After experimenting with
many alternatives, here is what I finally decided on and am very happy with:
noremap n j|noremap <C-w>n <C-w>j|noremap <C-w><C-n> <C-w>j
noremap e k|noremap <C-w>e <C-w>k|noremap <C-w><C-e> <C-w>k
noremap s h
noremap t l
noremap f e
noremap k n
noremap K N
noremap U <C-r>
I think you'll have to map the <C-W> window navigation commands explicitly, by doing something like:
noremap <C-W>n <C-W>h
noremap <C-W>e <C-W>j
noremap <C-W>i <C-W>k
noremap <C-W>o <C-W>l
noremap <C-W>h <C-W>n
noremap <C-W>j <C-W>e
noremap <C-W>k <C-W>i
noremap <C-W>l <C-W>o

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