Search code examples
vimvscodevim

Change camelCase to CONSTANT_CASE for the block of code in vim


I am using vim's abolish plugin to change camelCase variables to CONSTANT_CASE (UPPER_CASE in abolish lang) using cru

My Question:

I have an enum with 100 such variables, so naturally I want to know is there a way to use cru command over the block of code.

e.g.

MacAddr0High        = 0x000,  // address0 high Register

MacAddr0Low         = 0x004,  // address0 low Register

MacAddr1High        = 0x008,  // address1 high Register

MacAddr1Low         = 0x00C,  // address1 low Register

MacAddr2High        = 0x010,  // address2 high Register

.

.

I would like to change the variable name only and not the description

Currently all I can think of is to use cru and then repeat the command using . which is not the best approach.

I looked at some of the similar questions, which suggested going in the visual mode and using ~ u / U

But I can not use cru in visual mode. Maybe because its a plugin.

I can use VsCodeVim too, if it has a way of doing it.


Solution

  • As said in the comment, you could use the plugin and a global command and solve it with:

    :g/MacAddr/norm cru
    

    Without the plugin a search and replace could help:

    :%s/\v(\L\l+)(\L\l+)(\d+)(\L\l+)/\U\1_\U\2_\3_\U\4/g
    

    A recurisve macro would be a third thing to look for.