Anuj
Anuj

Reputation: 1200

Efficient way to remove spaces between two characters in Vim

I am facing this problem a lot. For eg.

a b c d abcd

x y z w xyzw

Now i have to remove the spaces between only the single characters. The lines have some characters in front as well.

i want to try something like this: s/[a-z] [a-z]/[a-z] [a-z]/g - what could be possible replace expression?

This is just an example of the problem. I face similar issues a lot of time when i have to apply some search to find these type of expression and replace to an expression but not replace them fully and only replace a part of it.

Edit: Want to remove single space between single characters.

Upvotes: 4

Views: 4274

Answers (2)

Rickard
Rickard

Reputation: 11

Thanks. I had a similar problem. I wanted to remove all white spaces from all numbers in a row but keep white spaces elsewhere.

%s/\\([0-9]\\) \\([0-9]\\)/\1\2/g

did the trick.

Upvotes: 1

gbulmer
gbulmer

Reputation: 4290

Use a regular expression with collections of substrings:

s/\([a-z]\) \([a-z]\)/\1\2/g

the \1 refers to the characters matched by the regular expression between the first \(...\), and \2 the characters matched by the regular expression between the second \(...\)

Upvotes: 8

Related Questions