Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Efficient way to remove spaces between two characters in Vim

Tags:

regex

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.

like image 452
Anuj Avatar asked Jan 18 '23 04:01

Anuj


1 Answers

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 \(...\)

like image 193
gbulmer Avatar answered Feb 08 '23 10:02

gbulmer