matpie
matpie

Reputation: 17522

How to make the glob() function also match hidden dot files in Vim?

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.

Upvotes: 6

Views: 4130

Answers (1)

ib.
ib.

Reputation: 29004

That is due to how the glob() function works: A single-star pattern does not match hidden files by design. In most shells, the default globbing style can be changed to do so (e.g., via shopt -s dotglob in Bash), but it is not possible in Vim, unfortunately.

However, one has several possibilities to solve the problem still. First and most obvious is to glob hidden and not hidden files separately and then concatenate the results:

:let backupfiles = glob(&backupdir..'/*').."\n"..glob(&backupdir..'/.[^.]*')

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

Another, perhaps more convenient but less portable way is to use the backtick expansion within the glob() call:

:let backupfiles = glob('`find '..&backupdir..' -maxdepth 1 -type f`')

This forces Vim to execute the command inside backticks to obtain the list of files. The find shell command lists all files (-type f) including the hidden ones, in the specified directory (-maxdepth 1 forbids recursion).

Upvotes: 5

Related Questions