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.

Atemu
  • 302
  • 1
  • 11
  • A solution to this problem would be to just delete everything up to the beginning of the next word but I have not found a way to do that either. – Atemu Oct 20 '21 at 11:25

1 Answers1

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.

gigiair
  • 2,166
  • 1
  • 9
  • 15
  • That's even more cumbersome than the C-SPC M-f M-b C-w I mentioned in my question. – Atemu Jul 19 '21 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 '21 at 20:56
  • Maybe the function I have add will enjoy you better. – gigiair Jul 21 '21 at 08:38