3

I can do :echo globpath('~/path/to/dir', 'string*') to get a list like:

/home/alec/path/to/dir/string_xyz123.mkd
/home/alec/path/to/dir/string_xyz456.mkd
/home/alec/path/to/dir/string_xyz789.mkd

but now I want to loop through that and for each /home/alec/path/to/dir/string_xyz123.mkd that gets returned I want to substitute on them.

Doing...

  let fileList = globpath('~/Documents/wiki/subdir', 'string*')
  for fileName in fileList
    substitute(fileName, '/home/alec/Documents/wiki/subdir', 'newtext', '')
  endfor

...produces error "List required"

:help for and :help while haven't been much help.

How do I iterate through this list of files?

8
  • By substitute, do you mean a :%s/oldtext/newtext/ command? If that's the case, you need to open the files into buffers... Please edit the question to clarify exactly what you want to do with the files (or the file names.) – filbranden Dec 20 '20 at 5:31
  • @filbranden I updated the question to clarify this. thanks for your time :) – alec Dec 20 '20 at 16:16
  • Did you visually examine the generated list of paths? Sometimes it's not what you expect. Also, using ~ in vim may not work as expected for certain use cases. You may need to expand() it. (Or use $HOME or the full typed out path.) – B Layer Dec 20 '20 at 16:58
  • 1
    Your question was originally about List required and I think @Matt's answer addressed that. Editing the question to make it about the next thing blocking you is not great (particularly once an answer addresses that)... Would you please edit it back to make about that (exclusively)? Then post a new question about the actual problem you're trying to solve. Thanks! – filbranden Dec 20 '20 at 17:35
  • 1
    @filbranden great thanks. I'll do as you suggest once I'm able to mentally process the comments! – alec Dec 20 '20 at 19:25
4

I can do :echo globpath('~/path/to/dir', 'string*') to get a list like:

This is not a List. This is a long String with newlines.

To get a List you must supply the 4th argument as in :h globpath():

echo globpath('path', ''expr', 1, 1)
3
  • 1
    What's this , 1, 1 extra arguments about? It solves the problem like you said, but I read the :help globpath and couldn't understand. Would you mind explaining it briefly in relation to this specific use of the command??? – alec Dec 20 '20 at 19:08
  • 1
    The last argument {list} defines whether to return a list or not, that's the one you actually need... The third argument {nosuf} is about whether to disable filtering of entries based on the 'wildignore' and 'suffixes' options. Note that 1 ("true") means to disable those filters, which is not the default behavior... You might want to pass , 0, 1 instead (see example in globpath()). Also note that when they talk about "true", they mean a non-zero value (such as 1), while 0 indicates a "false" value. I hope this helps! – filbranden Dec 22 '20 at 4:31
  • Yes, I noticed that both , 0, 1 and , 1, 1 worked. Thanks for explaining about the {list} argument :) – alec Dec 23 '20 at 2:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.