Getting Started with Vi
Most people who are getting started with Linux or UNIX today already know how to use graphical WYSIWYG (what you see is what you get) editors, with heavy use of the mouse, icons and pull-down menus. When they first encounter the traditional UNIX/Linux editor, vi, it may seem awkward and not very powerful, but the opposite is true.
As with graphical editors, you can select a range of text and execute a command to make changes, move the cursor by indicating where you want it to be and insert text by typing at the keyboard. The difference is that vi doesn't use the mouse; you use the keyboard to specify changes or move the cursor. In order for the entire keyboard to be available for commands, vi has separate command and insert modes.
Modes can be confusing at first. With most editors, whatever you type always goes into the document, and editing is done using a mouse or perhaps keyboard shortcuts. With vi, command mode uses the whole keyboard to specify editing commands. Only when you issue an insert command does the keyboard add text to the document.
Most new vi users learn only a few basic commands: the arrow keys, i for insert, x to delete a character, dd to delete a line, :wq to save and exit and perhaps a few others. They quickly grow tired of leaning on the same keys over and over but are afraid of learning a huge list of commands.
Instead of merely providing a list to memorize, here we explore the structure behind them, which makes the commands easier to remember and allows you to become a vi power user quickly. There are several basic categories of commands, including inserting text, moving the cursor, block edit commands, colon (ex) commands, options and miscellaneous commands.
The first command to learns in vi is i. Typing i puts the editor into insert mode, where everything you type is inserted into the document until you press the Esc key. But i is not the only way to insert text.
To add text after the cursor, such as at the end of the line, use a. Or jump to the end of the line and append text with A. Similarly, I inserts at the beginning of the line. With these and other insert commands, always press Esc to return to command mode.
Use o to open a new line after the current one and insert text there or O to open before the current line.
Replace the current character with s, or replace many characters using R, similar to overwrite mode in a graphical editor.
Nearly all vi commands allow a numeric prefix, which usually repeats the command a specified number of times. For example, type 3iHooray then press Enter and Esc. It inserts three lines of “Hooray” into your buffer. The s command is different; here, the number indicates how many characters are to be replaced: 5s replaces five characters with whatever you type.
A few control keys have special meaning in insert mode. You can use the arrow keys to move around, but don't forget you're still in insert mode. Also, Ctrl-T and Ctrl-D, at the start of a line, can be used to indent. Ctrl-T moves to the next level of indentation, and Ctrl-D moves back. Use these with the autoindent and shiftwidth options (described in the Options section below) for best results.
Graphical editors move around mainly by clicking the mouse. Some vi clones offer mouse support, but vi and all its clones share movement commands that can move you around quickly. The simplest are the arrow keys or the equivalent letters: h (left), j (down), k (up) and l (right). If these are all you use, however, you are missing out on the full power of vi.
You can move forward and back on a line by holding down the right or left arrow key (or l or h), but there are easier ways. Go to the beginning of the line with ^ or the end with $. To jump one word at a time, use w or W to go forward or b or B to go back. You also can use e or E to jump to the end of the current word. For lowercase w, b and e, a word is a sequence of alphanumeric characters; the uppercase versions use spaces to separate words.
Another way to move around on the current line is to jump to a particular character. For example, to move the cursor to the next s on a line, type fs. Use F for the previous one: Fs. Or use ts to go right before the s and Ts to go the other way. Change s to search for any character. Type , (comma) to repeat any of these searches.
To move the cursor to the top, middle or bottom of the screen, use H, M or L (uppercase). For H or L, a numeric prefix indicates how many lines from the top or bottom of the screen you want to go; it has no effect on M.
To jump back and forth by sentences, type ( or ); for paragraphs, use { and }. If you're editing source code with a lot of brackets, try the % command: put the cursor on any of the following characters: (, ), [, ], { or }, and press %; you'll be taken to the matching bracket.
Often the best way to move around is by searching with the / command. For example, type /hello and press Enter to jump to the next “hello” in the document. Instead of “hello”, enter any regular expression. The n command repeats the search; N goes the other way. Or use ? to search backward in the file. Search is case-sensitive; I discuss the ignorecase option later in this article.
With most movement commands, a numeric prefix can be used to repeat. For example, 5w moves you forward five words, 2n jumps not to the next search match, but to the one after that, and 5fs jumps to the fifth s to the right of the cursor.
Today’s modular x86 servers are compute-centric, designed as a least common denominator to support a wide range of IT workloads. Those generic, virtualized IT workloads have much different resource optimization requirements than hyperscale and cloud applications. They have resulted in a “one size fits all” enterprise IT architecture that is not optimized for a specific set of IT workloads, and especially not emerging hyperscale workloads, such as web applications, big data, and object storage. In this report, you will learn how shifting the focus from traditional compute-centric IT architectures to an innovative disaggregated fabric-based architecture can optimize and scale your data center.
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
| 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 |
| Non-Linux FOSS: Seashore | May 10, 2013 |
| Trying to Tame the Tablet | May 08, 2013 |
| Dart: a New Web Programming Experience | May 07, 2013 |
- New Products
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Drupal Is a Framework: Why Everyone Needs to Understand This
- A Topic for Discussion - Open Source Feature-Richness?
- Home, My Backup Data Center
- Trying to Tame the Tablet
- RSS Feeds
- New Products
- What's the tweeting protocol?
- Dart: a New Web Programming Experience
- Reply to comment | Linux Journal
1 hour 39 min ago - Drupal is an Awesome CMS and a Crappy development framework
6 hours 18 min ago - IT industry leaders
8 hours 40 min ago - Reply to comment | Linux Journal
1 day 1 hour ago - Reply to comment | Linux Journal
1 day 4 hours ago - Reply to comment | Linux Journal
1 day 5 hours ago - great post
1 day 5 hours ago - Google Docs
1 day 6 hours ago - Reply to comment | Linux Journal
1 day 11 hours ago - Reply to comment | Linux Journal
1 day 11 hours ago
Enter to Win an Adafruit Prototyping Pi Plate 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 Prototyping Pi Plate 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
- Next winner announced on 5-21-13!
Free Webinar: Linux Backup and Recovery
Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.
In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.




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?