Re: replace with a number sequence
Hi Vimers, Can anyone please help me with making the vim indent verilog files properly?! Thanx in advance!
Re: replace with a number sequence
On Thu 1-Feb-07 8:23am -0600, Naim Far wrote: > I liked the following replacement command, and I found it very usefull: > :let n=0|g//s//\=n/|let n+=1 > > But, could anyone please explain the way it works?! > I used the "help :=" to try and understand it, but the vim help says the > ":=" command displays the line number, which cannot explain the way the > command above works When pattern is missing in :g or s:, it uses the last pattern of global, substitute or search - in the OPs example, it was being used after a search. For \= see :h sub-replace-\= In the example, it is evaluating the variable - so its value is substituted. -- Best regards, Bill
Re: replace with a number sequence
On Thu 1-Feb-07 8:23am -0600, Naim Far wrote: > I liked the following replacement command, and I found it very usefull: > :let n=0|g//s//\=n/|let n+=1 > > But, could anyone please explain the way it works?! > I used the "help :=" to try and understand it, but the vim help says the > ":=" command displays the line number, which cannot explain the way the > command above works -- Best regards, Bill
Re: replace with a number sequence
Hi, I liked the following replacement command, and I found it very usefull: :let n=0|g//s//\=n/|let n+=1 But, could anyone please explain the way it works?! I used the "help :=" to try and understand it, but the vim help says the ":=" command displays the line number, which cannot explain the way the command above works Thanx in advance... jose isaias cabrera wrote: "Mark Woodward" wrote, On Mon, 2007-01-29 at 21:57 +1300, John Little wrote: Hi IMHO simpler and more flexible (works if there are lines not matching the pattern) is: :let n=0 | g/opIndex(\zs\d\+/s//\=n/|let n+=1 A useful idiom I learned here. Usually I muck around with ordinary searches until the search highlighting shows I've got it right, then :let n=0|g//s//\=n/|let n+=1 is what I have to remember, and it's obvious what the start and increment are. John Just as additional info this method also lets you perform this sort of incremental substitution on letters. UniqueID2 = lview.focusedItem.subItems.opIndex(0).text; Parent = lview.focusedItem.subItems.opIndex(0).text; Children= lview.focusedItem.subItems.opIndex(0).text; login = lview.focusedItem.subItems.opIndex(1).text; let i=97|g/opIndex(\zs\d\+/s//\=nr2char(i)/|let i=i+1 UniqueID2 = lview.focusedItem.subItems.opIndex(a).text; Parent = lview.focusedItem.subItems.opIndex(b).text; Children= lview.focusedItem.subItems.opIndex(c).text; login = lview.focusedItem.subItems.opIndex(d).text; let i=65|g/opIndex(\zs\d\+/s//\=nr2char(i)/|let i=i+1 UniqueID2 = lview.focusedItem.subItems.opIndex(A).text; Parent = lview.focusedItem.subItems.opIndex(B).text; Children= lview.focusedItem.subItems.opIndex(C).text; login = lview.focusedItem.subItems.opIndex(D).text; :h nr2char Since you brought it up, I also have this other situation, bla.blah.blahh[0].text; bla.blah.blahh[0].text; bla.blah.blahh[0].text; bla.blah.blahh[0].text; bla.blah.blahh[0].text; bla.blah.blahh[0].text; I tried using the original solution you gave me, :let n=0 | g/opIndex(\zs\d\+/s//\=n/|let n+=1 but it kept going to the same spot as before. I will not change the [#]. Where is the match for (? I can't figure it out. The next version of vim should have an easier way of doing this. I constantly have to copy from vim and paste to TextPad and doing it there and then copy from TextPad back to vim. Yes, I know I could make a function and assign a key to it, but these kind of things should be just built in. I know, I am a picky guy, aren't I? :-) thanks.
Re: replace with a number sequence
On Wed, 31 Jan 2007 15:23:36 +, "Tom Whittock" <[EMAIL PROTECTED]> wrote: > the ex (colon) commands are one of the major parts of vim - I would > highly recommend learning them a bit more, if you want to get the most > out of the program. For me, without ex there would be very little > point in using vim at all - I couldn't even write to a file ;) Unless you use ZZ. -- Matthew Winn
Re: replace with a number sequence
"Tim Chase" wrote, Using a simple change to Tim Chase' original substitution response, you have: [snip] if you don't want to type it in every time, :vmap :s/\d\+/\=line('.')-line("'<") which adds ctrl-i as a key in visual mode to do the job. Ok, one last question on this. :-) Is there a way to start this from where the cursor is and end with the first non-matched instance? For example, char[] str = null; bla.blah.blahh[0].text; bla.blah.blahh[0].text; bla.blah.blahh[0].text; bla.blah.blahh[0].text; bla.blah.blahh[0].text; bla.blah.blahh[0].text; int i = 0; bla.blah.blahh[0].text; bla.blah.blahh[0].text; bla.blah.blahh[0].text; bla.blah.blahh[0].text; bla.blah.blahh[0].text; bla.blah.blahh[0].text; So, if the cursor is on the first bla.* the first 6 lines will be numbered sequentially and stop there. Is this possible? Well, if you have the above mapping, you can then map :nmap vip which will visualize the block in question and then reduce the problem to the previously solved problem. If you plan to then expand this to auto-numbering blocks throughout your your document, you might try something like (broken into multiple lines for easy reading, but would be all one mappable line) :g/bla\.blah\.blahh\[\d/if getline(line('.')-1)=~'blah\.blah\.blahh\[' | let counter = 0 | endif | s/blahh\[\zs\d\+/\=counter/ | let counter = counter + 1 I haven't tested it (so there's likely some escaping I got wrong), but it should do similar to my previous mapping, only when the previous line doesn't match the target pattern, it resets the counter to zero. It might be reducable to something like :g/.../if getline(line('.')-1) =~ @/ which might be a little easier to read/understand, as it uses the search register. Just a few ideas. Thanks. vim rocks! f4 What a beautiful world is this we live on! thanks, josé
Re: replace with a number sequence
Using a simple change to Tim Chase' original substitution response, you have: [snip] if you don't want to type it in every time, :vmap :s/\d\+/\=line('.')-line("'<") which adds ctrl-i as a key in visual mode to do the job. Ok, one last question on this. :-) Is there a way to start this from where the cursor is and end with the first non-matched instance? For example, char[] str = null; bla.blah.blahh[0].text; bla.blah.blahh[0].text; bla.blah.blahh[0].text; bla.blah.blahh[0].text; bla.blah.blahh[0].text; bla.blah.blahh[0].text; int i = 0; bla.blah.blahh[0].text; bla.blah.blahh[0].text; bla.blah.blahh[0].text; bla.blah.blahh[0].text; bla.blah.blahh[0].text; bla.blah.blahh[0].text; So, if the cursor is on the first bla.* the first 6 lines will be numbered sequentially and stop there. Is this possible? Well, if you have the above mapping, you can then map :nmap vip which will visualize the block in question and then reduce the problem to the previously solved problem. If you plan to then expand this to auto-numbering blocks throughout your your document, you might try something like (broken into multiple lines for easy reading, but would be all one mappable line) :g/bla\.blah\.blahh\[\d/if getline(line('.')-1)=~'blah\.blah\.blahh\[' | let counter = 0 | endif | s/blahh\[\zs\d\+/\=counter/ | let counter = counter + 1 I haven't tested it (so there's likely some escaping I got wrong), but it should do similar to my previous mapping, only when the previous line doesn't match the target pattern, it resets the counter to zero. It might be reducable to something like :g/.../if getline(line('.')-1) =~ @/ which might be a little easier to read/understand, as it uses the search register. Just a few ideas. -tim
Re: replace with a number sequence
"Tom Whittock" wrote, but it kept going to the same spot as before. I will not change the [#]. Where is the match for (? I can't figure it out. Using a simple change to Tim Chase' original substitution response, you have: :'<,'>s/\d\+/\=line('.')-line("'<") which replaces the *first number* on every line in the visual selection. look at the difference between the two to see that the responses have all used opIndex as a marker to know what number is to be operated on. this modification doesn't really allow for any fine degree of control (like selecting which number on a given line to increment), but it works for the examples you've given. if you don't want to type it in every time, :vmap :s/\d\+/\=line('.')-line("'<") which adds ctrl-i as a key in visual mode to do the job. the ex (colon) commands are one of the major parts of vim - I would highly recommend learning them a bit more, if you want to get the most out of the program. For me, without ex there would be very little point in using vim at all - I couldn't even write to a file ;) Ok, one last question on this. :-) Is there a way to start this from where the cursor is and end with the first non-matched instance? For example, char[] str = null; bla.blah.blahh[0].text; bla.blah.blahh[0].text; bla.blah.blahh[0].text; bla.blah.blahh[0].text; bla.blah.blahh[0].text; bla.blah.blahh[0].text; int i = 0; bla.blah.blahh[0].text; bla.blah.blahh[0].text; bla.blah.blahh[0].text; bla.blah.blahh[0].text; bla.blah.blahh[0].text; bla.blah.blahh[0].text; So, if the cursor is on the first bla.* the first 6 lines will be numbered sequentially and stop there. Is this possible?
Re: replace with a number sequence
but it kept going to the same spot as before. I will not change the [#]. Where is the match for (? I can't figure it out. Using a simple change to Tim Chase' original substitution response, you have: :'<,'>s/\d\+/\=line('.')-line("'<") which replaces the *first number* on every line in the visual selection. look at the difference between the two to see that the responses have all used opIndex as a marker to know what number is to be operated on. this modification doesn't really allow for any fine degree of control (like selecting which number on a given line to increment), but it works for the examples you've given. if you don't want to type it in every time, :vmap :s/\d\+/\=line('.')-line("'<") which adds ctrl-i as a key in visual mode to do the job. the ex (colon) commands are one of the major parts of vim - I would highly recommend learning them a bit more, if you want to get the most out of the program. For me, without ex there would be very little point in using vim at all - I couldn't even write to a file ;) Cheers.
Re: replace with a number sequence
"Mark Woodward" wrote, On Mon, 2007-01-29 at 21:57 +1300, John Little wrote: Hi IMHO simpler and more flexible (works if there are lines not matching the pattern) is: :let n=0 | g/opIndex(\zs\d\+/s//\=n/|let n+=1 A useful idiom I learned here. Usually I muck around with ordinary searches until the search highlighting shows I've got it right, then :let n=0|g//s//\=n/|let n+=1 is what I have to remember, and it's obvious what the start and increment are. John Just as additional info this method also lets you perform this sort of incremental substitution on letters. UniqueID2 = lview.focusedItem.subItems.opIndex(0).text; Parent = lview.focusedItem.subItems.opIndex(0).text; Children= lview.focusedItem.subItems.opIndex(0).text; login = lview.focusedItem.subItems.opIndex(1).text; let i=97|g/opIndex(\zs\d\+/s//\=nr2char(i)/|let i=i+1 UniqueID2 = lview.focusedItem.subItems.opIndex(a).text; Parent = lview.focusedItem.subItems.opIndex(b).text; Children= lview.focusedItem.subItems.opIndex(c).text; login = lview.focusedItem.subItems.opIndex(d).text; let i=65|g/opIndex(\zs\d\+/s//\=nr2char(i)/|let i=i+1 UniqueID2 = lview.focusedItem.subItems.opIndex(A).text; Parent = lview.focusedItem.subItems.opIndex(B).text; Children= lview.focusedItem.subItems.opIndex(C).text; login = lview.focusedItem.subItems.opIndex(D).text; :h nr2char Since you brought it up, I also have this other situation, bla.blah.blahh[0].text; bla.blah.blahh[0].text; bla.blah.blahh[0].text; bla.blah.blahh[0].text; bla.blah.blahh[0].text; bla.blah.blahh[0].text; I tried using the original solution you gave me, :let n=0 | g/opIndex(\zs\d\+/s//\=n/|let n+=1 but it kept going to the same spot as before. I will not change the [#]. Where is the match for (? I can't figure it out. The next version of vim should have an easier way of doing this. I constantly have to copy from vim and paste to TextPad and doing it there and then copy from TextPad back to vim. Yes, I know I could make a function and assign a key to it, but these kind of things should be just built in. I know, I am a picky guy, aren't I? :-) thanks.
Re: replace with a number sequence
On Mon, 2007-01-29 at 21:57 +1300, John Little wrote: > Hi > > IMHO simpler and more flexible (works if there are lines not matching > the pattern) is: > > :let n=0 | g/opIndex(\zs\d\+/s//\=n/|let n+=1 > > A useful idiom I learned here. Usually I muck around with ordinary > searches until the search highlighting shows I've got it right, then > > :let n=0|g//s//\=n/|let n+=1 > > is what I have to remember, and it's obvious what the start and increment are. > > John Just as additional info this method also lets you perform this sort of incremental substitution on letters. UniqueID2 = lview.focusedItem.subItems.opIndex(0).text; Parent = lview.focusedItem.subItems.opIndex(0).text; Children= lview.focusedItem.subItems.opIndex(0).text; login = lview.focusedItem.subItems.opIndex(1).text; let i=97|g/opIndex(\zs\d\+/s//\=nr2char(i)/|let i=i+1 UniqueID2 = lview.focusedItem.subItems.opIndex(a).text; Parent = lview.focusedItem.subItems.opIndex(b).text; Children= lview.focusedItem.subItems.opIndex(c).text; login = lview.focusedItem.subItems.opIndex(d).text; let i=65|g/opIndex(\zs\d\+/s//\=nr2char(i)/|let i=i+1 UniqueID2 = lview.focusedItem.subItems.opIndex(A).text; Parent = lview.focusedItem.subItems.opIndex(B).text; Children= lview.focusedItem.subItems.opIndex(C).text; login = lview.focusedItem.subItems.opIndex(D).text; :h nr2char cheers, -- Mark
Re: replace with a number sequence
"Charles E Campbell Jr" wrote, jose isaias cabrera wrote: and I would like to replace the number between the parens () to a sequential number starting from 0. ie, Hello! The visincr plugin addresses just this issue with ease: * place cursor on the 0 at the top * use ctrl-v (visual block) and move to highlight the column * press: :I Visincr can handle various forms of enumeration, too. You can get visincr from: http://mysite.verizon.net/astronaut/vim/index.html#VISINCR or from http://vim.sourceforge.net/scripts/script.php?script_id=670 Quick overview: :I[#] left justified incremented list :II [# [zfill]] right justified incremented list :IO [#] left justified octal incremented list :IIO [# [zfill]] right justified octal incremented list :IX [#] left justified hex. incremented lsit :IIX [# [zfill]] right justified hex. incremented lsit :IYMD [# [zfill]] year/month/day incremented list :IMDY [# [zfill]] month/day/year incremented list :IDMY [# [zfill]] day/month/year incremented list :IA [#] alphameric incremented list :ID [#] dayname incremented list :IM [#] monthname incremented list if this works, this is easier than the previous formula. I'll give it a try. gracias. josé
Re: replace with a number sequence
Hi IMHO simpler and more flexible (works if there are lines not matching the pattern) is: :let n=0 | g/opIndex(\zs\d\+/s//\=n/|let n+=1 A useful idiom I learned here. Usually I muck around with ordinary searches until the search highlighting shows I've got it right, then :let n=0|g//s//\=n/|let n+=1 is what I have to remember, and it's obvious what the start and increment are. John
Re: replace with a number sequence
>>> I have these lines, >>> UniqueID2 = lview.focusedItem.subItems.opIndex(0).text; >>> Parent = lview.focusedItem.subItems.opIndex(0).text; >>> Children= lview.focusedItem.subItems.opIndex(0).text; >>> >>> and I would like to replace the number between the parens () to a >>> sequential >>> number starting from 0. ie, >> >> If you highlight the lines in question and issue the following: >> >> :'<,'>s/opIndex(\zs\d\+\ze)/\=line('.')-line("'<") >> >> It should renumber them starting at zero for you. >> >> I'd be glad to explain what that's doing if you have trouble >> un-opaquifying it :) > > I've used the line function in the past. And it's something seems to > recur frequently. It, and its mate "getline()" are commonly used in scripts, usually taking a line reference (such as "." for the current line or "'x" for a mark or "$" for the last line of the file) :help line( :help getline( > Since I'm always learning new things in vim, I'd appreciate any > instructive comment on your solution. > The use of zs and ze is also interesting (-: I'll try to address them both in the same pass. The \zs and \ze mark the beginning and end of the pattern in which I'm interested. This allows me to add context (the "opIndex(" and the closing paren) without worrying about extracting those pieces and putting them back in in the final expression. Thus, I'm only dealing with the numbers, but I know that those numbers are the ones in the opIndex() that I'm interested in. :help /\zs :help /\ze I now have as my "entire" pattern, the digits that I want to replace. I replace them with the results of evaluating an expression: \= :help sub-replace-special The math in question replaces the contents that were found (the digits) with the following result: line('.')-line("'<") The line('.') is the line number of the current match (or the beginning of the current match, if it had been multi-line). The second line("'<") is the line number of a mark. In this case, the "<" mark, which is the beginning of the range (as in, your "'<,'>" that shows up at the beginning of the line when you try to execute an Ex command within visual mode). Thus, the result of this on the first line (N) of the replacement will be N-N=0. The second line on which we replace this, it will have be (N+1)-N = 1 and so on until you get to the end of the range. If one wanted to count from 1, you could just use 1+line(".")-line("'<") Or you could count by multiples of 2 (0,2,4,6,8...): 2*(line(".")-line("'<")) or odd multiples of two (1,3,5,7,9...) 1+2*(line(".")-line("'<")) and so on. Things get a little trickier if one wants to do the replacement in a single line of multiple instances... opInstance(1), opInstance(2), opInstance(3) One has to write a function that effects a global variable: let g:start=0 function! Incrementer() let l:result = g:start let g:start = g:start + 1 return l:result endfunction and then do something like :%s/regexp/\=Incrementer()/g It would be nice not to have to use global variables for something like that. Just a few further riffs on the original idea...Hope this helps understand and dissect the original so you can use/abuse it in the future. :) -tim
Re: replace with a number sequence
Hi. Tim Chase wrote: I have these lines, UniqueID2 = lview.focusedItem.subItems.opIndex(0).text; Parent = lview.focusedItem.subItems.opIndex(0).text; Children= lview.focusedItem.subItems.opIndex(0).text; login = lview.focusedItem.subItems.opIndex(1).text; txtCust.text= lview.focusedItem.subItems.opIndex(2).text; txtProj.text= lview.focusedItem.subItems.opIndex(3).text; txtbDate.text = lview.focusedItem.subItems.opIndex(4).text; txtdDate.text = lview.focusedItem.subItems.opIndex(5).text; txteDate.text = lview.focusedItem.subItems.opIndex(6).text; txtPM.text = lview.focusedItem.subItems.opIndex(7).text; txtLang.text= lview.focusedItem.subItems.opIndex(8).text; txtVendor.text = lview.focusedItem.subItems.opIndex(9).text; txtInvoice.text = lview.focusedItem.subItems.opIndex(10).text; txtPMFund.text = lview.focusedItem.subItems.opIndex(11).text; txtProjFund.text= lview.focusedItem.subItems.opIndex(12).text; txtA_No.text= lview.focusedItem.subItems.opIndex(13).text; txtNotes.text = lview.focusedItem.subItems.opIndex(14).text; txtStatus.text = lview.focusedItem.subItems.opIndex(15).text; and I would like to replace the number between the parens () to a sequential number starting from 0. ie, If you highlight the lines in question and issue the following: :'<,'>s/opIndex(\zs\d\+\ze)/\=line('.')-line("'<") It should renumber them starting at zero for you. I'd be glad to explain what that's doing if you have trouble un-opaquifying it :) I've used the line function in the past. And it's something seems to recur frequently. Since I'm always learning new things in vim, I'd appreciate any instructive comment on your solution. The use of zs and ze is also interesting (-: Kind regards. Luke. -tim -- Luke Vanderfluit Analyst / Web Programmer e3Learning.com.au 08 8221 6422
Re: replace with a number sequence
> I have these lines, > UniqueID2 = lview.focusedItem.subItems.opIndex(0).text; > Parent = lview.focusedItem.subItems.opIndex(0).text; > Children= lview.focusedItem.subItems.opIndex(0).text; > login = lview.focusedItem.subItems.opIndex(1).text; > txtCust.text= lview.focusedItem.subItems.opIndex(2).text; > txtProj.text= lview.focusedItem.subItems.opIndex(3).text; > txtbDate.text = lview.focusedItem.subItems.opIndex(4).text; > txtdDate.text = lview.focusedItem.subItems.opIndex(5).text; > txteDate.text = lview.focusedItem.subItems.opIndex(6).text; > txtPM.text = lview.focusedItem.subItems.opIndex(7).text; > txtLang.text= lview.focusedItem.subItems.opIndex(8).text; > txtVendor.text = lview.focusedItem.subItems.opIndex(9).text; > txtInvoice.text = lview.focusedItem.subItems.opIndex(10).text; > txtPMFund.text = lview.focusedItem.subItems.opIndex(11).text; > txtProjFund.text= lview.focusedItem.subItems.opIndex(12).text; > txtA_No.text= lview.focusedItem.subItems.opIndex(13).text; > txtNotes.text = lview.focusedItem.subItems.opIndex(14).text; > txtStatus.text = lview.focusedItem.subItems.opIndex(15).text; > > and I would like to replace the number between the parens () to a sequential > number starting from 0. ie, If you highlight the lines in question and issue the following: :'<,'>s/opIndex(\zs\d\+\ze)/\=line('.')-line("'<") It should renumber them starting at zero for you. I'd be glad to explain what that's doing if you have trouble un-opaquifying it :) -tim
replace with a number sequence
Greetings! I have these lines, UniqueID2 = lview.focusedItem.subItems.opIndex(0).text; Parent = lview.focusedItem.subItems.opIndex(0).text; Children= lview.focusedItem.subItems.opIndex(0).text; login = lview.focusedItem.subItems.opIndex(1).text; txtCust.text= lview.focusedItem.subItems.opIndex(2).text; txtProj.text= lview.focusedItem.subItems.opIndex(3).text; txtbDate.text = lview.focusedItem.subItems.opIndex(4).text; txtdDate.text = lview.focusedItem.subItems.opIndex(5).text; txteDate.text = lview.focusedItem.subItems.opIndex(6).text; txtPM.text = lview.focusedItem.subItems.opIndex(7).text; txtLang.text= lview.focusedItem.subItems.opIndex(8).text; txtVendor.text = lview.focusedItem.subItems.opIndex(9).text; txtInvoice.text = lview.focusedItem.subItems.opIndex(10).text; txtPMFund.text = lview.focusedItem.subItems.opIndex(11).text; txtProjFund.text= lview.focusedItem.subItems.opIndex(12).text; txtA_No.text= lview.focusedItem.subItems.opIndex(13).text; txtNotes.text = lview.focusedItem.subItems.opIndex(14).text; txtStatus.text = lview.focusedItem.subItems.opIndex(15).text; and I would like to replace the number between the parens () to a sequential number starting from 0. ie, (0) (1) ... ... (19) etc. Any help would be greatly appreciated. josé