How to disable vim built-in keyword autocompletion? - vim

It's quite often that I accidentally hit Ctrl-P when I want to hit Ctrl-[ for esc from insert mode on my Macbook Pro which has no physical esc key. Then vim's keyword autocomplete menu will popup and insert some gibberish in my file. It's simply annoying. How do I disable vim's built-in keyword autocompletion? Thanks a lot.

To disable ctrl-p in insert mode, the following line can be added to your .vimrc, which maps ctrl-p to a no-op with inoremap. More details on that command are available with :help inoremap (or on a prior Stack Overflow question).
inoremap <c-p> <nop>
If you'd prefer to remap ctrl-p to ctrl-[—as you've specified that as your intended keypress—the following can alternatively be added to your .vimrc.
inoremap <c-p> <c-[>
In either case, you'll still be able to access Vim's completion functionality with ctrl-n, although this works differently than ctrl-p, as it searches for the next match as opposed to the previous match.

Related

vim: Tagbar windows opened when typing <Enter> in normal mode

When typing <Enter> in normal mode in Vim with the Tagbar plugin installed, the Tagbar window is opened automatically. I want to disable this functionality. What must I do?
put the flowing code in you .vimrc
unmap <cr>
Your mapping for <C-m> is actually the cause of the Enter key opening Tagbar. If you remove that map from your vimrc, the enter key will no longer trigger :TagbarToggle.
Mappings for <C-m> and <CR> (Enter) are synonymous in Vim:
The following table shows the mapping between some of the keys on the keyboard and the equivalent Ctrl-key combination:
Ctrl-I Tab
Ctrl-[ Esc
Ctrl-M Enter
Ctrl-H Backspace
If you use one of the Ctrl-key combination in the above table in a map, the map also applies to the corresponding key. Both the keys produce the same key scan code. For example, if you create a map for CTRL-I, then you can invoke the map by pressing Ctrl-I or the Tab key.
This means when you set nmap <C-m> :TagbarToggle<CR>, it is the same as
also setting nmap <CR> :TagbarToggle<CR>.
You'll probably want to choose a new key instead of M. The alternative is to
change the key code sent by <C-m> at the operating system level with some
remapping program.
The terminal key bindings come from readline, the program that processes input text in
your terminal prompt. A full list of key bindings is in the readline
manual.
You can find more info about Vim key codes by typing :help keycodes in Vim, or reading the help docs here.
Try :help tagbar to open the documentation. It sounds like you might have a mapping in your vimrc file that says something like
nnoremap <silent> <CR> :TagbarToggle<CR>
or
nnoremap <silent> <CR> :TagbarOpen<CR>
if you find and remove that mapping will no longer open Tagbar

What does TAB do in vim?

I normally don't write tabs, so I mapped <TAB> to <ESC> in insert mode so that it's faster to leave insert mode. However, when I accidentally press TAB in normal mode, it jumps to some location in the file, without saving the jump location. I first suspected that a plugin is doing that, but I find no mapping for TAB in normal mode. Even explicitly unmapping it doesn't help, so it must be a built-in command. What does it do, why isn't this jump saved to history, and how do I disable it?
You're seeing the built-in :help CTRL-I; <C-I> and <Tab> are the same in (G)Vim. That command is the opposite of <C-O>; it goes to a newer position in the jump list. You don't see the "saving to history", because it operates on existing jumps.
If you don't want any action on Tab in normal mode, just put
:nnoremap <Tab> <Nop>
into your ~/.vimrc; :help <Nop>.
What does it do, why isn't this jump saved to history, and how do I disable it?
"Tab" (or <C-I>, which is really the same thing) goes forward through the jumplist ("undo" for <C-O>), so it's either no-op, or still there's nothing to save ;-) To see the embedded help topic use :h <tab>
If you really want to disable it, just remap it:
nnoremap <tab> <nop>

Adding to mapping rather than completely remapping

I'm working on a plugin to allow bracket completion (I know it's available, it's more of a learning exercise). To properly implement it, I need to add to the backspace mapping. However, as it's an important key, I'd rather keep the existing functionality and just add to it rather than reimplementing the functionality. The steps would basically be when in insert mode and press backspace, execute the original backspace key, then check for some conditions and maybe remove more characters.
I've tried something like imap <backspace> <backspace><call_func_here>, but that doesn't seem to work. Again, I know I could remap backspace to just the function and try to recreate the backspace functionality, but I'd prefer to not do that.
Is this possible in vim?
I think what you are trying to do is the following:
inoremap <silent> <BS> <BS><C-o>:call MyFunction()<CR>
inoremap allows to create a non recurrent mapping in insert mode (it is often a good idea to use nore in your mappings). :h :inoremap
<silent> precise that the mapping will not be echoed on the command line (You will not see :call MyFunction() in the command line) :h :map-silent
<BS> is the reference to the backspace key that you want to remap.
The second <BS> is here to issue a backspace in insert mode
<C-o> switches to normal mode for only a command. :h i_CTRL-O
:call MyFunction() is the call to your function the way you would do it in normal mode.
<CR> correspond to the Enter key which validate the call to your function.

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

Vim: command-line grep-like search with Ctrl-P and Ctrl-N

Basically, I don't want to use my arrow keys completely.
But for that to happen the Ctrl-P needs
to have the same functionality in
the command-line as my Up arrow key.
For Example, I type
:so
then I press Up, it turns like so
:source $MYVIMRC
but when I press Ctrl-P, it only goes to the last executed
command
:h camelcasemotion
Can anyone help me with this? It would be much appreciated.
In command mode, <Up> does “move to the previous command in the history that matches the string entered so far”.
In command mode,
CTRL-P has two functions.
After a Tab completion that has multiple possibilities, it does “show the previous matching value”.
Think of it as backward Tab. Most terminals do not distinguish Tab from Shift-Tab, but it and CTRL-P are the same mapping in GUI versions of Vim.
Without a preceding Tab completion, it does “move to the previous command in the history” (without trying to match the text entered so far).
You can remap CTRL-P to be the same as <Up> if you really want to:
:cmap <C-P> <Up>
You might also want to remap CTRL-N, too:
:cmap <C-N> <Down>

Resources