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

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.

Related

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

Command line completion to enter into a folder in 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.

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

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.

Easily open include filenames in vim?

Basically, I'm editing files that have include file names and I want a quick way of opening the file without having to type the path/filename:
include('inc/doctype.inc.php');
Is there an easy way to do this? (Ideally, I'd like to use :tabnew to open the file in a new tab.)
Thanks.
Use the gf shortcut. Move your cursor on a path string, the exact cursor position is not important and then press gf in normal mode. gf stands for "goto file".
See vims help page with :h gf:
Uses the 'isfname' option to find out which characters
are supposed to be in a file name. Trailing
punctuation characters ".,:;!" are ignored.
Uses the 'path' option as a list of directory names
to look for the file. Also looks for the file
relative to the current file.
Uses the 'suffixesadd' option to check for file names
with a suffix added.
If the file can't be found, 'includeexpr' is used to
modify the name and another attempt is done.
To get back, use Ctrl-o in normal mode.
Note: This command brings the cursor position to older positions in the jump list. The opposite command is Ctrl-i which brings the cursor to newer positions in the jump list.
Put the cursor on the filename, then Ctrl+wgf
:h ctrl-w_gf

Resources