Search code examples
vim

What is the VIM way to copy a list of variables (a list of words vertically aligned with different widths)?


let(:aaaaa)   { 123 }
let(:bb)      { true }
let(:ccc_ccc) { "foo bar" }

I want to copy all variable names (:aaaaa, :bb, :ccc_ccc).
In VsCode, for example, I would use a multi-line selection.
How can I do it in VIM?

Block selection didn't work, once the variable names have a different length.


Solution

  • You could use the command :%norm f:"Qyt) to make your 'q register' contain the following:

    :aaaaa:bb:ccc_ccc
    

    The way it works is as follows:

    • :%norm means 'to all lines, apply the following normal commands'
    • f: moves the cursor to the first colon on the line
    • "Qy appends yanked text to the 'q' register
    • t) is a motion 'till the next closing parenthesis

    This assumes that the 'q' register is already empty (you can use qqq to clear it). If you only want to do this for a subset of lines, you'd replace the % with a range (or visual selection).

    What you do with the register's contents after that is up to you.

    "qp will put them into the buffer, and maybe you'd then do :s/:/\r:/g to split the lines at the colons like this:

    :aaaaa
    :bb
    :ccc_ccc