Vim beginners may be puzzled when it comes to doing search and replace operations in Vim. The syntax can be a bit arcane, but after you've practiced, it becomes second nature. Let's take a look at how easy it can be.
Let's start by looking at searches and doing search and replace operations within Vim. You can do a search in normal mode by using /searchstring . This will search forward through the file for searchstring . Likewise, running ?searchstring will search backwards through the file.
After running a search once, you can repeat it by using n in command mode, or N to reverse direction.
When you want to search for a string of text and replace it with another string of text, you can use the syntax :[range]s/search/replace/. The range is optional; if you just run :s/search/replace/, it will search only the current line and match only the first occurrence of a term.
Most of the time, that's not sufficient, so you can add a range like so:
:8,10 s/search/replace/g
In that example the range is from line 8 to line 10. I've also added the "global" option, which tells Vim to replace every occurrence on a line, and not just the first occurrence. Without adding g, your search will match only the first instance of a string in any given line.
Another way to specify the range is to enter visual mode and select the lines that you want to search, and then press : to enter command mode. To enter visual mode from normal mode, press v to select regular visual mode, or V for line selection, or Ctrl-v for block selection. Then select the range in visual mode and press :, followed by the search command you wish to use.
If you want to search an entire file, you can use % to indicate that as the range:
:%s/search/replace/g
You may also wish to be asked for confirmation before Vim makes a substitution. To do this, add the confirm (c) option to the end of the search and replace command: :%s/search/replace/gc. When you run this search, Vim will give you a prompt that looks something like this:
replace with foo (y/n/a/q/l/^E/^Y)?
The "y" and "n" are self-explanatory, but what about the rest? To tell Vim to go ahead and replace all instances of the matched string, answer with a. If you realize that you don't really want to make the changes, you can tell Vim to quit the operation using q. To tell Vim to make the current change and then stop, use l, for last.
^E and ^Y allow you to scroll the text using Ctrl-e and Ctrl-y.
Where you land
Searches in Vim put the cursor on the first character of the matched string by default; if you search for Debian, it would put the cursor on the D. That's usually fine, but Vim has a few options for offsetting the cursor placement from the beginning of the matched string.
To land on the last character in the matched string, rather than the beginning, add an /e to your search:
/Debian/e
That will place the cursor on the n rather than the D. Vim also allows you to specify a cursor offset by line, or from the beginning or end of the string. To have the cursor land two lines above a matched string, for example, use /string/-2. To place the cursor two lines below the string, use /string/+2.
To offset from the beginning of the string, add a /b or /s with the offset that you want. For example, to move three characters from the beginning of the search, you'd use /string/s+3 or /string/b+3 -- "s" for "start" or "b" for "begin." To count from the end of the string, use /e instead, so /string/e-3 will place the cursor on the third character from the last character of the matched string.
Sunday, 31 August 2008
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment