Back when I was a co-op student, one of the developers that trained me had a philosophy that a vi user should learn a new editing trick every day. While I haven't bothered to maintain such a regimen, I do from time to time like to learn new commands as editing situations arise. My latest lesson involved the use of pattern matching to select specific lines within a range, deleting them to a register, and putting them in a different location. Allow me to illustrate with an example.
Suppose I have code like this (familiarity with C-like languages is assumed):
1: char array[3][2];
2:
3: array[0][0] = 'x';
4: array[0][1] = 'y';
5: array[1][0] = 'z';
6: array[1][1] = 'z';
7: array[2][0] = 'y';
8: array[2][1] = '\n';
Let's suppose that I decided that I wanted to group these assignments according to the second index. I want to remove all rows that contain [1] as the second dimension's index and move them elsewhere. How can I do that in vim without deleting each row individually?
:3,8g/.*\[1\] /d A
Let's decompose this. The characters in blue specify the range of lines to consider. I only want vim to look at lines 3 through 8. The g tells vim to apply the pattern match to every line in the range. The yellow text is the pattern match itself. Finally, the delete command is highlighted in orange. This command is repeated for each line that matches the pattern. The uppercase A tells vim to append the line that is deleted into register "a". After this command is run, we'd see:
1: char array[3][2];
2:
3: array[0][0] = 'x';
4: array[1][0] = 'z';
5: array[2][0] = 'y';
Later on, when we want to paste these saved rows, we can position the cursor at line 5 and type the following command:
"ap
This tells vim to put the text in register "a" into the current location in the buffer. Now we end up with the following:
1: char array[3][2];
2:
3: array[0][0] = 'x';
4: array[1][0] = 'z';
5: array[2][0] = 'y';
6: array[0][1] = 'y';
7: array[1][1] = 'z';
8: array[2][1] = '\n';
To clear the register, issue this command:
:let @a = ""
Thursday, February 26, 2009
A vim lesson
Posted by Aaron at 19:30 0 comments
Labels: code
Friday, February 20, 2009
Spend to Save? Only in Alberta...
I'm glad that Ed Stelmach isn't a financial advisor. Today I was reading Don Braid's latest column in the Calgary Herald, where Braid writes:
When Liberal Leader David Swann scolded the premier Thursday for not saving more, Stelmach said the government was right not to, because the money would just have been lost on the markets.
Swann looked like he'd been socked by a rhetorical Whoopee cushion.
I wanted to read the actual text of the exchange in the legislature, so I looked it up in Hansard:
Dr. Swann: Thank you, Mr. Speaker. Today the finance minister gave a news conference that repeated previously announced economic initiatives to cloud the fact that this government has mismanaged the Alberta economy. For years this government has ignored the advice of experts to save during times of plenty, and now, once again, Alberta is in trouble. A former Premier admitted there was no plan for the boom. Will this Premier admit there is no plan for the bust?
Mr. Stelmach: Mr. Speaker, the hon. Treasurer today delivered a report that shows that our savings fund, the heritage savings trust fund, has taken a loss, but I would submit that if we would have listened to the advice of the member across, we would have lost even more money through this unbelievable economic turmoil...
Doesn't the Premier understand that a loss isn't locked in unless the securities are sold? Nobody's disputing that the value of the Heritage Fund is down on paper, but if that money is invested for the long term in accordance with the intentions of the fund, there will always be gains in the long term. To hear Stelmach tell it, I'd be better off had I not saved a dime over the past few years because the markets have since tanked. What kind of financial literacy is that?
The opposition should have been gnashing their teeth over such an ignorant response, but these are the Alberta Liberals, so they just say, "Oh," and sit back down.
Posted by Aaron at 23:00 0 comments
Labels: AlbertaPolitics