Vi super commands
Vi is one of the most powerful text editors, but it really separates the men from the boys. GUI's are highly over rated - all you want is a big black page. Apart from some clear context colouring what more do you need to write code?
One of Vi's strengths is pattern matching with search and replace features. Below I have outlined the reasons why developers choose this as their favourite editor.
Search and replace
Search and replace is done using sed syntax which follows the form: which lines, what to look for, what to replace it with and then how much to do.
Firstly we need to specify a range of lines to apply our change to.
- Missing this option out means the current line.
- n - line number n.
- a,b - from line a to line b.
- .,$ - from the current line to the end of the file.
- % - all lines.
Now we specify the command function, which in this case is search. We do that with a single 's' character.
Next comes the parameter separator. Most people use a forward slash but you can use anything. If your search pattern has a lot of forward slashes then you don't have to delimit them if you use a different separator. I use a greater than symbol - '>'. The following lines are equivalent.
:s/<html/<\/html/
:s><html></html>
Next comes the pattern you are looking for. This is a regular expression which can keep matches in buffers to use in the replacement section.
- [Dd]avid [Nn]ewcomb - camel case match.
- ^davidnewcomb - match davidnewcomb at the start of the line.
- davidnewcomb$ - match davidnewcomb at the end of the line.
- david\(newcomb\) - match davidnewcomb and save newcomb in buffer number 1.
- \(david\)\(newcomb\) - match davidnewcomb and save david in buffer number 1 and newcomb in a buffer number 2.
Add another separator character and now specify what we want to replace the search match with. I'll use the example above.
- nothing - this is the equivelent to delete the text.
- David Newcomb - make sure you have used camel case everywhere.
- nothing - as above
- nothing - as above
- mr \1 - change my title but keep my surname.
- \1 \2 - add a space between my name.
Another separator character and finally add the scope. Without the search scope the pattern matcher will stop after replacing the first occurance on the line. Adding the global flag 'g' will allow the matcher to find and replace all the matches on the line.
:%s/, *\([0-9]\)/,\1/g
This sequence, looks for a comma followed by a zero or more spaces followed by a digit, and save the digit in slot 1. Replace all that text with a comma and the digit from slot 1. Do it for each occurrence on the line.
Macros
How do you change:
0000367: Update the site contact us page 00:30
0000368: Correct the new section 00:30
0000369: How to access webmail 00:15
into this:
0000367,Update the site contact us page,00:30
0000368,Correct the new section,00:30
0000369,How to access webmail,00:15
ready for CVS paste import to Open Office or Excel.
Simples! Go to the first character on the line, hit escape to enter control mode and type:
qaw2s,ESC$3bhcw,ESC0q
Where ESC is pressing the escape key.
How can I remember that!!!! It's all very simple.
q | create a macro |
a | save the macro in slot 'a' |
w | the first command in the sequences, move one word forwards |
2s | delete the next 2 characters and start editing |
, | write a comma |
ESC | to enter control mode |
$ | go to the end of the line |
3b | move 3 words back |
h | more one character to the left |
cw | delete the next word and put into editing mode |
, | write a comma |
ESC | to enter control mode |
0 | go to the start of the line (back to the start) |
q | end the macro definition |
The macro does a whole step and returns to the starting point. Now that the macro is defined you can start using it.
- n - optionally, play the macro n times.
- @ - play the macro.
- a - play macro 'a'
No feedback yet
Form is loading...