0

I'm trying to conditionally load files in vim/neovim with Vimscript, except where the filename is prefixed with an underscore. It's not working 100% yet, instead all the files are still loading:

for filename in split(globpath('~/.config/nvim/plugins', '*.vim'), '\n')
  if filename !~ "^_"
    exe 'source' filename
  endif
endfor

Sort of works with file !~ "_", but that matches with the underscore anywhere in the string. I need ignore only filenames that start with _.

josef.van.niekerk
  • 11,941
  • 20
  • 97
  • 157
  • `^_` is the right pattern for "string starts with `_`". `if filename !~ "^_"` is truthy if `filename` *doesn't start* with `_` and falsy if it *starts* with `_`. – romainl Dec 22 '21 at 10:51

1 Answers1

0

I made a trivial mistake here, I assumed the filename contains something like _filename.vim, but instead the string contains the path as well, /Users/someuser/blah/_filename.vim.

I fixed it by splitting the path, and checking only the last element in the array:

for filename in split(globpath('~/.config/nvim/plugins', '*.vim'), '\n')
  if split(filename, "/")[-1] !~ "^_"
    exe 'source' filename
  endif
endfor
josef.van.niekerk
  • 11,941
  • 20
  • 97
  • 157