I want a simple solution to get directory names under a path in vim script.
Here is my tried ways: the code. https://gist.github.com/4307744
Function is at line L84.
I use this function as complete function for input().
So this function need to return a list of directory names under a path.
e.g.
to/path/
- a/
- b/
I want to get a and b.
I tried to find vim internal functions with :help functions. only found globpath(), but it will return full path.
So does anyone have a simple solution ?
(BTW, why it is so hard to get directory names under a path in Vim ??)
I don’t know whether it was intentional, but
glob()limits directories to only those with paths where the pattern ends with/:. Some explanations:
fnameescape()escapes top_directory (it should be set toto/pathin the example) in order to prevent special characters in it from being expanded on their own (I once used to have directory named*.*).{,.}is necessary because on unix vim won’t list files starting with dot by default. Note that normally.*pattern matches special.and..directories that are then removed, but due to some reason{,.}*does not match them., 1, 1makeglob()ignore'suffixes'and'wildignore'options (first) and return a list (second, requires most recent vim).:h:twould return only parent directory name, butglob()outputs paths liketo/path/a/and:hthus removes only the trailing slash.:tstrips directory path (returns trailing path component). Without:hstripping slash trailing path component would be empty string.You can join everything into one line: