Vi 201 - The Shell Game (Page 5 of 8 )
The guys who created vi built in all kinds of creature comforts designed to reduce your interaction with the shell to a minimum. One of the coolest ideas they came up with was a "shell filter", which allows you to replace shell commands with their output without needing to leave the editor at all.
Try this - open up a blank document and type
Today's date is date
Now exit insert mode, position the cursor over the second
"date" in that line, and type
!!bash
The line should now read
Today's date is Tue May 2 15:54:13 IST 2000
or whatever the current date and time happens to be on your
system. Vi passes the word under the cursor to the interpreter specified by the user - the "bash" shell, in this case - as a command, and then replaces the word with the output of that command.
Despite these time-saving features, there are times when you'll want to execute a shell command directly. In vi, you can do this with
:! <shell-command>
So, if you needed a quick directory listing of the /home
directory, you could type
:! ls -l /home
and vi would pass the command to the shell, and display the
output to you.
Finally, vi also allows you to spawn a new shell with the
:shell
command. You can then execute shell commands and run other
programs - even a new instance of vi, although only the truly warped among you would find this entertaining.
Once you're done, simply log out of the spawned shell by typing
^-D
and you'll be returned to your original vi session.
This article copyright 2000. All rights reserved.Next: First Aid 101 >>
More Administration Articles
More By Vikram Vaswani, (c) Melonfire