Where to find a list of special character representations? - vim

For example backspace is <BS> and tab is <Tab>, but where can I look them up if I don't know or remember the sequence for, say, the up or down arrow?

Two :helpful tricks:
In insert mode and in the command line, hitting <C-v> followed by some key inserts that key's internal notation.
For example,
:helpCtrl+v↑
produces
:help <Up>
The :help command supports completion so you can type a keyword related to what you want and hit <Tab> or <C-d>:
:help key<Tab>

I was going to ask this, but then I found the answer by random luck in the help files. So here it is for those who are looking for the same:
:help keycodes

Related

What's the quickest way to pull up Vim's quickref page?

What is the simple key combination in Vim to bring up the quickref page, or at least a page that is functionally similar i.e. it contains a list of commands and their key bindings?
After manhandling Google for 15 minutes, I can't find anything shorter than :h quickref.
Not exactly the same as :help quickref but
:viu[sage]
opens :help visual-index and
:exu[sage]
opens :help ex-cmd-index.
I couldn't find a way to do it, but you could remap a key.
I personally think the F1 key for the default help page is useless to me. Maybe you are in the same boat and could remap it to the quickref.
noremap <F1> :h quickref<CR>

Vim pymode: meaning of <C-c> key combination

I have come across the following key combinations(...i assume) in vim pymode documentation. <C-c>, <C-C>, <C-X><C-O>, <C-P>/<C-N>. How are they to be interpreted?
<C-c> and <C-C> both mean Ctrl+C.
I'm sure you can infer how to type the others.
See :help key-notation.

What is the meaning of a <CR> at the end of some vim mappings?

I see <cr>s a lot in vim mappings, but what does it do?
:help key-notation
says:
notation meaning equivalent decimal value(s)
-----------------------------------------------------------------------
<CR> carriage return CTRL-M 13 *carriage-return*
<Return> same as <CR> *<Return>*
<Enter> same as <CR> *<Enter>*
Mappings often involve Ex commands and you must press <CR> to execute them so it's included in the mapping.
Why <special keys>?
While you can use literal keys in mapping definitions (the Enter key would appear as ^M, or even just as an additional new line, depending on the settings), Vim provides a special key notation for key (combinations), so that it is easier to define (you don't have to use i_CTRL-V to literally insert the special character) and understand (<A-a> better expresses the intention than the equivalent á) the mappings.
See :help key-notation for a list and explanation.
Why <CR>?
As many mappings invoke Ex commands (e.g. :w) and therefore have to switch from normal to command-line mode, they have to conclude the command with <Enter> (or <CR>), just as you would when manually typing the command.
The <CR> in vim mappings is the carriage return usually the Enter on your keyboard.
<CR> in a mapping corresponds to the Enter key just like a in a mapping corresponds to the A key. Ley's say you have this mapping
:map <f8> :wq<cr>
This will map F8 to the key sequence :WQEnter (which would save the current buffer and quit).
It's basically a means to say "this is the end", see map:
When you have a mapping that contains an Ex command, you need to put a line
terminator after it to have it executed. The use of is recommended for
this. Example:
:map _ls :!ls -l %<CR>:echo "the end"<CR>
also,
<CR> [count] lines downward, on the first non-blank character |linewise|.

How to jump to a search in a mapped :normal command?

What do you need to properly jump to a matched search result?
To reproduce, make a macro with a search in it after you've run vim -u NONE to ensure there's no vimrc interfering. You'll need to make a file with at least 2 lines and put the cursor on the line without the text TEST_TEXT.
map x :norm gg/TEST_TEXT^MIthis
My intention is that when I press x, it goes to the top of the file, looks for TEST_TEXT and then puts this at the start of the line that matches the search. The ^M is a literal newline, achieved with the CtrlQ+Enter keypress. What's happening instead is either nothing happens, or the text gets entered on the same line as when I called the macro.
If I just run the :norm gg/TEST_TEXT^MIthis command without mapping it to a key, the command executes successfully.
I had an initially longer command involving a separate file and the tcomment plugin, but I've gotten it narrowed down to this.
What is the correct sequence of keys to pull this off once I've mapped it to a key?
The problem is that the ^M concludes the :normal Ex command, so your search command is aborted instead of executed. The Ithis is then executed outside of :normal.
In fact, you don't need :normal here at all. And, it's easier and more readable to use the special key notation with mappings:
:map x gg/TEST_TEXT<CR>Ithis
If you really wanted to use :normal, you'd have to wrap this in :execute, like this:
:map x :exe "norm gg/TEST_TEXT\<lt>CR>Ithis"<CR>
Bonus tips
You should use :noremap; it makes the mapping immune to remapping and recursion.
Better restrict the mapping to normal mode, as in its current form, it won't behave as expected in visual and operator-pending mode: :nnoremap
This clobbers the last search pattern and its highlighting. Use of lower-level functions like search() is recommended instead.
There are many ways of doing this however this is my preferred method:
nnoremap x :0/TEST_TEXT/norm! Itest<esc>
Explanation:
:{range}norm! {cmd} - execute normal commands, {cmd}, on a range of lines,{range}.
! on :normal means the commands will not be remapped.
The range 0/TEST_TEXT start before the first line and then finds the first matching line.
I have a few issues with your current mapping:
You are not specifying noremap. You usually want to use noremap
It would be best to specifiy a mode like normal mode, e.g. nnoremap
It is usually best to use <cr> notation with mappings
You are using :normal when your command is already in normal mode but not using any of the ex command features, e.g. a range.
For more help see:
:h :map
:h :norm
:h range
try this mapping:
nnoremap x gg/TEST_TEXT<cr>Ithis<esc>
note that, if you map x on this operation, you lost the original x feature.

Searching vim history for matching first characters

In bash, adding the lines
"\e[B": history-search-forward
"\e[A": history-search-backward
to my .inputrc, allows me to search the history for expressions that begin with the characters in front of my cursor by using the <page-up>/<page-down> keys.
Can I achieve something similar in vim?
I already know about the possibility of opening a history window with q: and performing even complex searches there, but I am looking for a simple solution for the simplest case of history search.
Thank you!
This is built-in as <Up> and <Down>.
Of course, you can customize this, e.g.:
:cnoremap <PageUp> <Up>

Resources