When searching for a command to enable me to open multiple tabs in normal mode, I found a custom command to do this:

" Open multiple tabs at once
fun! OpenMultipleTabs(pattern_list)
    for p in a:pattern_list
        for c in glob(l:p, 0, 1)
            execute 'tabedit ' . l:c

command! -bar -bang -nargs=+ -complete=file Tabedit call OpenMultipleTabs([<f-args>])

" less typing:
map <C-n> :Multitab 

However, this doesn't work when trying to open a path that doesn't exist yet. So one cannot use this to create new files. Example:

:Tabedit *.c
" works: opens all matching .c files
:!touch existing-file.txt
:Tabedit existing-file.txt
" works: opens the existing file
:Tabedit new-file.txt
" doesn't do anything at all

So what I want is for strings that don't require expansion, to open the same as with :tabedit.

1 Answer 1


My crude solution is to test whether glob finds any files and otherwise just to open the expression previously passed to glob. There is probably a more elegant way to do this.

fun! OpenMultipleTabs(pattern_list)
    for p in a:pattern_list
        let gl = glob(l:p, 0, 1)
        if empty(gl)
            let gl = [ l:p ]
        for c in gl
            execute 'tabedit ' . l:c
  • 1
    Won't this fail if you pass *.c and there are no C files...i.e. try to open single file *.c? Might be better to test the pattern for wildcards. If none, open using name as is otherwise use glob(). Question is whether there's a portable, robust way to test for wildcards. (You could simply look for for *, ?, ** but what about escaped wildcards? Or system specific wildcards?)
    – B Layer
    Mar 30, 2020 at 22:49
  • @BLayer Yes, I realise that. It just opens *.c as a file name. This is likely not intended, but I can live with it. And as you say, you'd need to have a portable way to test for wildcards, which I'm not keen on jury rigging myself.
    – bitmask
    Mar 30, 2020 at 23:01
  • If it's just a personal script and you're using it on *nix I'd go the wildcard match route. It should be less error-prone than the current version. Pattern is just something like [^\\]\(?\|*\|\[[^]]*]\). If you don't use [abc] wildcards then you just need [^\\]\(?\|*\) and that'll work on Windows, too. Your vim, your decision, though.
    – B Layer
    Mar 31, 2020 at 0:08

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.