vim completion deletes typed letters - vim

I've come to a vim completion behavior that is very annoying for me and I cannot figure out how to configure vim to behave differently.Maybe it is not possible at all.
Suppose I'm editing file with following content:
MyCompany2
MyCompanies
MyCompany3
Now I want to add another entry (say MyCompanyABC) so I type My and hit Ctrl-N, so now I have
MyCompany2
Now I hit backspace, then A so I'm at
MyCompanyA
No I decide to try completion again so I hit Ctrl-N and vim takes me back to
My
So is there a way to make it so that the last step keeps what I already have?
UPDATE:
I gave the completion sequence wrong. The problem I describe appears if one first hits Ctrl-P. Then in the above scenario you get and the rest as above.
MyCompany3

This doesn't behave like this on my machine (Ubuntu 10.10, vim 7.2.330 here). If I do this:
Open vim by typing vi
Type the text you gave above
MyCompany2
MyCompanies
MyCompany3
Open a new line below
Type "My"
Press Ctrl-N
Choose MyCompany2
Backspace (get "MyCompany" after that)
Type A (get "MyCompanyA")
Ctrl-N does nothing - as expected, status bar says: -- Keyword completition (^N^P) Pattern not found.
Is it possible you have some plugin that is changing the default behavior? You can also try vi -u NONE to check whether you have something in your .vimrc that is changing this behavior. Best if you have some other system to check it out.

Are you sure that you want to use Ctrl-N directly without hitting Ctrl-X beforehand? Ctrl-N will also search in all opened buffers.
Maybe hitting CTRL-X CTRL-N or CTRL-X CTRL-P will result in a more stable completion.

Related

vim - navigating characters in command mode

Relatively new to VIM and having a great time using it.
One very minor annoyance I've been having is command mode character navigation when I want to revise a command. I'm used to using readline shortcuts on the regular (non-vim) command line but these shortcuts don't seem to work in : command mode.
For example, using ctrl + b to go back a character ends up sending me to the start of the line, or using alt + f to go forward a word ends up clearing the line and exiting command mode.
The only way I've found to navigate in command mode is to use the arrow keys, but I'm under the impression you should avoid the arrow keys in vim for max efficiency.
What is the standard way to navigate around in : command mode? Do vim users usually use the arrow keys here? Is there a different way to modify commands?
As a more concrete example, I've been using vimgrep a lot to search through files. I'll do a command like:
:vimgrep /font-family/j my-project/**/*.less | cope
Later, I'll want to use the same search but look for a different property, so I hit : then ctrl + p to access my previous vimgrep. Now here I have to use the arrow keys to navigate backwards to the search string and modify it. It would be much faster if I could use readline to navigate backwards by word then delete by word.
For small edits, Backspace and light use of the cursor keys should be fine. For anything larger, I would advise to use the command-line window:
In the command-line window the command line can be edited just like editing
text in any window.
So, there's no need to mentally switch to readline key mappings; just use the full editing power (including any custom mappings) of Vim!
You can switch via <C-F> from the command-line, or directly enter it (from normal mode) by pressing q: instead of :. All the details can be found at :help cmdline-window.
I like this question. Long time vim user, but new-ish here, so I can't vote it up. But indeed, perhaps unofficially, many vim fans feel that most of the time the goal is to not have your hands leave home row position (fingers move, hands relatively still).
I will admit, when it comes to command mode, I use the arrows. With P being on my pinky finger, I would miss-hit ctrl-P a lot, and it's faster to slide my right hand down (on my Natural keyboard) and find the up-arrow by quick feel, instantly, to do the same thing. Once I'm there, left-right arrows are also easy to find without looking or delay. Also Ctrl-arrows let you skip by word.
One of the great things about vim is the :help. I have easily spent tens of hours over the years reading through it, and it solves 95% of my problems if I have enough time and working-memory to push deep enough into it.
Here is what I found for :help readline:
READLINE readline.vim ft-readline-syntax
The readline library is primarily used by the BASH shell, which adds
quite a few commands and options to the ones already available. To
highlight these items as well you can add the following to your
|vimrc| or just type it in the command line before loading a file with
the readline syntax:
let readline_has_bash = 1
This will add highlighting for the commands that BASH (version 2.05a
and later, and part earlier) adds.
Give it a try! (I am using vim 7.4)
You can see a list of the default key binds with :help cmdline-history (scroll down a bit) and :help ex-edit-index.
You can remap this with cnoremap:
cnoremap <C-b> <Left>
cnoremap <C-a> <C-b>
" .. Probably more
Note that using cmap will probably get you into problems here since the right-hand side is the currently mapped action, whereas with cnoremap the right-hand side it will always use the native Vim action.

Vim commands containing `r` cause me to replace

