Rollo Rollo - 1 year ago 109
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 Source

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


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

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download