I often mistype a space-delimited string and when I do that, I just want to type the correct space-delimited string, without deleting the incorrect space-delimited string. After I have typed the correct space-delimited string, I do "C-c SPC" and I then want the incorrect space-delimited string to be removed.
(defun remove-penultimate-spadel () "Remove the penultimate space-delimited string from the current line." (interactive) (let ((line (thing-at-point 'line t)) (case-fold-search nil)) (if (string-match "\\(\\w+\\)\\s-+\\(\\w+\\)\\s-+\\(\\w+\\)\\s-*$" line) (let* ((start (match-beginning 2)) (end (match-end 2)) (new-line (concat (substring line 0 start) (match-string 3 line)))) (goto-char (line-beginning-position)) (delete-char (length line)) (insert new-line)) (message "No penultimate spadel found.")))) (global-set-key (kbd "C-c SPC") 'remove-penultimate-spadel) I have this test
foo bar hukarx hukarz bat If point is after "hukarz" and I eval the function, it correcly replaces "hukarx" with "hukarz", but it also moves "bat" up on the same line.
How can I prevent "bat" from moving up to the same line as "hukarz"?;)
EDIT: I also have to add that point should not move after this operation is done. It should remain after "hukarz".
EDIT: Be aware that this is a space delimited string and not a word.
lineincludes the\n, so you are getting rid of that when you delete the line.edebugand step through it to see what is happening.