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

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
[...]

Related

switching windows with <leader> instead of ctrl

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.

How to remap arrow keys in Vim in insert mode?

When I am in insert mode I want to use the keys h, j, k and l to behave as the arrow keys.
I want to achieve this by holding the ctrl key and then ctrl + k would move the line upwards in insert mode. Since these four keys are already mapped to move the lines when not in insert mode, how can I achieve this behavior when I am in insert mode?
You can use imap for that like this:
inoremap <C-h> <Left>
inoremap <C-j> <Down>
inoremap <C-k> <Up>
inoremap <C-l> <Right>

How to unbind h and l in vim?

I'm learning vim and I would like to unbind h and l (left and right movements).
What should I include in my .vimrc?
Note: By doing that I'm forcing myself to use w, W, b, B, e, and other movements to move horizontally.
Use nop to suppress the effect of a command:
nnoremap h <NOP>
nnoremap l <NOP>
You could study/enhance the "dogmatic.vim: Arrow keys are heretical" plugin, which not only disables the keys but also displays a message to remind you. You would need to duplicate the following lines:
noremap <silent> <right> <Esc>:call dogmatic#TrackArrowPress('right')<cr>
noremap <silent> <left> <Esc>:call dogmatic#TrackArrowPress('left')<cr>
noremap <silent> <up> <Esc>:call dogmatic#TrackArrowPress('up')<cr>
noremap <silent> <down> <Esc>:call dogmatic#TrackArrowPress('down')<cr>
If you follow this approach you could consider providing a patch/pull request to the plugin author, possible by creating an option which allows the plugin user to enable/disable your modifications.

Mapping <Shift>-Arrows to selecting characters/lines

