Emacs Flaw: Select Word Command
Emacs has a command to select the current word, mark-word
, with default shortcut of
【M-@】.
Selecting the current word is a frequently needed operation.
However, emacs's mark-word
command is inconvenient.
It does not select the whole word.
It only select from the cursor position to the end of the word.
For example, if your word is “transmission”, and your cursor is at the “m”, then it will select just “mission”.
To select the whole word, you need to move the cursor to the beginning of the word first.
Also, mark-word
has a feature that if you repeat the command, then it extend the selection to the next word to the right.
But again, you need first to move the cursor to the beginning of the word.
Emacs has in fact a whole system of selecting text:

Here is suggestion of a scheme that may replace or supplement the above system.
- We create a command that select the whole word.
- When repeated, it should select the next larger syntactic unit.
- In human languages, that would be sentence, then paragraph, then whole buffer.
- In computer languages, the sequence would be: current identifier or token, current expression, current construct (or line), current block or defun.
- If the lang is lisp, this simply means extending the selection to the next outer parens.
- For a illustrated example of this, see: A Text Editor Feature: Extend Selection by Semantic Unit
Here's the code that implements the above idea for lisp (or any simply nested syntax):
- Emacs: Select Line 📜
- Emacs: Select Text Between Quotes 📜
- Emacs: Select Text Block 📜
- Emacs: Extend Selection 📜
You should give these commands keys. 〔see Emacs Keys: Define Key〕.
I suggest:
- Alt+6
- Alt+7
- Alt+8
- Alt+9
these commands are in Emacs: Xah Fly Keys 📦
- In the above, 【Alt+8】 is assigned to the command, because selecting whole word is a commonly needed operation, and 【Alt+8】 is one key less than 【Alt+@】, and the finger used, the 3rd finger pressing on 8, is also easier than the 4th finger on 2, and 8 is right hand.
- Pressing 【Alt+8】 will select the current whole word.
- Press it again will extend the selection to the next outer parens.
- The above code effectively does extend selection to higher level of semantic unit for lisp or simply nested syntax.
- It does not work in more complicated nesting, such as HTML/XML.
- For the code to work in other langs like Java, Perl, Python, XML, it'll need some more work.
- Another frequently needed operation is to select text inside straight 'single' or "double" quotes.
- This is especially frequently needed for string datatype in popular languages such as C, C++, Java, JavaScript, Perl, Python, PHP, HTML/XML.
- Ideally, the “extend-selection” above should do it, by simply invoking the command twice, or just once when cursor is on the quote, but since the above code does not work outside of lisp syntax, so here's a additional command as a workaround to do the frequently needed operation of selecting text inside quote pairs.
- With both of the above defined, pressing 【Alt+8】 will select whole word (or extend current selection).
- Pressing 【Alt+*】 will select the text inside matching quotes.
This “extend-selection” scheme with “transient-mark-mode” on, should simplify and replace the functionality of {
• mark-word
• mark-sexp
• mark-paragraph
• mark-defun
}.
With just one command to remember, and more efficient to operate. The “mark-whole-buffer” can be replaced by a shortcut 【Ctrl+a】 to be compatible with modern UI standard, and “mark-page” is today rather obsolete because it depends on page marker char “^L” (ASCII 12), which is not used in most languages but only in older emacs lisp source code.