I'm using vim and the python-mode extension, but I'm having a hard time using commands that contain r. It's causing vim to replace characters instead of executing my desired command.
Here is what the vim docs for python-mode say -
let g:pymode_rope_organize_imports_bind = '<C-c>ro'
So I'm doing CTRL-cro, but like I said, it's replacing which ever character I'm under with the letter o.
What am I missing?
It seems that your mapping is not being interpreted by Vim, so it only sees the Ctrl-c, which by default aborts the current action, then the replace command r (see :help r) followed by its "argument".
You could check if the mapping is defined with :map <c-c>.
If it is correctly defined it may be that your terminal is handling the Ctrl-c directly and not passing it to Vim, as stated in Vim FAQ 20.5 - Why does mapping the key not work?. In this case you could follow the instructions on Vim FAQ 20.4 - I am not able to create a mapping for the key. What is wrong?, in special:
1) First make sure, the key is passed correctly to Vim. To determine if
this is the case, put Vim in Insert mode and then hit Ctrl-V (or
Ctrl-Q if your Ctrl-V is remapped to the paste operation (e.g. on
Windows if you are using the mswin.vim script file) followed by your
key.
If nothing appears in the buffer (and assuming that you have
'showcmd' on, ^V remains displayed near the bottom right of the Vim
screen), then Vim doesn't get your key correctly and there is nothing
to be done, other than selecting a different key for your mapping or
using GVim, which should recognise the key correctly.

Traversing directories with vim file name completion in insert mode (Ctrl-X Ctrl-F)

I’m trying to use vim’s compl-filename feature (Ctrl-XCtrl-F) to complete paths in INSERT mode, but I can’t work out how to traverse into directories without (temporarily) ending the completion mode:
Let’s say I want to complete the path /etc/sysconfig/network-scripts/ifup.
I would like to be able to do something like:
/eCtrl-XCtrl-F
/etc/
/etc/sysCtrl-F
/etc/sysconfig/
/etc/sysconfig/netCtrl-F
/etc/sysconfig/netconsoleCtrl-N
/etc/sysconfig/networkCtrl-N
/etc/sysconfig/network-scripts/
/etc/sysconfig/network-scripts/ifupCtrl-Y
/etc/sysconfig/network-scripts/ifup
The issue is, as soon as I start typing* after a path match (like /etc/), it ends file name completion. I would like it to stay in file name completion, so that I can still use Ctrl-F, Ctrl-N, etc. Since it ends completion, I have to type Ctrl-XCtrl-F again to restart it, and the helpful completion popup menu disappears in the meantime.
Is there an option I can set to change this?
* By ‘typing’ here, I am referring to characters in 'isfname' -- of course, typing other characters (like space or punctuation) should not continue file name completion.
I'm not sure exactly what you're saying, but you can just press Ctrl-XCtrl-F again on a directory while you're in the completion menu to expand it. You don't have to close out of the menu first. I just keep Ctrl held down and tap xf to traverse a directory, n and p to move up and down and w to go back up.
If you don't use :h i_CTRL-F then you could remap it. For example,
inoremap <C-f> <C-x><C-f>
Simple remap would be
inoremap / /<C-x><C-f>
So when you type slash(/) in insert mode you will get that auto completion popup :)
Place it in your .vimrc file (for vim) or in init.vim (for neovim)
Vim doesn't do auto-completion.
For that, you'll need a dedicated plugin like AutoComplPop or NeoComplCache
Please use insert "i" first before using cntr+x+f. I was in similar situation. :)

Pressing tab for word completion in command line mode switches to search mode in Vim

When loading a file, lets call it foobar.txt, like so:
:b foo<tab>
Where <tab> is me pressing the tab key, sometimes I am redirected from the command line (:) to the search prompt (/) but other times it works fine. I am having trouble recreating the exact situation, but I know that I have had this same problem with at least these three commands:
:badd
:b
:vsplit
Can anyone tell me why this would ever happen?
I'm guessing this is something related to your termcap (does tab do anything weird when you're in normal/insert mode?), but if it's specifically related to interacting with the filesystem it could be something else. Have you tried a different termcap and/or using the gui version instead of the console version to see if the issue is reproducible?

Vim "show my last command" command?

Is there a command which shows what was the last command in normal mode?
Suppose I accidently hit random key and got some unexpected result.
Sure I can undo it, but could I reveal what key was pressed and how it was interpreted?
Hit the colon (:) and then use the up arrow to start going back through previous commands. You can use the up/down arrows too to move around the list.
q: will show you command history in Vim.
q/ will show you history of searches.
And must importantly, :q will quit the mode.
The text from the last command is stored in the . register. You can see all registers by :display. Unfortunately it doesn't say what the started the normal command.
To see commands from : (command mode) you can use :hist or q: which is limited to the last 20 (by default).
Another ability is to save the undo buffer :wundo undo.bin -- but the undo buffer is binary.
But none of these actually answer your question. I'm curious if it can be done.
Entering colon : then ctrl+p shows your previous command, i.e., moving backward through your vim command history. ctrl+n moves forward.
This is very convenient if you're used to using the command line and prefer not to change your keyboard hand positioning to use arrow keys.
It is difficult to know it. You can play with the variables:
v:operator
v:count (and v:prevcount)
v:register
But you cannot fully get the last normal mode command issued.
However if you want to systematically record everything you type while in Vim, you can launch vim -W ~/.vim-last-scriptout (a Windows version: vim -W "%HOMEPATH%\Vim\.last-scriptout) You can alias it in your shell on a UNIX machine. Every single key, or control-key, will be recorded into that file. Note that if you happen to use gvim or vim -g (the GUI) you might encounter this bug.
If you want to replay this file you can use :source! (with the exclamation mark) or the -s option from the command line.
On Windows I have set gvimportable.exe -W gvim_directory\last_scriptout as my default editor in my Commander program (FreeCommander). This way I can always remember what I have typed to do something and repeat a sequence of commands on another file. Of course I have another shortcut for opening Vim and playing the scriptout.
Note that the file might be written only when Vim exits, so you have to lose your session to know what you've done.

Resources