emacs regexp, adding a space between parenthesis in (i)

Would like to add a space between any character and an opening or closing parenthesis. But don’t add one if there is already one!

a) shows the openning and b) for the closing.

If you have more than one, like (()), you’ll need to run both (opening and closing) replacements twice, and another for three ‘(((‘!

Alt-x repl[TAB]reg[TAB][ENTER] or (C-M-%) to get:
M-x replace-regexp

a) Replace regexp in region: \((\)\([^[:space:]]\)
with: \1 \2

b) Replace regexp in region: \([^[:space:]]\)\()\)
with: \1 \2

Adding a ‘ // ‘ after every ‘}’ to do: if ( 1 ) { help(); } // if

Query replace regexp: \(}\)\([^[:space:]]\)
Query replace regexp \(}\)\([^[:space:]]\) with: \1 // \2

=== 2017 update ===

In Windows emacs, at least, needs [ESC] then [Ctrl]+s for something like C-M-s or isearch-forward-regexp which is useful for searching for two spaces following each other.

C-s will find all the single spaces too with C-s [SPC] [SPC]


~ by nambabwe on 2013/03/08.

%d bloggers like this: