How to unbind ctrl enter in vim? - vim

So I use undotree and I have it bound to ctrl m:
nnoremap <C-m> :UndotreeToggle<CR>
But for some reason, when I press ctrl enter, it also toggles undotree.
And then when I try to unbind ctrl enter like below, it still toggles undotree.
nnoremap <C-CR> <Nop>
So question is, how on earth do I make it ctrl enter not toggle undotree?

From Vim's point of view, <CR> and <C-CR> are indistinguishable. Since <C-m> and <CR> are two representations of the same key, pressing <C-CR> is effectively the same as pressing <C-m>.
See if :help modifyOtherKeys helps.

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.

Canceling searches for an empty string in vim

In my vimrc (I am using Nvim), I have mapped
noremap <C-f> <Esc>
noremap! <C-f> <Esc>
noremap / /\v
noremap ? ?\v
to automatically get very magic regex search. However, I often press / by mistake (or change my mind about searching), and then press C-f (and sometimes Enter) to cancel it. However, this highlights the entire file (since everything matches "\v"). Sometimes I remember to delete twice before pressing C-f, but this jarringly moves me to the next instance of my previous search (an empty search "").
I could press C-c or Esc to cancel the search, but I am not used to it and I feel like I would have to move my fingers in uncomfortable ways to do so. Would it be possible make this simpler and more comfortable? Two solutions I can think of, but don't know how to implement, would be
Remap C-f to work as Esc and C-c while searching.
and
Make some sort of alias or change some setting, so that searching for "\v" (empty regex) or "" (nothing) simply cancels the search.
The answer is simple:
Change your habit of "press Enter to cancel" an operation. Also don't press ctrl-f for searching.
Why you have this strange habit?
pressing Enter key after a command 90% will mean "Launch".
in Vim, ESC would be much more often pressed than Enter also, general speaking, Esc key should be the one to "cancel" an opeartion
what you meant "search nothing" in your comment, actually is not "search nothing", instead, it is "search anything"!
Mapping ctrl-f in normal mode is a bad idea, it does "page down", and ctrl-b for "page up". They are pretty useful functions I think.
Mapping ctrl-f in command/search mode is a bad idea too. ctrl-f brings you to the command window, there you can use full vim normal/insert commands to edit your commands. Very useful.
If you activated 'hlsearch', and want to remove the match highlighting, I have this in vimrc, you can try it:
"clear hl search by pressing ,/
nnoremap <silent> <Leader>/ :noh<cr>
I managed to solve it myself! Apparently, changing
noremap <C-f> <Esc>
noremap! <C-f> <Esc>
to
noremap <C-f> <C-c>
noremap! <C-f> <C-c>
did the trick. I am not quite sure why that is, especially since pressing Esc seems to work equally well to C-c.

What mode is vim in when I press a single 'r' and is it possible to have a mapping here?

For a quick overview, I map <s-space> to <esc> so I can more easily cancel out of things without moving my hand to the escape key. For example, when I want out of insert mode, or to cancel something.
inoremap <esc> <nop> " to force me to stop using <esc>
cnoremap <esc> <nop>
nnoremap <s-space> <nop>
onoremap <s-space> <esc>
inoremap <s-space> <esc>
However, if I press 'r' vim is waiting for a character, and when I press <s-space>, I end up replacing with a space, instead of canceling the replace operation. Is it possible for mappings to work after pressing 'r' once, while waiting for a character?
Thanks!
If you find you've accidentally pressed r, how about just tapping uu?
This will make the replacement but immediately undo it again, without you having to move your hands from the letter keys.

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

Vim - How do I use a key mapping command like <C-L>?

I'm almost certain that someone else has also had this question, and this may be a repeat, but I'm not sure what to call a command like <C-L> and so I haven't had any luck finding an existing question. If this is a duplicate, please redirect me.
The question arises from a vimrc section that reads like this:
" Map <C-L> (redraw screen) to also turn off search highlighting until the
" next search
nnoremap <C-L> :nohl<CR><C-L>
So what combination of keys do I press (in which mode) to input a <C-L> mapping?
in this line:
nnoremap <C-L> :nohl<CR><C-L>
nnoremap means normal no-recursive map <C-L>... which means, if you press ctrl + l in NORMAL mode, the mapped key-strokes would be applied.
<C-L> means ctrl + l
if you type
:h control
you can see the keycodes:
<C-...> control-key *control* *ctrl* *<C-*
The capital "C" character in <C-L> represents the control key while the capital "L" character represents a "L" character. So pressing Ctrl+L in normal mode should invoke the mapping.

Resources