vim tab completion for directories - vim

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)

Related

VIM: How to open another file from same location as an already open file

Let's say I am in my home directory. I open a file that is present in some deep nested directory structure:
vim /some/really/long/path/file.txt
Now, within vim, I want to start a vertical split with another file from that same long location (but I don't want to change the current directory to that path, I still want to be in the home directory).
A layman (like me) will do something like:
:vsp /some/really/long/path/file2.txt
But I want to find out from all you VIM geniuses out there, is there an easier way to do this?
On the same note, if I had multiple files already open residing in different paths, can VIM automatically assign some sort of internal variables to all the locations? And then when I want to start a new VSP or SP with one of the files from one of those locations, I simply use those internal variables?
Try this:
:vs %:p:h/otherfile
as %:p:h gives you the path of the current file.
See :help %:p for more info.
Edit another file in the same directory:
:vs %<Tab><C-w><C-w><C-w>file2<Tab>
With a mapping:
nnoremap <key> :vs <C-R>=expand('%:p:h')<CR>
If you like more navigation than typing the commands, a convenient option could be the use of the embedded Explore command, which opens a window with the list files in the directory of the file in current buffer. Then you can navigate with the cursors and press v when over the file you want to open in a vertical split.
A convenient way is to map the Explore command, like:
" open embedded file system navigator
map <leader>\ :Explore<cr>
Press <leader>\ and navigate to the file, then press v.
With some versions of vim, the Explore window stays open after pressing v -- in that case, if you want to avoid extra burden to close the Explore window, you can do first the vertical split with :vsp, invoke :Expore and open the desired file by pressing Enter.

vim / file explorer: make browsing directory the current directory

I'm starting to use vim's native explorer (:E). My doubt: when I press c to "make browsing directory the current directory" nothing happens.. When I used nerdTree and did the same operation (:NERDTreeCWD), that directory was placed at the top of the exporer and the directories upper were "hidden".
It just seems pressing c is not doing anything..
NOTE: Im using the tree view on explorer, instead of the default one. I pressed i to change the view.
Im using 7.4.52
The netrw command works different than NERDTree's. It doesn't apply to the directory under the cursor, but to the one currently being browsed. So if you want to make a subdir the current one, first go into it (via <Enter>), then press c.
To expand on Ingo's answer. If you always want netrw to make the current browsing directory the current vim directory, you can set the option let g:netrw_keepdir=0 in your .vimrc.
This allows you to use netrw to browse directories and use vim to operate in that directory. For example, you can use vim's command :e <filename> to create a new file instead of netrw's command % <filename>.
To learn more about netrw I recommend reading the help pages :help netrw-quickhelp

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

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.

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