开发者

Make the glob() function also match hidden dot files in Vim

开发者 https://www.devze.com 2023-01-18 04:08 出处:网络
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?

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 bee开发者_StackOverflow社区n working on: delete-old-backups.vim


That is due to how the glob() function works: A single star character 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 still has several possibilities to solve the problem. First and most obvious is to glob hidden and not hidden files separately and then concatenate results:

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

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

Another and probably more convenient way (but less portable, though) is to use 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 get 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).

0

精彩评论

暂无评论...
验证码 换一张
取 消