E-Mail:
Author Avatar

Text Editing in vi

Text Editing in vi

In my opinion, when it comes to editing in the console, there really are only two programs - emacs and vi. Each has a solid camp of users, all of whom are pretty adamant in their claims that their preferred editor “rocks.” I happen to be a vi guy and, yes, vi rocks. Even though the command set is typcially cryptic, vi has a rich toolset that makes it perfect for editing shell scripts and configuration files. I don’t use it much for editing long text documents, but for scripts and config files, it simply can’t be beat.

To start vi with a blank document, open the console window and enter the command vi. To open an existing document, type in the vi command followed by the file name.

vi has only two modes - input mode and command mode. They are exactly as they sound. Input mode is used to enter text, while command mode is the method by which you give vi instructions on saving files, exiting, etc. The default mode for vi is command mode. In other words, vi stays in command mode until you tell it differently.

It’s easier to learn the hieroglyphics of vi if you can categorize the different types of vi commands. These groups include general commands, commands that change to the input mode, text commands, copy and paste commands, commands for moving within a document, and commands for searching for and replacing text.

Some useful vi commands:

General Commands
:q quit
:q! quit with no changes
:w [filename] save the file
:wq save the current file and quit
:r [filename] insert the contents of filename at the current cursor position
Switching to Input Mode
i insert text at the current cursor position
a append text to the right of the cursor
A append text at the end of the line
o insert text in a new line just below the current line
O insert text in a new line just above the current line
Movement Commands
j or <enter> down one line
k up one line
l or <space> right one character
h or left one character
0 move to the beginning of the line
^ move to the first non-blank character on the line
$ move to the end of the line
G move to the end of the file
w forward one word
b backward one word
( move forward one sentence
) move backward one sentence
Changing and Deleting Text
R replace the character under the cursor
cw change the next word to the text you next type
cc change the next line to the text you next type
dd delete the current line
D delete the line from the cursor to the end
d^ delete the line from the beginning to the cursor
Copy & Paste
P paste text last copied to the right of the cursor
p paste text last copied to the left of the cursor
yy copy current line
ye copy from the cursor to the end of the word
Search & Replace
/findtext search from cursor forward for findtext
/ repeat the last forward search
? search from the cursor backward for findtext
? repeat the last backward search
:s/findtext/ replace the first occurrence of findtext
:%s/findtext/ replace all occurrences of findtext

When I first started using vi, I kept a little printed reference just like the one above. Sound like a hint?

One Comment

you forgot ‘x’

What Do You Think?

 


Anti-Spam Image

Want to Start a Blog Here for Free?

Are you an expert in one subject or another? If your goal is to help others and dispense hard-earned information back to the community, stake a claim on your very own Lockergnome blog today! You can write about anything - no matter the topic. Sign-up to start blogging!

Author Avatar
Interview, Linux, Misc - Dec 19, 2007

KOffice vs OOXML: Practical, Not Political

Author Avatar
Myths - Dec 3, 2007

5 Myths That Stymie Linux Growth

Author Avatar
Just Ask Matt - Linux Edition - Nov 20, 2007

Default Sound Card Not Sticking In Ubuntu!