Recursive Tab Complete Filenames on :edit in VIM - vim

I had found a .vimrc configuration that allowed me to simply type
:e <<characters_in_filename>>
and then tab and the path would expand out to :e full_path or show me a list of options if there are similarly named files in my current path.
Anyone know how to make this happen?
note: I'm aware of FuzzyFileFinder, Peepopen, and CommandT, this has just really been bothering me.
It's very similar to the functionality described in this google groups thread

As ZyX said, it sounds like you want find.
set wildmenu
set wildmode=longest:full
set path+=./**
If you type :find so<Tab>, it will complete with all files that start with so searching recursively from the current file's directory (not pwd). (<Tab> can be changed with wildchar.)
You may prefer this kind of completion:
set wildmode=list:longest
And you could add your often used roots to path if you don't want recursive from the current file's directory.
set path+=~/code/**
If you just want to recurse from the current directory, try the above wild settings and use:
:edit **/so<Tab>
For more on specifying filenames see :help {file} and for more on **, see :help starstar-wildcard.

Be sure that wildmenu is on and try :find command.

Related

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

navigating filesystem in vim -> :find vs. :edit

When opening files in Vim I almost always do something like this:
:e subDir/**/file<ctrl-d>
But in the docs and basically every StackOverflow/blog post I have read it seems that people use "find" the way I use "edit".
What am I missing by using the edit command instead of the find command?
:edit is restricted by default to the working directory: if you need to edit a file that is not under your working directory you will have to provide its absolute path or a path relative to the working directory. Also, you need to provide the necessary globs.
:find is superficially very similar to :edit but the (big) difference is that it finds files in the directories specified in the path option. path is what makes :find a lot more interesting than :edit.
With set path=,, you essentially get the same behavior as :e foo.
With set path=** you essentially get the same behavior as :e **/foo except you don't have to use any glob.
With set path=.,** you also get access to files in the same directory as the current file.
With set path=.,**,/path/to/some/central/vendor/directory you also get access to files from that directory… and so on.

How to get Vim to use a local spell-file in the current directory?

I undestand Vim can do spell-check, and it can modify a spell-file inside ~/.vim to keep words flagged as good.
I'd like to keep a "local" spell-file in the same directory as a file I'm editing, so that if I'm in different directories editing different files, different spell-files would be used.
How would I set up Vim to do this?
Vim's own help is usefull here:
:help spellfile
Name of the word list file where words are added for the
zg and zw commands. It must end in ".{encoding}.add".
You need to include the path, otherwise the file is placed
in the current directory.
More detailed information:
http://thejakeharding.com/tutorial/2012/06/13/using-spell-check-in-vim.html
Separate Vim spellfile for custom words
So:
:set spellfile=./en.utf-8.add
hth

Command-T not searching in current working directory

