vimneovim

Multicursor like editing in Vim


I have the following code block.

first_red_check = MAX(first_red_check),     
first_check = MAX(first_check),     
first_red_check_old = MAX(first_red_check_old),
first_check_old = MAX(first_check_old),
min_first_check = MAX (min_first_check),
min_first_red_check = MAX (min_first_red_check),
days_until_first_red = MAX (days_until_first_red)

Is there a shortcut in neovim or vi, to place the part before the = at the end of the string? I need it to be in this form:

MAX(first_red_check) AS first_red_check
MAX(first_check) AS first_check

I thought about something like multiline Cursor in VSCode.


Solution

  • Is there a shortcut in neovim or vi

    No, but there's a command that does line-wise transformations. You should familiarize yourself with it by reading :help :substitute.

    The following command will do what you ask for:

    :%s/\(\w*\)\s*=\s*\(.*)\).*/\2 AS \1/
    

    Essentially, it uses two groups in \(\) to capture the expression before the equals sign and the one after it. You can ignore the \s*, they will match any number of whitespace and serve just to make the expression more robust. The first group capture any number of word characters \w while the second group captures .*) - anything until a closing parenthesis - which will account for the non-word characters and the inconsistent whitespace on the right-hand side. The last .* matches the commas and the trailing whitespace to overwrite them as well.

    The substitution is a simple \2 AS \1, i.e. the second capture group, a literal " AS " and the first capature group.

    All of the regular expressions used are documented in :help pattern.txt.


    I thought about something like multiline Cursor in VSCode.

    Well, different tools are used differently. Vim does not natively have a multi-cursor feature although there are plugins which will add it. You may want to take a look at the vim-visual-multi plugin. Disclaimer: I never tried it myself, because ...

    I've not yet seen a use case where Vim's powerful line-wise commands or (blockwise) Visual mode couldn't do what multi-cursors can do. I do admit that multi-cursors look nice and are easy enough to use, just click and edit and it applies to every cursor. It's just Vim chose a different path some decades ago and has been doing great so far.