Search code examples
regexvimviex

Copying pattern from set of lines with Vim's Ex mode


I am tidying up my vimrc.

Mission:
- Copy content inside single-quotes (and preferably after the slash)
- Paste them below line 1

1: Name:
2: 
3: Bundle 'tpope/vim-rails'
4: Bundle 'tpope/vim-endwise'
5: Bundle 'mileszs/ack.vim'
6: Bundle 'scrooloose/nerdcommenter'
7: Bundle 'kana/vim-textobj-entire'
8: Bundle 'kana/vim-textobj-user'

I know about the

:{source_address}t{target_address}

Ex command, and I know it accepts patterns. However, I am still not able to get it just right. I am also fairly new to regexes so bear with me. I tried something along the lines of (on a visual selection

:'<,'>/\/.*\'t1

Where I attempt to match a pattern that:

  • Starts with a /
  • May contain stuff in the middle .*
  • Ends with a '

Obviously I am off by some degree. Any ideas?


Solution

  • :t copy the whole line, not the matchstr

    to achieve what you want, you need matchstr() function.

    this line will do the similar thing, but in reverse order. Hope it is ok for you:

    :'<,'>g/\/.*'/call append(1,matchstr(getline('.'),@/))
    

    for example, if I just select the first two "Bundle" lines, and execute the cmd, I get:

    1: Name:
    /vim-endwise'
    /vim-rails'
    2: 
    3: Bundle 'tpope/vim-rails'
    4: Bundle 'tpope/vim-endwise'
    .... (other lines)
    

    you see that the order was in reverse.

    To keep the order you have options :

    • write script with list
    • select those "reversed" lines, and do '<,'>g/.*/m1
    • assign the matchstr to reg x, and 2put! x