RE: Substitute tabs for specific column locations
-Original Message- From: Tim Chase [mailto:[EMAIL PROTECTED] Sent: Sunday, January 21, 2007 11:08 PM To: David Fishburn Cc: vim@vim.org Subject: Re: Substitute tabs for specific column locations So for each row, replace the tab with an appropriate number of spaces to ensure that column 2 (no matter the length of column 1) will always start in column 20. The same for column 3, but starting at column 25. This is my poor man's formatting utility. I can change the input in anyway I want (additional tabs or newline characters, strings and so on). I just cannot line it up appropriately before my plugin gets it to display it on the screen. I was hoping I could do 1 substitute command that would do something like this: s/\(.\{-}\)\t\(.\{-}\)\t\(.\{-}\)\t\n/\1\%20c\2\%25c\3\r/g You have to tweak the spacing yourself, manually-ish. Vim7 provides a repeat() function that, in earlier versions, you'd have to recreate yourself. Thus, you can do something like this one-liner (broken into several lines to make the parts more obvious) :%s/^\([^\t]*\)\t\([^\t]*\)\t\([^\t]*\)/\= matchstr(submatch(1).repeat(' ', 20), '^.\{20}'). matchstr(submatch(2).repeat(' ', 25), '^.\{25}'). submatch(3) As usual Tim, bang on. I realized this technique would not work for me since we are limited to the use of only 9 back references. I could do this in a loop, but I took a different approach and let the Perl code that generated the block in the first place take care of the formatting. Turned out pretty easy and fast using that approach. Thanks for the help. Dave
Substitute tabs for specific column locations
Vim 7 WinXP SP2 I have a string in this format: \nr1c1\tr1c2\tr1c3\t\nr2c1\tr2c2\tr2c3\t\nr3c1\tr3c2\tr3c3\t\n r1 = row 1 c1 = column 1 I also have this information and requirement: c1 should display at column 1 c2 should display at column 20 c3 should display at column 25 So for each row, replace the tab with an appropriate number of spaces to ensure that column 2 (no matter the length of column 1) will always start in column 20. The same for column 3, but starting at column 25. This is my poor man's formatting utility. I can change the input in anyway I want (additional tabs or newline characters, strings and so on). I just cannot line it up appropriately before my plugin gets it to display it on the screen. I was hoping I could do 1 substitute command that would do something like this: s/\(.\{-}\)\t\(.\{-}\)\t\(.\{-}\)\t\n/\1\%20c\2\%25c\3\r/g In other words, replace the tabs with a specified column position. But looking at the help the \%##c is only used for matching, not the replacement part (a simple test confirmed). c1, c2, c3 data can all be different lengths for each row, so I can just added a preset number of spaced. Does anyone have any suggested approaches? TIA, Dave
Re: Substitute tabs for specific column locations
So for each row, replace the tab with an appropriate number of spaces to ensure that column 2 (no matter the length of column 1) will always start in column 20. The same for column 3, but starting at column 25. This is my poor man's formatting utility. I can change the input in anyway I want (additional tabs or newline characters, strings and so on). I just cannot line it up appropriately before my plugin gets it to display it on the screen. I was hoping I could do 1 substitute command that would do something like this: s/\(.\{-}\)\t\(.\{-}\)\t\(.\{-}\)\t\n/\1\%20c\2\%25c\3\r/g You have to tweak the spacing yourself, manually-ish. Vim7 provides a repeat() function that, in earlier versions, you'd have to recreate yourself. Thus, you can do something like this one-liner (broken into several lines to make the parts more obvious) :%s/^\([^\t]*\)\t\([^\t]*\)\t\([^\t]*\)/\= matchstr(submatch(1).repeat(' ', 20), '^.\{20}'). matchstr(submatch(2).repeat(' ', 25), '^.\{25}'). submatch(3) It builds the expressions, padding the submatch(m) to N places, which you repeat, add to your original portion, and then lop off anything that extends beyond those N places. There are also some column-alignment plugins/scripts if you dig around on the vim site. Or perhaps Dr. Chip's sight. I usually just use the above on the rare occasion I need to do as much, on the grounds that I don't remember where to look, don't always have access to a network connection to download plugins. Besides, I tend to fly with a pretty stock vim with no add-ins. But other do sing the praises of the align.vim script. :) HTH, -tim