David
David

Reputation: 3046

Changing nth column from the end of the line, on multiple lines

I want to change in vim the following text:

test aatest
aaaa test a2test
xxxxx test 4 xctesc

to

test a5test
aaaa test a5test
xxxxx test 4 x5tesc

I want to change the 5th to last character of each line into 5

Is there a command that can change the nth column relative to the last column on multiple lines?

Upvotes: 0

Views: 53

Answers (1)

Ingo Karkat
Ingo Karkat

Reputation: 172590

Solution A

Already mentioned in the comments: For each line, go to the end, move 4 left, replace.

:%normal! $4hr5

Solution B

With :substitute: match the 5 last characters, capturing the first and last four separately, then modify in the replacement.

:%substitute/\(.\)\(....\)$/5\2/

Upvotes: 1

Related Questions