vim using leader together with <C-?> - vim

I found this snippet from unite.vim
nnoremap <leader>t :<C-r>Unite -no-split -buffer-name=files -start-insert file_rec<cr>
It seems it try to map "\t" to "Unite -no-split -buffer-name=files -start-insert file_rec"
what does <C-r> mean here ? what is the difference from the following:
nnoremap <leader>t :Unite -no-split -buffer-name=files -start-insert file_rec<cr>

In Ex mode, Ctrl+R (written <C-r> in Vim) will insert the content of the specified register on the Ex command line. <C-r>U will insert the content of the U register. All commands starting with : are Ex mode commands.
To see this in action, do the following:
Delete a word using "adw. This means "delete a word to register a".
Enter normal mode.
Type in :<C-r>a. Your Ex mode command line will now contain the word.
I have looked at the relevant .vimrc and the unite.vim source, and I doubt the <C-r> is supposed to be there. It is most likely a bug or a typo.
For more information, see :help c_CTRL-R.

Related

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.

Assigning keys to function in insert mode also in vim

I am trying to bind control-F to find dialog box in vim. Following code in .vimrc works:
:map <C-F> :promptfind<CR>
However, it works only in the command mode. How can I set it up so that it also works in insert mode?
In insertmode, your binding will just insert :promptfind and then move to the new line. Use <C-O> to execute a single command-mode operation before going back to insert mode (:help i_ctrl-o), or <Esc> in its place to go to command mode and stay there afterwards.
:nnoremap <C-F> :promptfind<CR>
:inoremap <C-F> <C-O>:promptfind<CR>

Why isn't this Vim mapping working?

noremap :hsp :botright new
noremap :vsp :botright vnew
"Not an editor command: hsp"
I'm probably googling the wrong thing, but I can't find many results on aliasing vim commands. I can find tons of info about mapping keys to commands like my one for tabs:
noremap <C-t> :tabnew<CR>
But can't find commands mapped to other commands.
What you're doing is simulating a command with a mapping. You're saying that when you press the 4 keys :hsv in normal mode, it should type out the keys :botright new (which would need a <CR> to run, as others have said), but it's not actually making the command hsv. You could make an actual command with a user command (:h user-commands). These must start with a capital letter, though.
:command Hsp botright new
:command Vsp botright vnew
Now you can type :Hsp and hit enter to run the command botright new.
Did you try command abbreviation?
ca hsp botright new
ca vsp botright vnew
You will have to initialize the expansion of the abbreviation by hitting the space key afterwards. Depending on the global vim configuration, expansion also happens automatically just when enter is pressed.
with your same mapping, I cannot get the Not an editor command: hsp error message with my vim (v7.4).
Your mapping works fine, but you don't have <cr> at the end, so when you press :hsp in normal mode, your mapping will switch to commandline mode, and put the mapped command there, without executing it. You have to manually press Enter.
#XZS's answer works, but keep in mind that it is an abbreviation(ab), not a mapping. ab is not command aliases, it is not exactly same as mapping. For example, you have to press another key (like space) after hsp to trigger the ab. also, you cannot ab some special keys, this would be another limitation of ab.
There is c(nore)map for command mapping.
e.g. you could have:
cnoremap hsp botright new
with above line, as same as your original one, you have to manually press Enter, if you want it to be executed, you need add <CR> at the end of the line.
I think if I do this, I would create mapping.
Creating command alias can be tricky:
Using a simple cabbrev and/or cmap will cause expansions and mappings to fire in unexpected places like during searches with / and in the middle of filenames.
cmap's will have a visible delay in outputting to the screen which is why cabbrev is often used.
However there are a few ways to create a proper alias:
Create a command via :command.
e.g. command W w
command's first letter must be a uppercase letter
must supply -nargs, -bar, -complete, and -range options according to the needs of your alias
Expression :cabbrev to guard the abbreviation from expanding in in proper places.
expression mapping use the <expr> option
verify getcmdtype() is equal to :
verify the abbreviation is at the beginning of command line via getcmdline() or getcmdpos()
e.g. cnoreabbrev <expr> W getcmdtype() == ':' && getcmdline() ==# 'W' ? 'w' : 'W'
Use :Alias via the cmdalias.vim plugin by Hari Krishna Dara
e.g. Alias W w
uses an expression cabbrev under the covers similar to the technique above

Why won't Vim use my key mapping when I start a new session?

After mapping the F2 key in Vim in the following sequence, it does not work in a new Vim session:
:map <F2> :ls<CR>
Pressing F2 here displays the files.
:mkexrc!
Close Vim.
Reopen Vim.
Pressing F2 now does nothing.
The mapping is visible in the _exrc file as:
map <F2> :ls
What am I missing?
I think that you are simply missing a literal <CR>. Type this command in the command line followed by Enter.
:map <F2> :ls<CR>
If it doesn't work…
See if that _exrc file is executed by Vim: :scriptnames should display it near the top.
If it is executed, see if <F2> is mapped elsewhere: :verbose map <F2>.
Anyway, Vim is not Vi. You should use :mkvimrc!. Or a normal $HOME/_vimrc.
edit
FYI I just created a ~/.exrc file using :mkexrc! and it's not executed by Vim at startup. You should definitely use a normal $HOME/_vimrc.

How to create a mapping for Insert mode but not for the autocomplete submode in Vim?

I have these insert mode mappings in my .vimrc file:
imap <C-e> <C-o>A
imap <C-a> <C-o>I
They make Ctrl-A and Ctrl-E move the cursor to the start and end of the line without leaving insert mode, a la emacs keybindings.
However, I just realized that the Ctrl-E mapping introduces a conflict with the autocompletion submode. The documentation in :help complete_CTRL-E states:
When completion is active, you can use CTRL-E to stop it and go back to the originally typed text.
Thus, my Ctrl-E mapping interferes with this. Is there a way that I can make Ctrl-E jump to the end of the line only if auto-completion is not active?
There is no proper way to test whether the
Ctrl+X-completion mode is active or not.
However, the following two workarounds are possible.
1. If one uses the popup menu to choose from the list of available
completions (especially in the case of menuone set in the completeopt
option), an acceptable solution might be the mapping
inoremap <expr> <c-e> pumvisible() ? "\<c-e>" : "\<c-o>A"
2. A general solution can be based on a side effect: In the
completion submode, it is disallowed to enter Insert mode recursively
(see :helpgrep Note: While completion), so if an attempt to do so
fails, we can suppose that we are in the midst of a completion:
inoremap <c-e> <c-r>=InsCtrlE()<cr>
function! InsCtrlE()
try
norm! i
return "\<c-o>A"
catch
return "\<c-e>"
endtry
endfunction

Resources