On Wed, Jan 5, 2011 at 9:59 AM, Michal Dorsett <[email protected]> wrote:
>
> Hi,
> Suppose I have the following text in my file:
> 9
> 7 4
> 5 8 3
> And I need it appearing as an array initialization like so:
> int a[3][3] = {
> {9,1,1},
> {7,4,1},
> {5,8,3}
> };
> That is, with all the additional array elements filled with 1's.
Another way of doing it is inserting ,1,1,1 to each line, despite the
number of elements, and then trimming out the exceeding ones.
Put the cursor in the first 9 and record the macro as following:
qqvip => start recording into register q and select whole paragraph
:s/^/{/g<cr> => add leading {
gv:s/$/}/g<cr> => select last selection and add trialing }
gv:s/ /,/g<cr> => add , between numbers
gv:s/}/,1,1,1}/g<cr>
gv:s/\v(\d+),(\d+),(\d+)(.*)}/\1,\2,\3}<cr> => here's the meat,
delete exceeding 1's
{oint a[3][3] = { => go to beginning of paragraph and add the
variable declaration
}i}<esc> => go to end of paragraph and add final }
Of course you should replace <cr> with the enter key.
That should do it :)
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php