0
  1. Is there a way I can directly import/insert the output of any linux command into vim editor? For example: pwd in currently open vim buffer.

Also

  1. From terminal how can I directly open command output for manipulation? For example: opening lshw output in vim and filter unwanted info and quick save file.
2

Import shell output

Launch vi/vim and try:

!! pwd

The output will be pasted in the current buffer

See for the various possibilities:

:he !!

Pipe stdout to vim

Try:

pwd | vi -
  • !!pwd in vim execution mode doesnt seems to work for me. However, as @statox suggested :read !pwd works perfectly. And thankyou for pipe stdout to vim - exactly what I needed :D – Avi Mehenwal Apr 27 '18 at 14:52
  • 1
    @AviMehenwal to clarify this answer: !! should be use in normal mode (not in command line mode). This will open the command line with :.! and you can then type your command to get :.!pwd which means insert the result of the external command !pwd at the current line . you can also use :$!pwd to put the result on the last line, :1!ls -l to put the result on the first line or use whatever line number you want. – statox Apr 27 '18 at 15:02
  • Amazing ... hacky way to get things done :) Thanks for explanation and it works like you suggested. – Avi Mehenwal Apr 27 '18 at 16:33
  • 1
    @statox :.!pwd does not mean "insert the result of the external command at the current line". It means "write the current line as the input to the external command and replace this line with the output". The current line is being filtered through the external command. To simply insert the output of a command after a certain line, the desired address should be used as the range for :[range]read !cmd. – jamessan Apr 29 '18 at 19:31
5

You could use :read (see :h :read):

:[range]r[ead] [++opt] !{cmd}
          Execute {cmd} and insert its standard output below
          the cursor or the specified line.  A temporary file is

For example: :read ! pwd will put the current working directory at the cursor position.

Note that you can pipe your command to grep to filter what you want.

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.