Is it possible to move pieces of text in Emacs, with keybindings?
For example in the following sentence: "this is a sentence wrong" I want to move the cursor to the 'w' of 'wrong' and then, with some keybindings, move the 'wrong' one word to the left.
I can't to use transpose-word: it's moving words to left or right, not swapping with something else.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Yes, it's absolutely possible in Emacs to move words or arbitrary text left/right using keybindings, though Emacs doesn't have a built-in "move word left/right" command as you describe by default. However, you can achieve exactly what you're describing—efficiently moving words—to reorder them with keybindings, using custom functions, packages, or Emacs macros.
✅ Your Goal (Example):
Given: "this is a sentence wrong"
Cursor is at "wrong".
You want to move the word "wrong" one position to the left to make: "this is a wrong sentence"
You're right that transpose-words just swaps adjacent words without giving fine control.
🔧 Option 1: Define Custom Functions
Emacs Lisp makes it easy to define commands like move-word-left and move-word-right.
📌 Add This to Your Emacs Config:
elisp
(defun move-word-left ()
"Move the word at point one word to the left."
(interactive)
(let ((word (thing-at-point 'word t)))
(when word
;; Kill the word
(kill-word 1)
;; Go back one word and insert
(backward-word 2)
(insert word)
(insert " "))))
(defun move-word-right ()
"Move the word at point one word to the right."
(interactive)
(let ((word (thing-at-point 'word t)))
(when word
(kill-word 1)
(forward-word 1)
(insert word)
(insert " "))))
M-<left> / M-<right> — to move words (customizable)
You can also modify it to work better with word movement.
✅ Summary
Goal
Solution
Move word left/right
Write custom function (above)
Transpose 2 words
transpose-words
Move anything (lines)
Use drag-stuff package
You're not stuck with transpose-word — with a tiny bit of Elisp, you can make Emacs behave exactly how you want.
Would you like a version of move-region-left/right as well?
User
Is it possible to move one character to the left (or right) instead of one word?
Assistant
Yes, absolutely! In Emacs, you can move a single character to the left or right—just like you might do when reordering or correcting typos—by writing a small custom function (just like with words), or using built-in commands like transpose-chars.
✅ Option 1: Built-in Solution — transpose-chars
Emacs already has a built-in command:
emacs-lisp
M-t ; often bound to `transpose-words`
C-t ; this is bound to `transpose-chars`
✔ How to Use transpose-chars:
Move your cursor after the character you want to swap with the one to the left.
For example, in "tihs", if the cursor is after 'h', hitting C-t makes it "this".
So it always swaps the character before point with the one at point.
❗ Limitation:
Only swaps characters — doesn’t “move” a character multiple times like in other editors.
If you want to move a character repeatedly, a custom function makes sense.
🔧 Option 2: Custom Functions to Move Character Left or Right
Here's how to move the character at point left or right repeatedly.
📌 Add to your Emacs config:
elisp
(defun move-char-left ()
"Move the character at point one position to the left."
(interactive)
(when (and (not (bolp)) (not (eolp)))
(let ((char (char-after))
(prev (char-before)))
(delete-char 1)
(backward-delete-char 1)
(insert char)
(insert prev)
(backward-char 1))))
(defun move-char-right ()
"Move the character at point one position to the right."
(interactive)
(when (and (not (eolp)) (not (eq (char-after) ?\n)))
(let ((char (char-after))
(next (char-after (1+ (point)))))
(delete-char 1)
(delete-char 1)
(insert next)
(insert char)
(backward-char 1))))