Welcome to Linux Knowledge Base and Tutorial
"The place where you learn linux"
Let The Music Play: Join EFF Today

 Create an AccountHome | Submit News | Your Account  

Tutorial Menu
Linux Tutorial Home
Table of Contents
Up to --> Editing Files

· Vi
· vi Basics
· Changing Text in vi
· Moving Around in vi
· Searching in vi
· vi Buffers
· vi Magic
· Command Output in vi
· More vi Magic
· vi Odds and Ends
· Configuring vi

Man Pages
Linux Topics
Test Your Knowledge

Site Menu
Site Map
Copyright Info
Terms of Use
Privacy Info
Masthead / Impressum
Your Account

Private Messages

News Archive
Submit News
User Articles
Web Links


The Web

Who's Online
There are currently, 75 guest(s) and 0 member(s) that are online.

You are an Anonymous user. You can register for free by clicking here

Linux Tutorial - Editing Files - Vi - Command Output in vi
  vi Magic ---- More vi Magic  

Command Output in vi

It often happens that we want the output of UNIX commands in the file we are editing. The sledgehammer approach is to run the command and redirect it to a file, then edit that file. If that file containing the commands output already exists, we can use the :r from ex-mode to read it in. But, what if it doesn't yet exist. For example, I often want the date in text files as a log of when I input things. This is done with a combination of the :r (for read) from ex-mode and a shell-escape.

A shell-escape is when we start from one program and jump out of it (escape) to a shell. Our original program is still running, but we are now working in a shell that is a child process of that program.

To do a shell-escape, we need to be in ex-mode. Next, press the exclamation mark (!) followed by the command. For example, to see what time it is, type :!date. We then get the date at the bottom of the screen with the message to press any key to continue. Note that this didn't change our original text; it just showed us the output of the date command.

To read in a command's output, we need to include the :r command, as in :r!date. Now, the output of the date is read into the file (it is inserted into the file). We could also have the output replace the current line by pressing ! twice, as in !!date. Note that we are brought down to the last line on the screen, where there is a single !.

If we want, we can also read in other commands. What is happening is that vi is seeing the output of the command as a file. Remember that :r <file_name> will read a file into the one we are editing. Why not read from the output of a file? With pipes and redirection, both stdin and stdout can be files.

We can also take this one step further. Imagine that we are editing a file containing a long list. We know that many lines are duplicated and we also want the list sorted. We could do :%!sort, which, if we remember from our earlier discussion, is a special symbol meaning all the lines in the file. These are then sent through the command on the other side of the !. Now we can type


to remove all the duplicate lines.

Remember that this is a shell-escape. From the shell, we can combine multiple commands using pipes. We can do it here as well. So to save time, we could enter

:%!sort | uniq

which would sort all the lines and remove all duplicate lines. If we only wanted to sort a set of lines, we could do it like this


which would sort lines 45 through 112. We can take this one step further by either writing lines 45-112 to a new file with :45,112w file_name or reading in a whole file to replace lines 45-112 with :45,112r file_name.

 Previous Page
vi Magic
  Back to Top
Table of Contents
Next Page 
More vi Magic


Test Your Knowledge

User Comments:

You can only add comments if you are logged in.

Copyright 2002-2009 by James Mohr. Licensed under modified GNU Free Documentation License (Portions of this material originally published by Prentice Hall, Pearson Education, Inc). See here for details. All rights reserved.

There are several different ways to navigate the tutorial.



Security Code
Security Code
Type Security Code

Don't have an account yet? You can create one. As a registered user you have some advantages like theme manager, comments configuration and post comments with your name.

Help if you can!

Amazon Wish List

Did You Know?
You can choose larger fonts by selecting a different themes.


Tell a Friend About Us

Bookmark and Share

Web site powered by PHP-Nuke

Is this information useful? At the very least you can help by spreading the word to your favorite newsgroups, mailing lists and forums.
All logos and trademarks in this site are property of their respective owner. The comments are property of their posters. Articles are the property of their respective owners. Unless otherwise stated in the body of the article, article content (C) 1994-2013 by James Mohr. All rights reserved. The stylized page/paper, as well as the terms "The Linux Tutorial", "The Linux Server Tutorial", "The Linux Knowledge Base and Tutorial" and "The place where you learn Linux" are service marks of James Mohr. All rights reserved.
The Linux Knowledge Base and Tutorial may contain links to sites on the Internet, which are owned and operated by third parties. The Linux Tutorial is not responsible for the content of any such third-party site. By viewing/utilizing this web site, you have agreed to our disclaimer, terms of use and privacy policy. Use of automated download software ("harvesters") such as wget, httrack, etc. causes the site to quickly exceed its bandwidth limitation and are therefore expressly prohibited. For more details on this, take a look here

PHP-Nuke Copyright © 2004 by Francisco Burzi. This is free software, and you may redistribute it under the GPL. PHP-Nuke comes with absolutely no warranty, for details, see the license.
Page Generation: 0.08 Seconds