Find and Replace - Regular Expressions in Vi
The previous article Quick Reference Vi talking about the basic commands of the editor. Today we will see how to do a little search and replace text within Vi.
| / text | Find the text forward |
| ? text | Find the text backwards |
| n | Find the next time the text appears in the document |
| N | Find the text which has appeared previously |
The search and replace function executes the :s It is used commonly ranges and when combined with the :g
:s/patron/string/flags Replaces second string pattern with the flags.
| g | Flag - Replace all occurrences of the pattern |
| c | Flag - Confirm replacement |
| & | Repeat the last command: s |
| . (dot) | A single character except newline (newline) |
| * | All occurrences of any character |
| [...] | Any character set specified in |
| [^...] | Any character not specified in the joint |
| ^ | Anchor - the beginning of the line |
| $ | Anchor - end of the line |
| \ < | Anchor - the beginning of a word |
| \> | Anchor - end of a word |
| \ (... \) | Grouping - usually able to group |
| \ n | Content Group |
Examples
| [AZ] | The set of September or uppercase A through Z. |
| [az] | The package of up to saragossa sensitive. |
| [0-9] | The conjuto 0 to 9, all the numbers. |
| [./=+] | The package contains. (Point) / (diagonal) = y +. |
| [-AF] | The whole shift from A to F and the region. |
| [0-9 AZ] | The set contains all capital letters, numbers and spaces. |
| [AZ] [a-zA-Z] | In the first position, all the uppercase letters A through Z and the second position of the character, the set containing all the letters. |
| / H0la / | Matches if the line holds the value H0la. |
| / ^ TEST $ / | Matches if the line contains TEST by itself. |
| / ^ [a-zA-Z] / | If lalinea begins with any of these letters. |
| / ^ [az] .* / | If the first character is az and if at least one point but still below. |
| Matches if the line ends in 2134. | |
| / \ (21 | 35 \) / | Matches if the line holds 21 or 25. |
| / [0-9] * / | Matches zero or more if there are numbers on the line |
| /^[^#]/ | True if the first character in the line is not a # |
Note: Regular Expressions
1 .- It is case sensitive
2 .- It must be used in place of the patron
Accountants
Almost all commands can be preceded by a number, which specifies the time that the command should be run. For example, 5dw delete 5 words and 3fe move the cursor forward to the third occurrence of the letter e. It is even possible to insert the same line 100 times.
Ranges
The ranges can be written with commands colon (:) and be executed in one or more lines. For :3,7d delete lines 3-7. The ranges are combined with the :s for a replacement on several lines, including: $s/patron/string/g to replace the current line from the end of the document.
| n, m | Range - Lines nm |
| :. | Range - Current Line |
| $ | Range - Ultima Online |
| 'C | Range - Marker c |
| :% | Range - All lines in the file |
| : g / pattern / | Range - All lines containing pattern |
Archives
| : w file | Write to file |
| : r file | Read from file |
| n | Go to the next file |
| : p | Go to the previous file |
| : e file | Edit the file |
| ! program | Replace the line with the output from program |
Other
| ~ | Change case-insensitive |
| J | Joining lines |
| . | Repeat the last text change command |
| u | Undo the last change |
| U | Undo all changes on line |
Popularity: 5% [?]







On 06 May 2008 at 9:05 am # talishte
Vim rocks
[Reply]