I think the title says it all but just to give some context, I have this set on my .vimrc
set wildignore+=*.o,*.obj,**/.git/*,**/.svn/*,**/node_modules/**,node_modules/**,.git/*,svn/*
And from the readme:-
:CommandT
A prompt will appear at the bottom of the screen along with a file window
showing all of the files in the current directory (as returned by the
|:pwd| command).
It mentions that it should show all files in the current directory but even if I cd into any directory, Command-T still goes all the way up to my Desktop and lists all the files and folders which is not what I want. I just want to search on current working directory like it says in the readme.
I also tried checking if I was indeed in the right directory by doing :pwd and it's showing me I'm in the right directory and still lists out everything. However, if I have .git folder in my root directory then it seems to work.
Am I missing something? If it helps, I also have this in my .vimrc file:-
imap <C-t> <C-c>:CommandT<CR>
vmap <C-t> <C-c>:CommandT<CR>
nmap <C-t> :CommandT<CR>
Add this line to .vimrc file
let g:CommandTTraverseSCM='pwd'
It will search in current directory instead of SCM root directory.
Warning: This is a non-answer answer to your question.
Please take #romainl's advice and look at the plugin's issue tracker.
Documentation
Command T and all good Vim plugins come with documentation. Please read all of it before asking questions. See :h command-t. The things you might have noticed if you had:
g:CommandTScanDotDirectories - basically the default config means CommandT will not search dot directories. Meaning your 'wildignore' probably doesn't need .git/.svn/... entries.
See :h command-t-wildignore for more information on CommandT and 'wildignore'
g:CommandTTraverseSCM dictates how CommandT find its root directory. The default looks for a SCM root marker, e.g. .git. If it can't find one it will fall back to the current directory. Please look at this option for more options. You may want to use let g:CommandTTraverseSCM = 'pwd'
Mappings
I have some concerns:
You should be using *noremap style mapping unless you are mapping to a <Plug>(something) or you really want recursive mappings (Hint: not usually).
Please use <esc> instead of <c-c> as they are not the same. Sure they are similar but they are not the same.
Overshadowing the <c-t> command in normal mode which pops the tag stack.
Overshadowing the <c-t> command in insert mode which increases indentation.
Probably should not even worry about creating mappings for any mode except normal mode because that is the Vim way.
My recommendation is to use the default CommandT mappings which are <leader>t and <leader>b (See :h command-t-mappings). The <leader> defaults to \ however this changes if you change the mapleader. See :h mapleader.

How to autocomplete file paths in Vim, just like in zsh?

In Zsh, I can use filename completion with slashes to target a file deep in my source tree. For instance if I type:
vim s/w/t/u/f >TAB<
zsh replaces the pattern with:
vim src/wp-contents/themes/us/functions.php
What I'd like is to be able to target files the same way at the Vim command line, so that typing
:vi s/w/t/u/f >TAB<
will autocomplete to:
:vi src/wp-contents/themes/us/functions.php
I'm trying to parse the Vim docs for wildmode, but I don't see what settings would give me this. It's doing autocompletion for individual filenames, but not file paths. Does Vim support this natively? Or how can I customize the autocomplete algorithm for files?
Thanks for any advice!
-mykle-
I couldn't find a plugin to do this, so I wrote one. It's called vim-zsh-path-completion. It does what you're looking for, although via <C-s> rather than <Tab>. You can use it with <Tab> for even more control over what matches, though.
It's got bugs, but for basic paths without spaces/special characters, it should work. I think it's useful enough in its current state to be helpful. I hope to iron out the bugs and clean up the code, but I figured I'd start soliciting feedback now.
Thanks for the idea!
Original (wrong) answer, but with some useful information about Vim's wildmode.
Put the following in your .vimrc:
set wildmenu
set wildmode=list:longest
That will complete to the longest unique match on <Tab>, including appending a / and descending into directories where appropriate. If there are multiple matches, it will show a list of matches for what you've entered so far. Then you can type more characters and <Tab> again to complete.
I prefer the following setting, which completes to the first unique match on <Tab>, and then pops up a menu if you hit <Tab> again, which you can navigate with the arrow keys and hit enter to select from:
set wildmode=list:longest,list:full
Check out :help wildmenu and :help wildmode. You might also want to set wildignore to a list of patterns to ignore when completing. I have mine as:
set wildignore=.git,*.swp,*/tmp/*
Vim doesn't have such a feature by default. The closest buil-in feature is the wildmenu/wildmode combo but it's still very different.
A quick look at the script section of vim.org didn't return anything but I didn't look too far: you should dig further. Maybe it's there, somewhere.
Did you try Command-T, LustyExplorer, FuzzyFinder, CtrlP or one of the many similar plugins?
I use CtrlP and fuzzy matching can be done on filepath or filename. When done on filepath, I can use the keysequence below to open src/wp-contents/themes/us/functions.php (assuming functions.php is the only file under us that starts with a f):
,f " my custom mapping for the :CtrlP command
swtuf<CR>
edit
In thinking about a possible solution I'm afraid I was a little myopic. I was focused on your exact requirements but Vim has cool tricks when it comes to opening files!
The :e[dit] command accepts two types of wildcards: * is like the * you would use in your shell and ** means "any subdirectory".
So it's entirely possible to do:
:e s*/w*/t*/u*/f*<Tab>
or something like:
:e **/us/f<Tab>
or even:
:e **/fun<Tab>
Combined with the wildmode settings in Jim's answer, I think you have got a pretty powerful file navigation tool, here.

Resources