Trouble remapping <c-h> in vim normal mode - vim

I have the following in my .vimrc
nnoremap <c-h> zh
nnoremap <c-j> <c-e>
nnoremap <c-k> <c-y>
nnoremap <c-l> zl
and nmap reports the following mappings
n <C-H> * zh
n <NL> * <C-E>
n <C-K> * <C-Y>
n <C-L> * zl
The main problem I have is that <c-h> hasn't actually been remapped to zh. <c-h> still acts as the default behavior, which just moves the cursor left.
I've done this in a clean .vimrc file so it's not due to a plugin collision. This is happening with vim 7.4 on a redhat 7.2 system that I remote into from putty. I've tried to replicate this on my windows machine using WSL but it works as expected there.
The minor problem is the second row of nmap, why does it say <NL> instead of <C-J> The mapping seems to be behaving as expected.

Related

Vim <A-j> Keybinding for 10j moves cursor to the right

I've recently mapped 10j to <A-j> and 10k to <A-k>, which is seemingly quite amazing, but there is one problem with it:
When I normally type 10j (not using the shortcut), it will just move 10 rows down vertically but not move horizontally at all (given the lines have the same length), but when I use <A-j> it will always (well, interestingly enough, not always, but most of the times) also move one letter to the right.
Funnily enough, this happens only for <A-j>, whereas <A-k> works as intended. How can I prevent that? And maybe most importantly: Why is that?
If it helps, these are my other keybindings:
nnoremap K K<C-w>L
nnoremap <A-h> :set hls!<cr>
nnoremap / :set hlsearch<cr>/
nnoremap <A-j> 10j
nnoremap <A-k> 10k
nnoremap <A-w> W
nnoremap <A-b> B
nnoremap <A-v> V
nnoremap <A-m> '
nnoremap <A-p> "+p
nnoremap <A-y> "+y
nnoremap <A-4> $
nnoremap <A-3> 0
nnoremap Y y$
vnoremap <A-h> :set hls!<cr>
vnoremap / :set hlsearch<cr>/
vnoremap <A-j> 10j
vnoremap <A-k> 10k
vnoremap <A-w> W
vnoremap <A-b> B
vnoremap <A-v> V
vnoremap <A-m> '
vnoremap <A-p> "+p
vnoremap <A-y> "+y
vnoremap <A-4> $
vnoremap <A-3> 0
Yeah, I like the alt-key a lot.
You have a trailing space character at the end of your mapping:
:nnoremap <A-j>
n <M-j> * 10j<Space>
<Space> is the same command as l; it moves a character to the right (where possible).
The right-hand side in a mapping is taken literally (up to the end of the line or a | command separator). Another common mistake is appending a " comment to a mapping definition.
Plugin recommendations
If you regularly stumble over trailing whitespace (it's generally frowned upon in many coding styles, and tools like Git also highlight it as problematic), my ShowTrailingWhitespace plugin can alert you to those, and the DeleteTrailingWhitespace plugin can remove them for you. (The plugin pages have links to alternative plugins.)

Vim: mapping to switch window and fill screen

I have in my .vimrc the following lines, which lets me switch windows with ctrl+hjkl:
nnoremap <C-h> <C-W>h
nnoremap <C-j> <C-W>j
nnoremap <C-k> <C-W>k
nnoremap <C-l> <C-W>l
These are fine for my desktop computer, but on my netbook, I want to have the active window completely fill the tiny screen. This means typing ctrl+w _ and ctrl+w | after each window change. The logical step would be to add those keystrokes to the mapping, yielding:
nnoremap <C-h> <C-W>h<C-W>_<C-W>|
nnoremap <C-j> <C-W>j<C-W>_<C-W>|
nnoremap <C-k> <C-W>k<C-W>_<C-W>|
nnoremap <C-l> <C-W>l<C-W>_<C-W>|
But that fails, consistently, when in a mapping, despite working when I simply type the required keys; and (as I have set 'showcmd') it seems to leave a trailing <C-W>.
I have also tried using :wincmd:
nnoremap <C-h> :wincmd h<cr>:wincmd _<cr>:wincmd |<cr>
nnoremap <C-j> :wincmd j<cr>:wincmd _<cr>:wincmd |<cr>
nnoremap <C-k> :wincmd k<cr>:wincmd _<cr>:wincmd |<cr>
nnoremap <C-l> :wincmd l<cr>:wincmd _<cr>:wincmd |<cr>
But that complains about trailing <cr> whenever my vimrc is sourced, so I'm not going to pursue that further without more research.
Any ideas?
Try using <Bar> instead of |. ie:
nnoremap <C-h> <C-W>h<C-W>_<C-W><Bar>
nnoremap <C-j> <C-W>j<C-W>_<C-W><Bar>
nnoremap <C-k> <C-W>k<C-W>_<C-W><Bar>
nnoremap <C-l> <C-W>l<C-W>_<C-W><Bar>
| are used to have multiply commands on one line and you will need to be escaped with a backslash when used literally:
nnoremap <C-h> <C-W>h<C-W>_<C-W>\|
nnoremap <C-j> <C-W>j<C-W>_<C-W>\|
nnoremap <C-k> <C-W>k<C-W>_<C-W>\|
nnoremap <C-l> <C-W>l<C-W>_<C-W>\|
On the other hand | can be useful:
nnoremap xxx :if 1 == 2 | echom "hello" | endif

Vim map failed unless reload .vimrc

I want to do the following maps in my .vimrc.local
```
inoremap <C-H> <Left>
nnoremap <C-H> <C-W><C-H>
inoremap <C-J> <Down>
inoremap <C-K> <Up>
inoremap <C-L> <Right>
```
When I restart my vim, the fist two mapping failed, and the last three commands succeeded. However, when I source ~/.vimrc.local, then all the five mapping methods are OK. That means, I need to load my .vimrc.local myself.
I've already write source ~/.vimrc.local in my .vimrc . And if I source ~/.vimrc rather than .vimrc.local, it works as well.
What's wrong with my mapping? Is there any conflict? I searched in my .vimrc and .vimrc.local, but didn't find conflicts.
I'm using OS X 10.9 and MacVim Snapshot 71, and the main .vimrc from spf13. There are my .vimrc and .vimrc.local.
How can I do the mapping without manually reload .vimrc?
Thanks!

Vim nmap :q to :Q not working [duplicate]

This question already has answers here:
Remapping :Wq to :wq in vim
(3 answers)
Closed 8 years ago.
I have the following keymappings in my vimrc
nmap :Q<CR> :q<CR>
nmap :W<CR> :w<CR>
nmap :WQ<CR> :wq<CR>
However, they do nothing. Vim complains that Q isn't a valid command. Same for W and WQ. I restarted vim and everything. I'm trying to figure this out and I'm also hoping to map Ctrl+W and the arrow keys to just Ctrl+h for changing windows left and etc. How can I go about doing that?
For your second question:
nnoremap <C-h> <C-w>h
nnoremap <C-j> <C-w>j
nnoremap <C-k> <C-w>k
nnoremap <C-l> <C-w>l

How do you check when a key is mapped in vim?

I have a few mappings in vim for moving between splits set in my .vimrc,
nnoremap <C-j> <C-w>j
nnoremap <C-k> <C-w>k
nnoremap <C-h> <C-w>h
nnoremap <C-l> <C-w>l
All of these work except for <C-j>, which I suspect is being remapped in one of my plugins. I'd like to find out where, but I'm not sure how. Is there a way to find "where was <C-j> last mapped?"
:verbose nnoremap <c-j>
should help.

Resources