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>
Related
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
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.
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.
In order to save time in Vim, I came up with an idea. To map :w key binding to Esc in both normal mode and insert mode. However it only works in insert mode whereas in normal mode things are getting messy when I open a new file. This is what I added in .vimrc:
:inoremap <Esc> <Esc>:w<CR>
:nnoremap <Esc> :w<CR>
As I said the first command alone, works fine. But adding the second command, keys are messed up ESPECIALLY when I open a new file. For instance, although I have explicitly added in .vimrc:
map <up> <nop>
map <down> <nop>
map <left> <nop>
map <right> <nop>
by adding the second command for the normal mode, pressing up down left or right keys cause to enter in insert mode and add A B C D.
Could you help me to achieve my idea?
Information on Vim FAQ 10.9 may be useful:
10.9. When I use my arrow keys, Vim changes modes, inserts weird characters
in my document but doesn't move the cursor properly. What's going on?
There are a couple of things that could be going on: either you are using
Vim over a slow connection or Vim doesn't understand the key sequence that
your keyboard is generating.
If you are working over a slow connection (such as a 2400 bps modem), you
can try to set the 'timeout' or 'ttimeout' option. These options, combined
with the 'timeoutlen' and 'ttimeoutlen' options, may fix the problem.
The preceding procedure will not work correctly if your terminal sends key
codes that Vim does not understand. In this situation, your best option is
to map your key sequence to a matching cursor movement command and save
these mappings in a file. You can then ":source" the file whenever you work
from that terminal.
For more information, read
|'timeout'|
|'ttimeout'|
|'timeoutlen'|
|'ttimeoutlen'|
|:map|
|vt100-cursor-keys|
From :h vt100-cursor-keys:
Other terminals (e.g., vt100 and xterm) have cursor keys that send <Esc>OA,
<Esc>OB, etc. ...
So probably your nnoremap is causing the Esc on the arrow's key sequence to save the file, and the remaining characters are being interpreted alone, so the A is entering insert mode.
You could consider using option 'autowriteall', or using a different mapping to save your file; these are defined in $VIMRUNTIME\mswin.vim:
" Use CTRL-S for saving, also in Insert mode
noremap <C-S> :update<CR>
vnoremap <C-S> <C-C>:update<CR>
inoremap <C-S> <C-O>:update<CR>
The :update command is similar to :w, but only writes only if the file has been modified.
Also, you can use
autocmd InsertLeave * write
Is there an option to auto-complete a currently typed command with one from the history, without using the direction keys?
This an extension to this question: How do you search through vim's command history?.
The answer was, after typing :somecommand to use the <up> key. I would think it is more vim-like not to have to lift the hand to go to the direction keys.
From :help cmdline-completion, I tried, Ctrl-N, Ctrl-P, Ctrl-D, Ctrl-L without success.
Here's maybe a related part of my vimrc.
set completeopt=menu,longest,preview
set showcmd
set wildchar=<Tab> wildmenu wildmode=longest,list,full
set wildcharm=<C-Z>
I'm aware of the command line window invoked with q: or :Ctrl-F and the Ctrl-N and Ctrl-P to go through the history linearly after typing :.
You can always map this to something. I once tried using
cnoremap <C-p> <Up>
, but this will disable completion cycling so you should better deduce something else (there is no way like pumvisible() to determine whether completion is active in command mode).