4
  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.
avimehenwal
  • 909
  • 9
  • 18

2 Answers2

10

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.

statox
  • 49,782
  • 19
  • 148
  • 225
4

Import shell output

Launch vi or vim and try:

!!pwd

The output will be pasted in the current buffer

Checkout the possible options:

:help !!

Pipe stdout to vim

Try:

pwd | vi -
D. Ben Knoble
  • 26,070
  • 3
  • 29
  • 65
  • 1
    !!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 – avimehenwal Apr 27 '18 at 14:52
  • 3
    @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. – avimehenwal Apr 27 '18 at 16:33
  • 3
    @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
  • pwd | vim - worked great – stevec Jun 04 '23 at 00:42