How to run Unix Commands in vi/vim editor

There are some scenarios where unix admin wants to run unix commands and insert the output of unix command in vi editor without leaving it. This can be possible using below steps :


Example : Run the ifconfig command within the /etc/hosts file.

[email protected]:~$ sudo vi /etc/hosts


Insert UNIX Command Output in vi editor :

To insert the output of a UNIX command into a file, immediately after the cursor use below syntax in vi editor


Example: Insert the output of hostname in /etc/hosts file.

[email protected]:~$ sudo vi /etc/hosts


When we press enter , the output of hostname command will be inserted in the file just after the cursor posotion.

Starting Shell in vi Editor

We can start the UNIX shell within the editor , to start a shell use below command :


Type of a shell that is started is determined by the $SHELL variable. And to return back to editor use exit or Ctrl-D

Example : Start sh shell in vi editor :

[email protected]:~$ sudo vi /etc/hosts


Note: Above discussed features becomes very useful if you are using vi/vim to document a UNIX command and you wanted to include examples of the output from this command.

1 Response

  1. Shawn H Corey says:

    You can filter lines through a shell command. For example, to sort some lines:

    Go the the top of the lines and press: ma

    Go to the bottom of the lines and press: !’asort
    and then RETURN.

    The lines will be sorted. 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

Pin It on Pinterest