Inside a function, saved multiple lines of external command output to a variable.
How to delete a line from the variable? Like first line.

I can run let tmp = split(variable, "\n") to assign the variable to a tmp list, then delete an item. Not sure if there is any easy way.

2 Answers 2


You may try this substitute command to delete, e.g the 3rd line

substitute(var, '\v^((.{-}\n){2}).{-}\n(.*)', '\1\3', '')

The regex works as follows

  • ^((.{-}\n){2}) matches the first 2 lines and capture it in group \1
  • .{-}\n matches the next line
  • (.*) matches the rest of the variable and capture it in group \3

\{-} matches 0 or more of the preceding atom, as few as possible


It depends on how you get the result of your external command but you might be interested in :h systemlist().

It executes the command given as parameter and returns a list containing every lines of the output. You can then use :h list-functions on the result or simply listVar[1:].

I think that is more robust and portable than matching the lines and doing some substitutions.

Here is an example:

" Get the result of ls has a list
let output = systemlist('ls')
echo output    " ['autoload', 'CODE_OF_CONDUCT.md', 'CONTRIBUTING.md', 'doc', 'plugin', 'README.md']

" Remove first line
let output = output[1:]
echo output    " ['CODE_OF_CONDUCT.md', 'CONTRIBUTING.md', 'doc', 'plugin', 'README.md']

" Remove README.md
call filter(output, 'v:val != "README.md"')
echo output    " ['CODE_OF_CONDUCT.md', 'CONTRIBUTING.md', 'doc', 'plugin']

" Remove all *.md files
call filter(output, 'v:val !~ ".md"')
echo output    " ['doc', 'plugin']


  • Could you show how such matchings and substitutions would look?
    – mroavi
    Feb 28, 2021 at 14:44
  • 1
    @mroavi I edited my answer with an example.
    – statox
    Mar 1, 2021 at 9:28

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.