pbn
pbn

Reputation: 2706

Vim - how to replace with match without last char?

I would like to fix every line in code which has following pattern:

int main() {

with

int main()
{

Same applies for if statements and loops. Simply said - fixing brackets. I have matched them with:

:%s/.*\(.*\).*{/&^?^M{/gc

But I get the following output:

int main() {
{

How do I replace my pattern with it's match (&), but without the last character or specifically without the "{" ?

Upvotes: 2

Views: 56

Answers (1)

Kent
Kent

Reputation: 195039

%s/.*(.\{-})[^{]*\zs{/\r&/

this command works for your example, add flag g or gc if you need them. however you may want to check if the indentation also correct if you apply it on you real source file.

I think there should be special tool for the code style fixing.. you should check it. IMO, Vim/shell script would be the last option for those source codes batch editing.

Upvotes: 3

Related Questions