1

I am often in a situation where there are two words with a bunch of symbols in between like this:

word1|); }; } word2

Now I'd like to delete those symbols + whitespace between the words and maybe replace them with a single space, depending on what I want to do afterwards. This should work similar to M-\ and C-SPC but for non-word characters other than whitespace too:

word1| word2

M-d does delete those symbols but it also deletes the word behind them which I don't want:

word1|

C-SPC M-f M-b C-w technically works but is cumbersome.

Bonus: Sometimes I also only want to delete one set of symbols (separated by whitespace):

word1| }; } word2

In all of these cases I usually revert to using evil-mode with its definition of a word and delete operation but I'd like to have a pure emacs solution in my toolbelt too.

0
3

Select the region where you want this occur, and

M-x query-replace-regexp \W+ RET <space>

C-M % \W+ RET

Other solution :

Evaluate and save in your init file :

(defun removes-all-no-letter ()
  "Removes any non-alphabetic character around the point or following the current word.
 Insert a space char instead."
  (interactive)
  (while (looking-at-p "\\w") (forward-char))
  (while (looking-at-p "\\W") (backward-char))
  (forward-char) (while (looking-at-p "\\W") (delete-char 1))
  (insert " "))

(bind-key "C-c u" #'removes-all-no-letter)

I arbitrarely bind the function to C-c u, The choice is yours.

3
  • That's even more cumbersome than the C-SPC M-f M-b C-w I mentioned in my question.
    – Atemu
    Jul 19 at 19:05
  • 2
    Your solution only applies to one case, and requires the point to be at a particular location. Mine deals with all the intervals between two words of any region of any size. You can easily create a macro or a function and associate it with a key combination of your choice.
    – gigiair
    Jul 19 at 20:56
  • Maybe the function I have add will enjoy you better.
    – gigiair
    Jul 21 at 8:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.