Rollo Rollo - 4 months ago 19
Bash Question

How can I run a shell command in vim and return the resulting string into a VimScript variable?

How can I do this extremely simple operation in vim?

let g:string_from_shell = run_in_shell('awk ...')


(command use for exposition only)

Answer

You can use system function to execute system/shell commands in vim. Then, you can store its output to a vim variable.

Syntax :

  :let variable = system("shell command")

You can check its output by printing the variable like

  :echo variable

Example:

  :let list = system("ls -ltr")
  :echo list

To know more about it, give :help system() in command line.