I'd like to read the contents of a file, and put this into a variable - so I can later use it in a command.

How can I achieve this?

1 Answer 1


See :help readfile():

Read file {fname} and return a |List|, each line of the file as an item. Lines are broken at NL characters. [...]

As general advice, try to get familiar with Vim's help system. In the beginning, it might be hard to find what you need in there, but once you get used to it, it's a very powerful way to quickly find what you need.

  • Thanks! Was a bit difficult to search for though ('read' came up in many places). Will accept the answer once I can in a few minutes. Commented Apr 22, 2020 at 18:13
  • @ChrisStryczynski Agreed finding something specific can be overwhelming... :helpgrep can be helpful sometimes. Asking here is all good too! Just pointing out that Vim help system is one more resource you can use to find what you need.
    – filbranden
    Commented Apr 22, 2020 at 18:34
  • 1
    I know the help system quite well. I wrote my own help pages for my own scripts. But sometimes you just just need the correct search term first to find the correct help page. Thanks.
    – Martin
    Commented Oct 15, 2021 at 6:58

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.