How to increment a previously used substitution digit

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

How to increment a previously used substitution digit

parekh

Hi,

I have something like this -
reg a0 = abc[0]
reg a0 = abc[0]
reg a0 = abc[0]
reg a0 = abc[0]

I am trying to create something like this -
reg a0 = abc[0]
reg a1 = abc[1]
reg a2 = abc[2]
reg a3 = abc[3]

Any recommendations on how I can do this at a command line?

TIA
Sanjay

--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: How to increment a previously used substitution digit

Agathoklis D. Hatzimanikas

On Thu, Mar 12, at 12:42 parekh wrote:

>
> Hi,
>
> I have something like this -
> reg a0 = abc[0]
> reg a0 = abc[0]
> reg a0 = abc[0]
> reg a0 = abc[0]
>
> I am trying to create something like this -
> reg a0 = abc[0]
> reg a1 = abc[1]
> reg a2 = abc[2]
> reg a3 = abc[3]
>
> Any recommendations on how I can do this at a command line?

Here is a possible way:

Source this function,

function! Increm() range
    let nr = 1
    let line = a:firstline + 1
    while line <= a:lastline
        let string = getline(line)
        call setline(line, substitute(string, '[[:digit:]]\+', nr, 'g'))
        let nr += 1
        let line += 1
    endwhile
endfunction

:1,4 call Increm()

or while selecting the lines

:'<,'>call Increm()


> TIA
> Sanjay

Regards,
Ag.

--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: How to increment a previously used substitution digit

John Little-4
In reply to this post by parekh


On Mar 13, 8:42 am, parekh  wrote:

> Any recommendations on how I can do this at a command line?

Do you mean a shell prompt, or vim's command line?

If the lines you want to change are contiguous, the visincr script
(from vim.org) works well.

Another approach would be:

    :let n = 0
    :g/reg a0/s/a\zs0/\=n/|s/\[0\]/\='['.n.']'/|let n+=1

I suppose you could use

    vim -c "let n = 0|g/reg a0/s/a\zs0/\=n/|s/\[0\]/\='['.n.']'/|let n
+=1" -c x <file>

to do it all from a shell prompt.

Regards, John


--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: How to increment a previously used substitution digit

StarWing
In reply to this post by parekh

On 3月13日, 上午3时42分, parekh <[hidden email]> wrote:

> Hi,
>
> I have something like this -
> reg a0 = abc[0]
> reg a0 = abc[0]
> reg a0 = abc[0]
> reg a0 = abc[0]
>
> I am trying to create something like this -
> reg a0 = abc[0]
> reg a1 = abc[1]
> reg a2 = abc[2]
> reg a3 = abc[3]
>
> Any recommendations on how I can do this at a command line?

put your cursor on the first line,
input: qqyyp7l<C-A>jddkq
then, put cursor to second line, press 3@q
now every thing is done :-)
--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: How to increment a previously used substitution digit

StarWing



On 3月13日, 下午1时26分, StarWing <[hidden email]> wrote:

> On 3月13日, 上午3时42分, parekh <[hidden email]> wrote:
>
> > Hi,
>
> > I have something like this -
> > reg a0 = abc[0]
> > reg a0 = abc[0]
> > reg a0 = abc[0]
> > reg a0 = abc[0]
>
> > I am trying to create something like this -
> > reg a0 = abc[0]
> > reg a1 = abc[1]
> > reg a2 = abc[2]
> > reg a3 = abc[3]
>
> > Any recommendations on how I can do this at a command line?
>
> put your cursor on the first line,
> input: qqyyp7l<C-A>jddkq
> then, put cursor to second line, press 3@q
> now every thing is done :-)

sorry, it's qqyyp/\d<CR><C-A>n<C-A>jddkq
--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: How to increment a previously used substitution digit

Charles Campbell
In reply to this post by parekh

parekh wrote:

> Hi,
>
> I have something like this -
> reg a0 = abc[0]
> reg a0 = abc[0]
> reg a0 = abc[0]
> reg a0 = abc[0]
>
> I am trying to create something like this -
> reg a0 = abc[0]
> reg a1 = abc[1]
> reg a2 = abc[2]
> reg a3 = abc[3]
>
> Any recommendations on how I can do this at a command line?
>  
You could use visincr.vim:

    http://mysite.verizon.net/astronaut/vim/index.html#VISINCR   
(cutting edge)
    http://vim.sourceforge.net/scripts/script.php?script_id=670  (stable)

With it, just use visual-block (put cursor on 0, ctrl-v, move down,
:I).  Visincr
can do a lot more incrementing stuff: octal, hex, dates, alphameric, etc.

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

Regards,
Chip Campbell


--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---