vim path auto completion, how do I enter a directory? - vim

When I want to open a file in vim, I enter :e to see directories on my disk (set wildmenu). Vim shows me list of directories I have, then I press tab several times to choose directory I need, and when I select that directory (vim highlights it) what should I do to stop completion on current level and enter that directory?
I know that in insert mode completion it's ctrl-y, from vim help file:
*complete_CTRL-Y*
When the popup menu is displayed you can use CTRL-Y to stop completion and
accept the currently selected entry. The CTRL-Y is not inserted. Typing a
space, Enter, or some other unprintable character will leave completion mode
and insert that typed character."
how to do that in command mode?

I think its Ctrl E in command mode
From vim docs,
*complete_CTRL-E*
When completion is active you can use CTRL-E to stop it and go back to the
originally typed text. The CTRL-E will not be inserted.
I tried this in my gVim and it works.
EDIT: Thanks to the suggestion by #François, Ctrl D does the same thing with some additional info. ie it stops completion in the current level and also shows the contents of the current selection
Something like
:e eclipse-cpp-indigo-SR2-incubation-win32-x86_64\eclipse\ <Ctrl><D>
artifacts.xml eclipse.exe epl-v10.html p2\
configuration\ eclipse.ini features\ plugins\
dropins\ eclipsec.exe notice.html readme\
It is showing the contents inside the folder eclipse too apart from stopping completion at the eclipse directory level.

When you reach the required directory pressing the tab key, press the right arrow key (or type a character and erase it). After that completion use the directory as the base one and iterates through its subdirectories.
For example, you have the following file structure in the current directory:
- a
- b
- c
- d
You type the e command and press the tab key. The first suggested directory is a. Press the tab key again to select the next directory - b. And now press the right arrow key. After that the b directory is selected and completion starts inside it.
As I said before you may type a character and erase it. If you leave a character (or a sequence of characters, e.g. prefix) it is used as a filter (selects only those directories whose name starts from the prefix).
I think there are other solutions. But the two above is fully enough for me.

One more option: when I want to select and continue, I just enter another "/" and continue tabbing. Looks a little ugly in my status line, but it's one keystroke and I don't have to leave my home row.

Related

Visual Studio Code - "Add Selection to find next match" losing selection when vim keymap enabled

I have a new installation of Visual Studio Code (default configuration, with vim keymap). I want to use the command "add selection to find next match". I think this is equivalent to how multicursors work in sublime text and vim (with an extension), or Atom (find and replace: select next).
When I try to run the command directly by using Ctrl D it does not work, taking me to the last line in the file. This might be some kind of conflict that might be easy to solve.
More interestingly, when I run the command from the command palette Ctrl Shift P.
The first time selects the word I am at (well done!)
The second time selects the next occurrence of that word, but loses the selection, moving the two cursors right after the two first occurrences of the word.
The third time and next times nothing happens, the selection was lost in the previous time.
How to get the awesome normal behaviour that other editors have?
PD: I have now confirmed that this behavior is not present without the vim keymap, may they be compatible somehow?
Ctl + D is a default Vim keybind that scrolls the window down in the buffer. This is expected behavior for the Vim extension, and you should disable the mapping if you wish to use it to run the VSCode command "Add Selection To Find Next Match".
To do this, open the extension's setting by clicking File > Preferences > Extensions. From there, choose the Vim extension and select the option to Configure Extension Settings. There will be an option called Handle Keys, where you can enter JSON data to send certain key combos back to VSCode (and there's another option to turn off all Ctl + Letter combos, called Use Ctrl Keys). You can find some examples of such data on the extension's Marketplace page. Below is the snippet of code that I added to my settings.json file to get Ctl + D back.
"vim.handleKeys": {
"<C-d>": false
}
From my testing, it looks like changing this setting also fixes the behavior when using the Command Palette. It might be a bug, but who cares when you are most likely just going to use Ctl + D anyway. :)

vim tab completion for directories

In vim using a command that expects a file I can use tab to cycle through the files in a directory.
If it is currently showing a directory in the "cycle", is there a way to make it so that the next time I tab it will instead cycle through the currently shown directory? What I am currently doing is just pressing space and then backspace but is there a better way to do this?
For example, if I type
:e ~
then when I press tab I will cycle through the directories in my home directory, e.g. several presses of tab may give me
:e ~/Desktop/
:e ~/Documents/
:e ~/Downloads/
Now for example I may actually want to open a file in ~/Documents, so I'm wondering if there's a way to make it start cycling from ~/Documents/ instead of ~/
Thanks!
Simply hit Down or / when you've reached ~/Documents.
Note that using / will result in ~/Documents//, but completion will still work (i.e show items in ~/Documents).
Edit
You can also use Ctrl - E, which doesn't add the trailing / (and might be easier to type)

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. :)

How could I get this Vim binding to work?

qnamebuf plugin description says:
<S-F4> opens an explorer from the current working directory showing all files which are not in a hidden directory (one that starts with '.')
I'm not very good with Vim, but I imagine S-F4 is a capital S followed by F4. However, when I press S, I find myself in insert mode, and pressing F4 after this, just adds an F4 in the window. What should I do to make the command work? I am running gVim under Windows.
The S stands for Shift. So try pressing Shift-F4.

When in Vim insert mode, is there a way to add filepath autocompletion?

I write a lot of shell scripts and I am constantly having to enter in filepaths. I am wondering if anyone knows of a way to get Vim to autocomplete filepaths while in insert mode, just like when you are in your favorite shell you tab to complete the path of the directory or file.
Extra credit if you can get CTRLD functionality of the shell into Vim insert mode (i.e. to see what files/directories are in the current path).
For file name omni completion, you can use:
Ctrl-XCtrl-F
There's ctrl-x ctrl-f
:he compl-filename
To build on #CMS and #michael excellent answers
When using ctrl+X ctrl+f command sequence it will display a list of files in the current directory. I spend a minute looking for the right key to move up and down between the different filenames. The correct keys are Ctrl-n and Ctrl-p. You can use almost any other key (like Space) to select and continue typing.
In addition, if you are not already at the file/directory you would like to insert, you can go down a file tree structure as follows:
Optionally enter some part of the directory. You can even use ../../ for example!
Press ctrl+X ctrl+f and select the first item in the tree from the list.
Next press ctrl+f again while the correct item is highlighted to display a list of the next level down the directory tree structure.
You can keep doing this until you find the directory/file you would like to insert in the document.
I experienced similar problem. I found solution like:
sudo apt-get install realpath
And in VIM naviagte to file with in normal mode type:
:r !realpath /path/to/file
When you are navigating in non-insert mode after !realpatch you are able to use our key button.
VOILA! TAB is working again!
edit: excuse me, I landed here from a google result for "vim insert file absolute path"
(first leave insert mode with esc or ctrl+c) ;)
from normal mode, on a blank line
!!readlink -f #
this will run a command, and substitute # with the current file name, readlink will resolve a canonical name, and !! will write the output where the cursor was
note, this needs to be done on a blank line, as the content of the line will be fed as stdin to the subcommand.

Resources