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

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.

Related

Vim: Remap Ctrl-W in insert mode to behave as it does in normal mode

I would like Ctrl-W to allow me to switch windows even when I am in insert mode. How can I make this change?
My motivation is to not need to press escape before shifting windows.
For <c-w><c-j> (as an example), you can do:
inoremap <c-w><c-j> <esc><c-w><c-j>gi
Then you can repeat this kind of mapping for every command you use:
inoremap <c-w><c-k> <esc><c-w><c-k>gi
inoremap <c-w><c-w> <esc><c-w><c-w>gi
inoremap <c-w>+ <esc><c-w>+gi
inoremap <c-w>- <esc><c-w>-gi
...
If you choose this simple solution, then you can finally add this mapping to inhibit the native <c-w> key (= delete the last word):
inoremap <c-w> <nop>
More "smart" solutions could be written, but they would imply a bit more code.
Note 1: as noted in the comments, the mappings to choose depend on which mode you want to reach after the keystroke: the suffix gi in the commands above means that you want to go back to insert mode in the new window; but you can remove this suffix if you want to be in normal mode.
Note 2: the suffix gi could be simply i, depending on the case : see :h i and :h gi

Vim - Command Line - previous and next command key mapping

When opening the command line and pressing the up arrow or down arrow keys, it shows the commands there were typed the last time. Is there a way to map this behaviour? For example when I press ctrl p, I want vim to show me my previous command (make vim act as if I pressed the up arrow). The same thing for ctrl n for the next command.
How can I make this happen?
The CTRL-P and CTRL-N keystrokes already do what you want, they search your command history. See :help c_CTRL-P, which explains how it will "recall older command-line from history."
The way CTRL-P and CTRL-N work differs slightly from the up and down arrow, in that the arrows will only go through the items in history that start with the characters you typed. So :e, space, up arrow will go to the last command you used to open a file for editing. See :help c_<Up> for details.
You can remap them so that they do the same as their counterpart, by using the cnoremap command, which creates mappings for keystrokes typed while in the Vim command-line.
For example, to make CTRL-P and CTRL-N behave the same as the arrows (complete respecting the prefix), you can use the following commands to create a (somewhat naive) mapping:
cnoremap <C-P> <Up>
cnoremap <C-N> <Down>
The shortcoming of this approach is that CTRL-P and CTRL-N behave differently on a wildmenu, so a more complete mapping would be:
cnoremap <expr> <C-P> wildmenumode() ? "\<C-P>" : "\<Up>"
cnoremap <expr> <C-N> wildmenumode() ? "\<C-N>" : "\<Down>"
That will preserve the original behavior of CTRL-P and CTRL-N in the wildmenu.

What the D-S-Up means in vim?

I find a .vimrc file config:
" Move selection up/down (add =gv to reindent after move)
:vmap <D-S-Up> :m-2<CR>gv
:vmap <D-S-Down> :m'>+<CR>gv
I know the D-S-Up must be a key, but what is the key?
I type:
:help D-S-Up
nothing happened
:help key-notation tells you the meaning of all those <key> notations.
You can't expect :help D-S-Up to do anything because:
it doesn't follow established patterns like i_ctrl-r,
it is a custom mapping and Vim only provides documentation for its own commands.
<D> is the Cmd key on Mac keyboards, <S> is the Shift key, and <Up> is the Up arrow key.
So <D-S-Up> means Cmd + Shift + Up.
The Cmd key only works in the MacVim GUI.
Non-portable mappings are worthless.
One should use :xmap or :xnoremap for visual mode mappings, not :v*.
Non-recursive mappings should be used by default unless one really wants recursion.
Using someone else's vimrc is a bad idea.
By the way, here are enhanced versions of those mappings:
nnoremap <silent> <D-S-Up> :<C-u>move-2<CR>==
nnoremap <silent> <D-S-Down> :<C-u>move+<CR>==
xnoremap <silent> <D-S-Up> :move-2<CR>gv=gv
xnoremap <silent> <D-S-Down> :move'>+<CR>gv=gv
where:
<silent> prevents the commands in the mapping from echoing useless info,
<C-u> removes any default range inserted by Vim,
move-2<CR> is a more readable version of m-2<CR>,
== re-indents the line,
gv=gv reselects the lines, re-indents them, and re-selects them again.
Have a look at Move entire line up and down in Vim
In an answer you can read (concerning the line :vmap <D-S-Up> :m-2<CR>gv):
According to the shortcuts above, pressing Cmd-Shift-Up/Down will shift the block selection up/down. "D" is the Command key in MacVim, for Windows try "C" (Control), or "A" (Alt) (eg. would be Control Alt f).
Since I don't have a Mac myself, I can't check, but it's most certainly true.

Map backspace to remove search highlight, and normal action when there is no highligted search in vim

I normally do this:
nnoremap <silent> <backspace> :noh<CR>
to remap the backspace button to remove the highlighting from a search, however I would like it so the default action of the backspace button is performed if there is no highligted search. How do I perform this?
You can determine if search highlight is currently on by variable v:hlsearch, thus, with a <expr> mapping, your goal can be achieved. Try this mapping:
nnoremap <expr> <BS> v:hlsearch?':noh<cr>':'<BS>'

Remapping <C-a> in Vim causes jumping

Long ago I remapped tmux' C-b to C-a, to save my left index finger from becoming grotesquely elongated.
Now I find I need to remap vim's C-a to something else as a result. I'm trying to remap it to C-i, with:
nnoremap <C-i> <C-a>
Which works, but when I press C-i on a number, the cursor jumps down a line after incrementing said number.
How is one supposed to remap a key properly?
Instead, you should
nn <C-i> <C-a>
which make ctrl-i behave like ctrl-a.
As to recover the default behavior of ctrl-a, use
nn <C-a> <C-a>

Resources