I started to use vim recently, but I miss the character/line selection methods from other text editors. By default vim maps <S-Up>, <S-Down> to jumping one page up/down and I want to remap these to text selection.
Is there a way to do that?
I completed #escrafford mapping with insert mode's ones:
" shift+arrow selection
nmap <S-Up> v<Up>
nmap <S-Down> v<Down>
nmap <S-Left> v<Left>
nmap <S-Right> v<Right>
vmap <S-Up> <Up>
vmap <S-Down> <Down>
vmap <S-Left> <Left>
vmap <S-Right> <Right>
imap <S-Up> <Esc>v<Up>
imap <S-Down> <Esc>v<Down>
imap <S-Left> <Esc>v<Left>
imap <S-Right> <Esc>v<Right>
Also mapping usual copy/cut/paste like this you can return to insert mode after select+copy, for example.
vmap <C-c> y<Esc>i
vmap <C-x> d<Esc>i
map <C-v> pi
imap <C-v> <Esc>pi
imap <C-z> <Esc>ui
Now you can start a shift+arrow selection from any mode, then C-c to copy, and then C-v to paste. You always end in insert mode, so you have also C-z to undo.
I think this approaches more to the 'expected standard' behaviour for a text editor yu are asking for.
There's an specific option for this: keymodel:
'keymodel' 'km' string (default "")
global
{not in Vi}
List of comma separated words, which enable special things that keys
can do. These values can be used:
startsel Using a shifted special key starts selection (either
Select mode or Visual mode, depending on "key" being
present in 'selectmode').
stopsel Using a not-shifted special key stops selection.
Special keys in this context are the cursor keys, <End>, <Home>,
<PageUp> and <PageDown>.
The 'keymodel' option is set by the |:behave| command.
TL;DR: To enable the behavior you want, use:
set keymodel=startsel
If you also want to leave visual mode when using <Up> or <Down> without <Shift> pressed, you can use:
set keymodel=startsel,stopsel
Slightly different from progo's answer - this gives the same feel as mac apps normally have:
nmap <S-Up> v<Up>
nmap <S-Down> v<Down>
nmap <S-Left> v<Left>
nmap <S-Right> v<Right>
vmap <S-Up> <Up>
vmap <S-Down> <Down>
vmap <S-Left> <Left>
vmap <S-Right> <Right>
The differences being switch to visual mode instead of visual line mode, and not losing the initial up/down etc keystroke.
Vim doesn't bend to that easily in my opinion. The terminal one doesn't even recognize Shift-Up in my case! I thought the v (character-wise selection) or V (line-wise selection) was among the easier concepts to learn about vi/vim.
If this works (can't test right now), this is something you'll want:
" activate visual mode in normal mode
nmap <S-Up> V
nmap <S-Down> V
" these are mapped in visual mode
vmap <S-Up> k
vmap <S-Down> j
"
" etc...
" similarly <S-Left>, <S-Right> for v
I found another solution that is easier to execute. The command ':behave mswin' does all that is needed to use shift plus cursor keys to select text. Works from any mode. It also supports Cmd-c, Cmd-v and Cmd-x. It works in MacVim but I did not try other platforms.
It is definitely recommended that you don't remap this feature. Simply switching to visual mode and using v and the arrow keys is a better idea. V will select the entire line, v$ will select to the end of the line and vw will select the next word. There are many more commands you can use to select different lines and words. Learning these commands will not only be useful for selecting but also useful for editing your files more efficiently.
This mapping keeps insert mode during selection (visual mode) and it starts on the correct position. You can also select a word to the left or right using Ctrl-Shift-Left/Right (if your terminal supports it):
" Select with shift + arrows
inoremap <S-Left> <Left><C-o>v
inoremap <S-Right> <C-o>v
inoremap <S-Up> <Left><C-o>v<Up><Right>
inoremap <S-Down> <C-o>v<Down><Left>
imap <C-S-Left> <S-Left><C-Left>
imap <C-S-Right> <S-Right><C-Right>
vnoremap <S-Left> <Left>
vnoremap <S-Right> <Right>
vnoremap <S-Up> <Up>
vnoremap <S-Down> <Down>
" Auto unselect when not holding shift
vmap <Left> <Esc>
vmap <Right> <Esc><Right>
vmap <Up> <Esc><Up>
vmap <Down> <Esc><Down>
This may be useful for quickly selecting small parts when you're in insert mode but I recommend using the default commands for selecting larger parts.
I've written this to be able to navigate using Alt+hjkl (and friends) and select using Alt+HJLK when both in insert, visual and normal mode.
So the same can be applied to normal arrow keys as well
let hjklfriends = ['h','j','k','l','w','e','b','W','E','B', 'n', 'N', 'y', 'Y', 'p', 'P']
" define if using alt (it works in neovim) or Escape key.
function! Meta(key)
if has('nvim')
return "<A-" . a:key . ">"
else
return "<Esc>" . a:key
endif
endfunction
execute 'noremap! ' . Meta('h') . ' <left>'
execute 'noremap! ' . Meta('j') . ' <down>'
execute 'noremap! ' . Meta('k') . ' <up>'
execute 'noremap! ' . Meta('l') . ' <right>'
execute 'noremap! ' . Meta('b') . ' <C-Left>'
execute 'noremap! ' . Meta('w') . ' <C-Right>'
execute 'noremap! ' . Meta('e') . ' <C-Right>'
for k in hjklfriends
execute 'imap ' . Meta(k) . ' <C-o>' . k
if k =~ '[a-z]'
execute 'imap ' . Meta(toupper(k)) . ' <C-o>v' . k
execute 'vmap ' . Meta(toupper(k)) . ' ' . k
execute 'nmap ' . Meta(toupper(k)) . ' v' . k
endif
endfor
Modified from #RubenCaro's answer.
The issue is: when escaping from insert mode, the cursor will be shifted to the left by one. This makes the behaviour of the keys , , and in insert mode different from the behaviour of other generic text editors.
Assuming that the goal is to make those keys behave like generic editor, the mapping should be slightly modified to:
" shift+arrow selection
nmap <S-Up> v<Up>
nmap <S-Down> v<Down>
nmap <S-Left> v<Left>
nmap <S-Right> v<Right>
vmap <S-Up> <Up>
vmap <S-Down> <Down>
vmap <S-Left> <Left>
vmap <S-Right> <Right>
imap <S-Up> <Esc>v<Up>
imap <S-Down> <Esc>vlvv<Down>
imap <S-Left> <Esc>v<Left>
imap <S-Right> <Esc>vlvv<Right>
vmap <C-c> y<Esc>i
vmap <C-x> d<Esc>i
map <C-v> pi
imap <C-v> <Esc>pli
imap <C-z> <Esc>ui

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

Resources