Vim - Macro for verilog bus extension

I am unable to create a vim macro that does the following:

Source:

this_is_a_bus[10:0]
another_bus[2:0]

      

Required parameter:

this_is_a_bus[10]
this_is_a_bus[9]
this_is_a_bus[8]
this_is_a_bus[7]
this_is_a_bus[6]
this_is_a_bus[5]
this_is_a_bus[4]
this_is_a_bus[3]
this_is_a_bus[2]
this_is_a_bus[1]
this_is_a_bus[0]
another_bus[2]
another_bus[1]
another_bus[0]

      

What I was trying to do: I can search \d\+:\d\+

and place the cursor on the first number and then copy it using yw

to memory. However, I cannot say that vim can run the paste command according to the number in the register.

Am I approaching this the wrong way? What's the suggested way to create something like this?

+3


source to share


2 answers


Here is a function that could automatically expand with one keystroke. ( <F6>

in this case)

fun! ExpandIt()
    let pat = '^\([^][]\+\)\[\(\d\+\):\(\d\+\)\]\s*'
    let line = getline('.')
    let lnr = line('.')
    if line !~ pat
        return
    endif
    let exestr = substitute(line,pat,'range(\2,\3,-1)','g')
    let text = substitute(line,pat,'\1','g')
    exec 'let range='.exestr
    let result = []
    for i in range
        call add(result, text.'['.i.']')
    endfor
    call append(lnr, result)
    exec lnr'.d'    
endf
nnoremap <F6> :call ExpandIt()<cr>

      

  • post this file or put it in your vimrc.
  • the last line creates a mapping, it allows you to click <F6>

    on that line to expand
  • If the string is not in format xxxx[number:number]

    , nothing will happen
  • I have not tested the error like xxx[1:10]

    orxxx[000:000]



I did a little test and it looks like this:

enter image description here

+3


source


I would use a macro to accomplish this task. Start with the following line:

this_is_a_bus[10]

      

Then write the macro:

qqyyp<c-x>q

      

Then play the macro through @q

. Better yet, we can give him a bill 9@q

.



Explanation of the macro

  • q{reg}

    starts recording the macro and saves the macro in a register {reg}

    . egqq

  • yy

    copies the entire line
  • p

    paste the newly copied line below the current line (the cursor is on a new line)
  • <c-x>

    will decrease the first found number from the current cursor position
  • q

    pressing q

    while recording a macro will end recording
  • @{reg}

    will play the macro inside {reg}

    . for example @q

    to the play registerq

  • [count]@{reg}

    : @

    can take a number of macro playback [count]

    times. eg9@q

  • @@

    will play the last command used @{reg}

    . @@

    Can optionally accept a counter

For more help see:

:h q
:h @
:h @@
:h ctrl-x

      

+1


source







All Articles