1

Let's say I have 10 files opened in buffer with vim *. One way of saving the name of all those files inside a new file would be using the command:

:argdo !echo % >> filenames.txt

This command works fine and does what I expect... However, it opens a dialog where I need to press enter multiple times until I reach the number of files that are on my buffer... Like the following:

10 files to edit


Press ENTER or type command to continue

Press ENTER or type command to continue

Press ENTER or type command to continue

Press ENTER or type command to continue

Press ENTER or type command to continue

Press ENTER or type command to continue

Press ENTER or type command to continue

Press ENTER or type command to continue

Press ENTER or type command to continue

Press ENTER or type command to continue

How can I achieve this same goal without having to press enter multiples times?

3

You can use the following command, which will put the return of the argv() functiob the current buffer under the line where the cursor is:

:put =argv()

This function returns the list of files in arguments (same as the :args command will list), but it will return them as a Vimscript list. Passing :put a list (through the expression register) will have it put each entry into a new line.

4
  • Is there a way of making the list separated by new lines instead of spaces? I can achieve this goal by going to a new file and using a command like :exe "%s/ /\r/g" | exe ":%! grep -v '^$'"... But I'm not sure if that's the fastest way of doing it... – raylight May 2 at 23:56
  • 1
    Using :put =argv() directly is much better and does exactly what you want. I updated my answer to do that instead. – filbranden May 3 at 0:29
  • 1
    Thanks! I'm not allowed to edit your answer... There's a typo on argv() there :) – raylight May 3 at 0:35
  • Thanks for pointing out... It was autocorrect. Fixed it now. – filbranden May 3 at 0:42

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.