Given the following sentence:
This is one two three.
When I move the cursor to the space between one and two, I would like to replace two with XXX. I know there is a way in vi so that I can mark two as the modified string so that when I finish entering the new string and the new string replaces the two in-place.
// Update-1 //
The corresponding command in vi is 'cw'
I would probably just use M-d SPC X X X (if the SPC is necessary depends on wether the cursor is placed directly after "one" or before "two") or M-f M-DEL X X X, but maybe that's not what you're looking for.
Ok, you didn't answer my comment, and didn't vote up an answer, so I guessed what you might like and here is a little hack:
(defun change-word (n)
(interactive "p")
(lexical-let ((old-window-configuration (current-window-configuration)))
(clone-indirect-buffer "*edit-word*" t)
(narrow-to-region (point) (save-excursion
(forward-word n)
(point)))
(overwrite-mode 1)
(local-set-key (kbd "C-c C-c")
(lambda ()
(interactive)
(widen)
(let ((end-of-edit (point)))
(kill-buffer)
(set-window-configuration old-window-configuration)
(goto-char end-of-edit))))))
Invoke M-x change-word (or bind it to a key you like), edit the word, and type C-c C-c when you're done. If you want to edit the next n
words, give it a prefix argument (e.g. M-3 M-x change-word to change the next three words). It's not exactly the same -- You'll edit in another buffer -- but it comes close. Try and see if you like it. It is not a very elaborate solution. Maybe the best, and most emacs-style approach, would be to have something akin to isearch-mode, that highlights the changed region and so on. Note that you'll have to (require 'cl)
because of lexical-let
.
Another possibility would be something like this:
(defun change-word ()
(interactive)
(dotimes (i (- (save-excursion (end-of-thing 'word)) (point)))
(insert (read-char))
(delete-char 1)))
But this example is just a very crude hack -- You won't even be able to navigate while editing your word. Maybe one could write something modifying the command-loop behavior, but I didn't look into that.
I don't know about any built-in functionality that does exactly what you seem to want, but of course there are Viper and other Vi emulation modes built in.
With the cursor before the letter t
in "two," press M-@ or C-M-Space to select the word "two," and then type XXX to replace it. In my Emacs, M-@ is bound to mark-word
, and C-M-Space is bound to mark-sexp
. I find the latter easier to type, but the former is more likely to work as intended across various modes.
The nice thing about these commands is that repeated executions (via the aforementioned key sequences) extend the selection incrementally. In your case, the first press of M-@ highlights "two":
This is one |two three.
Pressing M-@ again highlights "three" as well:
This is one |two three.
On your keyboard, the meta key (denoted by "M" in these examples) may be bound to the Alt key.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With