Yes, you can move text in Emacs using keybindings, and transpose-word is actually the command you want here. Let me clarify how to use it effectively for your example:
Step-by-Step Solution:
-
Cursor Position: Place the cursor anywhere within the word "wrong" (e.g., on the 'w').
-
Transpose Words: Use the command M-t (or C-t in some keybinding configurations) to swap "wrong" with the previous word ("sentence").
- Result: The sentence becomes:
this is a wrong sentence
This is exactly what you wanted: moving "wrong" one word to the left by swapping it with the preceding word.
Why This Works:
M-t (or transpose-word) swaps the current word with the previous word. When your cursor is inside "wrong," it treats the entire word as the target, swapping it with the prior word ("sentence"). This effectively moves "wrong" left by one word.
If You Prefer Manual Movement (Alternative Approach):
If you want more control or need to move words farther left, you can manually cut and paste:
- Mark the Word:
- Move to the start of "wrong" (
M-f to move forward a word, then C-SPC to set mark).
- Move to the end of "wrong" (
M-f again).
- Cut the Word:
C-w (kill ring).
- Move Left:
M-b (back one word to "sentence").
- Paste:
C-y (yank).
Custom Function for Advanced Use Cases:
If you frequently need to move words left without swapping, create a custom command in your Emacs config:
(defun move-word-backward ()
"Move current word backward by one position."
(interactive)
(transpose-subr -1 'word t t))
(global-set-key (kbd "C-M-t") 'move-word-backward)
This binds C-M-t to moving the current word backward (swap with previous word).
Keybindings Summary:
- Swap words left/right:
M-t (transpose-word).
- Cut/Paste:
C-w, C-y.
- Cursor movement:
M-f (forward word), M-b (backward word).
Transpose-word is the built-in solution here. If it didn’t work as expected earlier, double-check your cursor placement or keybindings. Let me know if you need further clarification!