0

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.

HarshaD
  • 243
  • 2
  • 9

2 Answers2

2

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.

Doktor OSwaldo
  • 5,732
  • 20
  • 41
  • 1
    It would be more generic to extract the thing to transform then use `substitute(...., 'g')` through `:s\=` -- which is the core of my `ConvertNames` command. – Luc Hermitte Jul 26 '19 at 14:03
  • @Doktor Oswaldo - can you please explain `:%s/\v(\L\l+)(\L\l+)(\d+)(\L\l+)/\U\1_\U\2_\3_\U\4/g` – HarshaD Jul 26 '19 at 14:11
  • @LucHermitte Absolutly! But wanted to point out the three most common ways to solve questions like this: 1. Repeat the command on every line with `g`. 2, Use a `substitute` 3. Repeat the command don every found pattern with a recursive macro. Of course there are more advanced generic solutions. But I often find myself needing a quick and diry one time solution for this kind of problems. – Doktor OSwaldo Jul 26 '19 at 14:58
  • 1
    @HarshaD It is a search and replace which works by splitting the term up in 3 groups of words (starting with an letter `\L` followed by some lowercase letters `\l`) and a group of numbers (`\d`). Then replace the found groups with the uppercase version of the group (`\U` transforms the capturing group to uppercase) and concat them with a underscore. – Doktor OSwaldo Jul 26 '19 at 15:00
  • @Doktor OSwaldo : Thanks for the explanation. This does work but it replaces the string match in comments too. But I can live with that... – HarshaD Jul 26 '19 at 15:15
  • You could improve that by add a test for that before in the regex. But I guess you are not yet familiar with regexes? If not have a look on it. Regexes are extremely useful and powerful and usable in every programming language and most editors! – Doktor OSwaldo Jul 26 '19 at 15:24
1

(Not directly your question, yet with lh-style you could use :%ConvertNames/^\k\+/SCREAMING_SNAKE_CASE)

Otherwise, may be

:g/^MacA/normal cru

should work as expected as what you wish to change is at the beginning of the line. It would be a little bit more complex otherwise.

Luc Hermitte
  • 31,979
  • 7
  • 69
  • 83
  • `:g/^MacA/normal cru` says pattern not found: ^MacA.... would take a look at lh-style! Thanks – HarshaD Jul 26 '19 at 14:15
  • If `^MacA` is not found, it means your lines are not starting with `MacA`. It may be a little more complex then as you'll need to move the cursor to the word you wish to change. – Luc Hermitte Jul 26 '19 at 14:41