2 Editing a Little Faster
- Additional movement commands
- Quick searches along a single line
- Additional delete and change commands n The repeat command
- Keyboard macros (how to record and play back commands)
- Digraphs
-
Word Movement
Let’s start with movement.To move the cursor forward one word, use the w command.
The b command moves backward one word. Like most Vim commands, you can use a numeric prefix to move past multiple words. For example, 4b moves back four words. -
Moving to the Start or End of a Line
The $ command moves the cursor to the end of a line.
1$ moves you to the end of the first line (the one you’re on), 2$ to the end of the next line, and so on.
The ^ command moves to the first nonblank character of the line -
Searching Along a Single Line
The command fx (forward search) searches the line for the single character x.
therefore, you can space forward five words by using the command 5f:. Note: this only moves five space characters, not five words. If there are multiple spaces between words, this will not move five words!
The F command searches to the left.
The tx (search ‘til) command works like the fx command, except it stops one char-
acter before the indicated character.The backward version of this command is Tx.
Sometimes you will start a search, only to realize that you have typed the wrong com- mand.You type f to search backward, for example, only to realize that you really meant F.To abort a search, pressas the search key. So f is an aborted for- ward search. (Note: cancels most operations, not just searches.) -
Moving to a Specific Line
G
command: 3G puts you on line 3,
With no argument, it positions you at the end of the file.gg
, it positions you at the begin of the file. -
Telling Where You Are in a File
:set number
To turn it on, use this command::set nonumber
-
Where Am I?
The CTRL-G command displays a status line that indicates where you are in the file. For example:“c02.txt” [Modified] line 81 of 153 —52%— col 1
This indicates that you are editing a file called c02.txt, and that it has been modified since the editing started.The cursor is positioned on line 81 out of a total of 153, or about 52% of the way through the file.The cursor is currently sitting in column 1. -
Scrolling Up and Down
The CTRL-U command scrolls up half a screen of text.
The CTRL-D command scrolls you down half a screen. -
Deleting Text
The dw command deletes a word.
The d3w com- mand deletes three words.
In fact, the d command may be followed by any motion command, and it deletes from the current location to the place where the cursor winds up. (Therefore, we say the syntax of the d command is d motion.)
The $ command moves to the end of a line.The d$ command deletes from the cursor to the end of the line, as seen in Figure 2.11. A shortcut for this is the D command. -
Where to Put the Count (3dw or d3w)
The commands 3dw and d3w delete three words. If you want to get really picky about things, the first command, 3dw, deletes one word three times; the command d3w deletes three words once.This is a difference without a distinction.
You can actually put in two counts, however (for example, 3d2w).This command deletes two words, repeated three times, for a total of six words. -
Changing Text
The c command changes text. It acts just like the d command, except it leaves you in insert mode.
For example, cw changes a word. Or more specifically, it deletes a word and then puts you in insert mode.
the cw and dw commands.Whereas cw deletes the text up to the space following the word (and then enters insert mode), the dw command deletes the word and the space following it. -
The . Command
It repeats the last delete or change command. For instance, suppose you are editing an HTML file and want to delete all the<B>
tags.You position the cursor on the first < and delete the<B>
with the command df>.You then go to the < of the next</B>
and kill it using the . command. -
Joining Lines
The J command joins the current line with the next one. A space is added to the end of the first line to separate the two pieces that are joined. 3J -
Replacing Characters
The rx command replaces the character under the cursor with x.
The r command can be preceded with a count, indicating the number of charac- ters to be replaced.
Be careful where you place the count.The 5rx command replaces five characters with
the character x, whereas r5x replaces the character under the cursor with 5 (r5) and then deletes a character (x). -
Changing Case
The ~ command changes a character’s case. It changes uppercase to lowercase and vice versa. If a count is specified, the count characters are changed. Figure 2.17 contains examples. -
Keyboard Macros
The . command repeats the preceding change. But what if you want to do something more complex than a single change?
Theqcharacter
command records keystrokes into the register named character. (The char-
acter must be between a and z.)
To finish recording, just type a q command.You can now execute the macro by
typing the @character command. (This can be preceded by a count, which will cause the macro to be executed that number of times.)
Take a look at how to use these commands in practice.You have a list of filenames that look like this:
stdio.h
fcntl.h
unistd.h
stdlib.h
And what you want is the following: #include “stdio.h”
#include “fcntl.h”
#include “unistd.h”
#include “stdlib.h”
qa ^ i#include “<Esc> $ a”<Esc> j q
- Digraphs
Some characters are not on the keyboard—for example, the copyright character (©). To type these letters in Vim, you use digraphs, where two characters represent one.To enter a ©, for example, you type CTRL-Kc0.