tags:

views:

56

answers:

1

In a Linux or Mac environment, Vim's glob() function doesn't match dot files such as .vimrc or .hiddenfile. Is there a way to get it to match ALL files including hidden ones?

The command I'm using:

let s:BackupFiles = glob("~/.vimbackup/*")

I've even tried setting the mysterious {flag} parameter to 1 and yet it still doesn't return the hidden files.

UPDATE: Thanks ib! Here's the result of what I've been working on: delete-old-backups.vim

+1  A: 

That's because of how the globbing works: single star * symbol does not match hidden files by design. In the shell default globbing style could be changed to do so (shopt -s dotglob in Bash), but it seems that it's not easily reachable in Vim.

You have several possibilities to solve your problem anyway. First and most obvious is to glob hidden and not hidden files separately and then concatenate results, like this:

let backup_path = '~/.vimbackup'
let s:backup_files = glob(backup_path . '/*') . "\n" . glob(backup_path . '/.[^.]*')

(Be careful not to fetch . and .. along with hidden files.)

Another and probably more convenient way (but less portable, though) is to use backticks expansion in glob:

let backup_path = '~/.vimbackup'
let s:backup_files = glob('`find ' . backup_path . ' -maxdepth 1 -type f`')

This forces Vim to execute the command inside backticks to get the list of globbing files, and find lists all files (-type f tells to list files, not directories) including hidden ones, in the specified directory (-maxdepth 1 forbids recursion).

ib
This works great. Thanks! As a side note, using `find ~/.vimbackup/ -mtime +14` was my first version but now that I'm working on Windows too I decided to make a more portable version.
sirlancelot