Getting Started with Vi
In graphical editors, to change a block of text, click and drag the mouse to highlight the text, then click an icon or menu option or type a keyboard shortcut. Although vi does not utilize the mouse, the process is surprisingly similar.
As we have just seen, vi has a rich set of movement commands. Any of these will work as an argument to a block editing command (for example, dw to delete a word). In vi, instead of first highlighting with a mouse and choosing an action, type the block edit command (d) first, and then a movement command (w) to specify the block of text. As a shortcut, you can type the block edit command twice to affect only the current line, for example, dd deletes the current line.
As we've seen, d is for delete. Whenever you delete text, that text is remembered, much like the cut command in a graphical editor. To paste the text (“put” in vi parlance), type p to put it after the cursor's position (or uppercase P for before).
That takes care of cut and paste, but what about copy? In vi, it's called yank and is done with the y commands. These are exactly like the d commands, except they don't delete anything: yy yanks the current line, yw yanks a word and so on.
To replace a chunk of text, you could delete it first and use i to insert a replacement, or use c (change) instead, and you are placed into insert mode to type the replacement text; press Esc when done. Use with a movement command (such as, cw) or change the whole line with cc.
Programmers will love the > and < commands. These shift a range of text to the right or left. To shift a block of code, put the cursor on the { or } character at the beginning or end of the block and type >% to shift it to the right. Any other movement command can be used in place of %, or use >> to shift one line. Use < to shift left. See also Ctrl-T and Ctrl-D (while in insert mode) and the shiftwidth option below.
Lastly, use ! to filter text through an external program. Type the program to run, and the selected part of the file is passed as standard input; the output of the program is inserted into the document in its place. For example, !! filters the current line; !} filters a paragraph. One command that is particularly useful with this is the UNIX/Linux filter fmt, which reformats paragraphs. To run an external program without modifying the file, see the :! command in the “Colon (ex) Commands” section.
Any numeric prefix given is passed along to the movement portion of the command, whether it is given first or between the edit and movement command. For example, either 3dw or d3w deletes three words; 3yy or y3y yanks (copies) three lines.
Vi inherits as class of commands from its predecessor ex, accessible through what is sometimes called colon mode. Common examples are :wq (write and quit) and :s/X/Y/ (substitute Y for X). Type : followed by an ex command, and press Enter. After the command is done, vi returns to command mode.
There are several variations on the :w and :q commands. To save the file and continue editing, use :w. You can quit vi with :q, but only if your work is saved; to quit no matter what, use :q! (dangerous). Try :w! to override read-only mode if :q gives an error. Many people use :wq! to write and exit. In vim or nvi, this is like typing :w! and then :q if there was no error while saving. But some other versions of vi treat it like :w and :q!, which means if there was any error saving the file, the work would be lost. Because of this, it is a better habit to use :wq.
The substitution command is extremely powerful. In its simplest form, :s/X/Y/ replaces one occurrence of X with Y on the current line. To replace all occurrences on a line, add g (global) to the end, for example, :s/foo/bar/g. Add c to the end to confirm each change, for example, :s/foo/bar/gc. To change a range of lines, put two line numbers separated by a comma in front, for example, :10,20s/foo/bar/g replaces all occurrences of “foo” with “bar” on lines 10 through 20. Use 1 for the first line of the file and $ for the last line; to replace on all lines, use :1,$s/foo/bar/g (instead of 1,$, you can use %, as in :%s/foo/bar/g).
Another useful command is :g/X/. It allows you to run any ex command for all lines matching the pattern X. For example, to print all lines that contain “hello” on the screen, use :g/hello/p (:p is an ex command for printing lines to the screen). You even can combine it with the :s/// command. To change “foo” to “bar” on all lines that contain “hello”, use :g/hello/s/foo/bar/g.
In both :s/X/Y/ and :g/X/, the expression X is a regular expression, not only a static string. Set the nomagic option to disable this (see below). To learn about regular expressions, I recommend the book Mastering Regular Expressions by Jeoffrey Friedl (O'Reilly & Associates, 2nd ed., 2002).
Use :! to run an external program. :!ls prints the current directory listing. As a convenience, the percent character (%) in the command is replaced with the name of the file you are currently editing, so use :!chmod +x % to make the file executable. Typing :!! repeats the previous :! command. Unlike the command-mode ! commands, this does not modify the text; the output of the program, if any, is simply displayed.
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Sponsored by AMD
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
| Designing Electronics with Linux | May 22, 2013 |
| Dynamic DNS—an Object Lesson in Problem Solving | May 21, 2013 |
| Using Salt Stack and Vagrant for Drupal Development | May 20, 2013 |
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
- Designing Electronics with Linux
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Dynamic DNS—an Object Lesson in Problem Solving
- Using Salt Stack and Vagrant for Drupal Development
- New Products
- Build a Skype Server for Your Home Phone System
- Validate an E-Mail Address with PHP, the Right Way
- A Topic for Discussion - Open Source Feature-Richness?
- Why Python?
- Tech Tip: Really Simple HTTP Server with Python
- Great
1 hour 10 min ago - Reply to comment | Linux Journal
1 hour 18 min ago - Understanding the Linux Kernel
3 hours 33 min ago - General
6 hours 3 min ago - Kernel Problem
16 hours 5 min ago - BASH script to log IPs on public web server
20 hours 32 min ago - DynDNS
1 day 8 min ago - Reply to comment | Linux Journal
1 day 41 min ago - All the articles you talked
1 day 3 hours ago - All the articles you talked
1 day 3 hours ago
Enter to Win an Adafruit Pi Cobbler Breakout Kit for Raspberry Pi

It's Raspberry Pi month at Linux Journal. Each week in May, Adafruit will be giving away a Pi-related prize to a lucky, randomly drawn LJ reader. Winners will be announced weekly.
Fill out the fields below to enter to win this week's prize-- a Pi Cobbler Breakout Kit for Raspberry Pi.
Congratulations to our winners so far:
- 5-8-13, Pi Starter Pack: Jack Davis
- 5-15-13, Pi Model B 512MB RAM: Patrick Dunn
- 5-21-13, Prototyping Pi Plate Kit: Philip Kirby
- Next winner announced on 5-27-13!
Free Webinar: Hadoop
How to Build an Optimal Hadoop Cluster to Store and Maintain Unlimited Amounts of Data Using Microservers
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Some of key questions to be discussed are:
- What is the “typical” Hadoop cluster and what should be installed on the different machine types?
- Why should you consider the typical workload patterns when making your hardware decisions?
- Are all microservers created equal for Hadoop deployments?
- How do I plan for expansion if I require more compute, memory, storage or networking?




Comments
I hate vi
Why on earth would anyone ever use this now? Basic text editors much more intuitive than vi have been available for over a decade. Why hasn't this abortion of a text editor been put to rest? There is no reason to even learn it except for some reason, a lot of Linux certification tests have vi questions in them.
Why in God's name does this text editor have it's own command line? It's a TEXT EDITOR. jed and kate are more powerful for crying out loud. Why should I feel like I should to learn this cumbersome piece of crap?
"Even if you use a different editor for most of your work, it helps to know the basics of the ubiquitous vi." <==WHY?