I don't understand the findfile() function in Vim-Script, what am i doing wrong?

set encoding = utf-8
cd %:p:h
let b:gitiginorfiles = findfile('.gitignore', system('git rev-parse --show-toplevel'), -1)

My variable b:gitignorefiles is always empty.

  • 3
    I think you should check that the result of your git command is a path the Vum understands. To debug your problem maybe you could first hard code the path instead of the system call, when you're sure that it works, check that this path and the one returned by the system call match.
    – statox
    Commented Jan 3, 2017 at 9:12
  • @statox nothing :-/ Commented Jan 3, 2017 at 10:25

2 Answers 2


I need to chomp the trailing newline to see the line work:

:echo findfile('.gitignore', system('git rev-parse --show-toplevel')[:-2], -1)

This is a recurring problem with system(). I did rapidly end-up with a first version of system() in my library plugin that always chomps these unwanted characters.

EDIT: Otherwise, don't hesitate to extract intermediary result into variables that you'll be able to investigate. Often, I check what I really compute with a :echo .'##'.myvar.'##'.


Vim documentation is the best point to start. :help findfile():

findfile({name}[, {path}[, {count}]])               *findfile()*
        Just like |finddir()|, but find a file instead of a directory.
        Uses 'suffixesadd'.
        Example: >
            :echo findfile("tags.vim", ".;")
        Searches from the directory of the current file upwards until
        it finds the file "tags.vim".

You can use Ctrl+] to navigate into links, such as |finddir()|:

finddir({name}[, {path}[, {count}]])                *finddir()*
        Find directory {name} in {path}.  Supports both downwards and
        upwards recursive directory searches.  See |file-searching|
        for the syntax of {path}.
        Returns the path of the first found match.  When the found
        directory is below the current directory a relative path is
        returned.  Otherwise a full path is returned.

You can debug that call using this:

:echo findfile('.gitignore', system('git rev-parse --show-toplevel'), -1)

This will run the findfile() and echo the result in the screen. You can use q: to edit the call and test again.


You could try to understand it better by using simple examples. For instance, try to search for a nonexistent file in the current direct, then create the file and repeat:

echo findfile('bla', .)   " empty result, no file
!touch bla                " create the file (this works in *nix systems)
echo findfile('bla', ".") " now it returns a non-empty result

You can check that you are in the directory you expect by using :pwd and getcwd().

  • I read this and i do what is write but nothing... I'm confused. Commented Jan 3, 2017 at 10:28
  • Only [] a empty list after let b:gitiginorfiles = findfile('.gitignore', '/home/username/projects/gitrepo/', -1) Commented Jan 3, 2017 at 10:32
  • Well, that means that findfile() didn't find a match. Ensure that the file can be found from your working directory, given the arguments you passed to findfile(). Is there something else unclear about findfile()?
    – mMontu
    Commented Jan 3, 2017 at 10:34
  • Why I become a empty list, I don't see a mistake... sorry... Commented Jan 3, 2017 at 10:36
  • Now it work, don't ask my why - First day in new year ;) Commented Jan 3, 2017 at 10:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.