1

I am coding a C project and as I am unexperienced, everytime I needed to access the element of a pointer to a struct I typed (*x).y (where x is a struct and y an item), which I just realized could be rewritten as -> which would look much nicer. So I first tried to make a regular expression to find all such occurences. I thought /(/*.+/)/. would work, but it gave some strange unwanted occurences. For example (queue_t *queue) would match which I don't understand why.

I'm also unsure what macro could be used if I got the correct regex. I thought making then a macro which deletes the first 2 characters, and then would go to the "end" to replace ). with ->, but then if I have something of the type (*(*a).b)).c I don't know how to find the correct ).

Also are there better alternatives to do what I want other than use regular expressions and macros?

4

Iwould try it like this:

%s/(\*\(\k*\))\./\1->/gc

The search string is:

  • ( opening parenthesis
  • \* a literal *
  • \(...\) a match group. This can later be addressed with \1
    • \k* any number of keywor characters
  • ) closing parenthesis
  • \. a literal .

The replace is:

  • \1 This is replaced with what is matched by the test within \(...\)
  • ->

See: :help /\k, :help /\( and :help /\1.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.