Command line completion to enter into a folder in Vim - vim

When I do :edit C:\ and keep tabbing, vim will cycle through all the files and folders. When I want to enter a folder, I think I can do Ctrl-E. But just by chance, I typed another backslash after the folder I want to enter, e.g, :edit C:\Documents\\. This also seems to work. Now if I hit tabs, vim start cycling through files/folders inside C:\Documents.
I wonder what is the reason behind this. Could Anyone point me to the reference manual where this behaviour is fully explained? And does there exist even better way to stop completion in the current directory and start command line completion inside another directory?

It is not documented, however, when you modify the text (for example, insert a backslash) during completion, vim will only auto-complete the rest of the string. This is because it starts a new completion cycle taking the current entry as the base.

I use this mapping, analog to concluding insert-mode completion:
" c_CTRL-Y Yes: Stop wildmode completion. Useful when completing
" directory names and wanting to use the currently
" completed directory and now complete its contents,
" instead of continuing to iterate the directory names.
cnoremap <C-Y> <Space><BS>
But Ctrl-E works as well when you're at the end of the command-line.

Related

Vim: How to open a file from current directory list without using arrow keys

Suppose I am editing a file using vim, and I go back to the file's current directory using :Ex, and I have a list of all the files I could open, I know arrow keys + Enter works, but is there a way to use : something to open a specific file? I tried :e filename but this goes directly back to the root of vim instead of the current directory.
Thanks.
The following is not a :-command, but it does the job and it can be in the muscle memory already:
You can move around the directory listing just like in a standard buffer. So you can /filename<Enter> to get to your file and <Enter> to open it. But typing whole filename can be rather cumbersome, so let's improve:
If there is something specific in the filename-baz, it will be enough to /baz<Enter><Enter>. And yet better, if you run vim with set incsearch and set hlsearch as many do, you'll see the search space narrow down to your filename, so you can easily get the prefix-search behavior of file commanders. Or even better, thanks to the coloring.
In case you can see the filename on the screen, then with EasyMotion, you can <Leader><Leader>w, then the usually two letters to get there and <Enter><Enter>.
tried :e filename but this goes directly back to the root of vim instead of the current directory.
This may happen because you are running vim from a different directory.
Suppose I run vim from my home directory, you will have to run :e /path/to/filename and :tabe /path/to/filename where the filepath is relative to the home directory.
you can open another file while vim is open with :tabe filename and to switch to the other file you type :tabn or :tabp for next and previous accordingly.
Maybe this link can help you

vim path completion in command mode

In vim, If you hit :e <tab><tab>, vim complete some path.
However, I want to open a/b/c/d.cpp, I stroke :e <tab><tab> and find AA.
I want to find subdirectory of AA. Alternatively, I hit / key.
example :e AA//BB//CC//DD.cpp
Are there another good way? In summary, I want to know complete path for subdirectory in command :e.
Command-line completion lets you use wildcards.
The basic * means "any character":
:e *<Tab> " similar to plain <Tab>
:e foo*<Tab> " completes only files starting with 'foo'
The fancier ** means "any subdirectory":
:e **/<Tab> " completes every file under every subdirectory
" of the current working directory
:e **/*foo<Tab> " completes every file ending with 'foo' under every subdirectory
" of the current working directory
See :help file-searching.
By the way, it's "command-line mode". "Command mode" is just another name for "normal mode".
This answer is based on the assumption you want the path for picking out the correct file for editing.
May I suggest to use :find after setting the path properly. This will save extra lot of typing.
You can start by setting :set path+=**(this will search sub-dirs as well). Now you can simply do
:find d.cpp
vim will do the rest for you. It will find out the path to d.cpp and open it.
What is nice about it is it allows to use wildcard, for example, as :find d.* or :find *.cpp.
If you don't want to use find and continue with edit, May be it will be useful to :set wildmenu. This will show all available options that you can iterate with tab

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

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