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.
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
| 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 |
| Non-Linux FOSS: Seashore | May 10, 2013 |
- RSS Feeds
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Using Salt Stack and Vagrant for Drupal Development
- New Products
- Validate an E-Mail Address with PHP, the Right Way
- Dynamic DNS—an Object Lesson in Problem Solving
- Drupal Is a Framework: Why Everyone Needs to Understand This
- A Topic for Discussion - Open Source Feature-Richness?
- Download the Free Red Hat White Paper "Using an Open Source Framework to Catch the Bad Guy"
- Tech Tip: Really Simple HTTP Server with Python
- Please correct the URL for Salt Stack's web site
2 hours 18 min ago - Android is Linux -- why no better inter-operation
4 hours 34 min ago - Connecting Android device to desktop Linux via USB
5 hours 2 min ago - Find new cell phone and tablet pc
6 hours 53 sec ago - Epistle
7 hours 29 min ago - Automatically updating Guest Additions
8 hours 38 min ago - I like your topic on android
9 hours 24 min ago - This is the easiest tutorial
16 hours 27 sec ago - Ahh, the Koolaid.
21 hours 38 min ago - git-annex assistant
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?