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.
Related
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
[...]
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>
I am having trouble to activate the following mappings:
nnoremap <buffer> <C-K> <c-w>w
nnoremap <buffer> <C-S-K> <c-w>W
Where, I want have "pressing Ctrl+K in normal mode" to send me to the next buffer (of the same screen), in a clockwise way. <c-w>W in Vim will go the other direction. I want to map this action to be Ctrl+Shift+w.
Any idea why the mapping fails? I found only the latex-suite.vim has a mapping of <c-w> and none <c-s-w> has been mapped. The plugin mapping is buffer only.
Thank you for your input.
All the best,
-Linfeng
With the <buffer> keyword, your mappings only apply to the current buffer. So, when you have these in ~/.vimrc, they aren't effective, and if you type them in, you would need to have the same current buffer visible in multiple window splits. I don't think that's what you want. Instead, define them to be global mappings:
nnoremap <C-K> <c-w>w
nnoremap <C-J> <c-w>W
Edit: I've replaced <C-S-K> with <C-J>, because shifted control sequences are generally not available in Vim, unfortunately.
They could still be overwritten by some filetype plugins using nnoremap <buffer> <C-K>.
Alternative
I'd prefer the following set of directional mappings. They provide more precision, at the cost of wasting more keys:
nnoremap <C-j> <c-w>j
nnoremap <C-k> <c-w>k
nnoremap <C-h> <c-w>h
nnoremap <C-l> <c-w>l
This a common question on the internet, but after a lot of research I still can't do it.
This is what I have on my .vimrc file:
map <up> <nop>
map <down> <nop>
map <left> <nop>
map <right> <nop>
imap <up> <nop>
imap <down> <nop>
imap <left> <nop>
imap <right> <nop>
And I still can move my courser using the arrow keys.
What am I doing wrong ?
You should use noremap and inoremap instead of map and imap, because those disallows nested and recursive use of mappings, so the original meaning of <left>, <right>, ... will be disabled. It needs nocompatible mode.
Explanation in the help of noremap:
Map the key sequence {lhs} to {rhs} for the modes
where the map command applies. Disallow mapping of
{rhs}, to avoid nested and recursive mappings. Often
used to redefine a command. {not in Vi}
There is an opinion that when working in vim you should not use Esc key (use ctrl+c instead) and don't use arrow keys (use h,j,k,l) on you keyboard. But it is difficult to not to use those keys. I thought that there is a way to disable those keys in .vimrc so there will be no other option but to use ctrl+c and hjkl.
I've searched a bit and found a solution on this link.
So I've inserted the following in my .vimrc file:
inoremap <Up> <NOP>
inoremap <Down> <NOP>
inoremap <Left> <NOP>
inoremap <Right> <NOP>
inoremap <Esc> <NOP>
noremap <Up> <NOP>
noremap <Down> <NOP>
noremap <Left> <NOP>
noremap <Right> <NOP>
noremap <Esc> <NOP>
But this does not work. Adding this to my .vimrc breaks my mapping to the
function keys. The another problem is that it does not block the function of arrow keys rather when I press Down in normal mode multiple actions are performed - the cursor goes up one line, the new line is created and the character 'B' is inserted.
How can I disable in my vim 7.2 the cursor keys and Esc key without breaking anything else?
If you're using vim in a terminal you should absolutely not remap Escape. Because of the way keys are handled in vim (and probably terminals in general), remapping it will break all kinds of keys you didn't intend on changing. To see what I mean, do the following.
Open up vim with no startup files: vim -u NONE --noplugin -N.
Enter insert mode.
Press Ctrl-v followed by any of the function keys, such as <F2>.
Notice the sequence that is entered. It very likely begins with ^[ which is a literal Escape.
Now open try the following:
:inoremap <esc> NO ESCAPE FOR YOU
Enter insert mode.
Press any of the function keys, like <F2>.
If the previous sequence showed the escape character as part of the <F2> key press, you'll now see our new string printed to the screen. In fact, now that you have the mapping, try to move around using the cursor keys. You'll probably notice the same bizarre behavior.
In conclusion, don't remap escape, I almost guarantee you will have unexpected consequences.
Here's a non-geeky way of achieving what you want: Crumple pieces of paper to the size of your thumb and tape them to the keys. The moment your finger tries to reach them you'll bump into the paper instead. They'll become a good reminder. Keep them taped there until you stopped bumping into them.
What you had was close:
inoremap <esc> <NOP>
inoremap <Left> <NOP>
inoremap <Right> <NOP>
inoremap <Up> <NOP>
inoremap <Down> <NOP>
nnoremap <Left> <NOP>
nnoremap <Right> <NOP>
nnoremap <Up> <NOP>
nnoremap <Down> <NOP>
This line was causing you trouble:
noremap <Esc> <NOP>