Hot answers tagged

13

You can use the function thing-at-point and tell it to return the word at point and not return any text properties (unless you need them). Example buffer contents (| is the cursor): Hello wor|ld Calling (thing-at-point 'word 'no-properties) returns "world".


10

I usually use C-M-SPC or, with easy-kill installed, M-w w.


8

You don't need to define a separate function (command) for this. And even if you did define one, it need not use query-replace-regexp. The standard command query-replace does just what you request, if you provide it with a prefix argument. So just use C-u M-% to query-replace words. C-h f query-replace tells you this (note the part in bold): query-...


6

For occasional use, you can do with query-replace-regexp (C-M-%). Replace .\{32\} with \&^J. This means, replace all chunks of 32 characters, with the same thing followed by a newline. To type the newline into the replacemnt, use C-q C-j. If you want to apply this only to binary strings, use [01]\{32\} for matching. This will protect other text from ...


6

You can query-replace-regexp as follows: \(\b\w+\b\)\W+\1\b → \1 This means, match a whole word (\b\w+\b), followed by non word characters (\W+), followed by the first word (\1) and a word ending (\b). Replace with the first word only. Case sensitivity is handled by options case-fold-search and case-replace.


6

You can use the expand-region package to do this and more. Let | be point and (...) indicate that ... is marked. foo-|bar Calling er/expand-region once: foo-(|bar) Calling it again: (|foo-bar)


5

Take a look at subword-mode (see manual), a built-in minor mode that makes standard movement and editing commands treat capital letters in camelCaseWords as word boundaries. For example, you could enable subword-mode and then use the standard mark-word command (M-@) to mark the fragments of "trulyAwesomeFunction" one at a time. With snake_case, the ...


5

C-u -1 M-@ M-w or define a new command yourself, something like (defun backward-copy-word () (interactive) (save-excursion (copy-region-as-kill (point) (progn (backward-word) (point)))))


5

Since the answer is picked from the comment from @kaushalmodi, I cannot choose the comment as an answer, so I post this as the right answer. According to comment from @kaushalmodim, syntax-subword is great, it is exactly what I need. You can install it using package.el. Here is my configuration in init.el: (global-syntax-subword-mode) (setq syntax-subword-...


4

Here are two possibilities: Use subword-mode. It is designed to do what you request. Emacs’s word commands recognize upper case letters in StudlyCapsIdentifiers as word boundaries. When Subword mode is enabled, the minor mode indicator , appears in the mode line. Change the syntax class of uppercase letters to something other than word-constituent....


4

Run query-replace-regexp with '\bi\b' as the query and 'I' as the replacement. (Update to comment) The function @lawlist linked to should work nicely if you want to replace all instances without questions. To still be prompted for each instance, below should do (defun query-replace-word (word new-word) (interactive "sWord: \nsNew word: ") (query-...


4

M-@ is mark-word, but it does not grab the part of the word that is before point. You can write a command (or create a keyboard macro) that does M-<left> (which is command left-word) followed by M-@, to pick up also the first part of the word. For example: (defun mark-whole-word (&optional arg allow-extend) "Like `mark-word', but selects ...


3

You used tag isearch so presumably you want to search interactively, not using Lisp. And you used tag words; you apparently want to search for a whole word. In that case, just use M-s w, which does isearch-forward-word at the top-level, and which does isearch-toggle-word from within Isearch. See the Emacs manual, node Word Search. (I found that ...


3

Place the cursor on either side of the word, then hold the shift key down and hold the alt/option key down, and then use the left or right arrow. If the cursor is not on either side of the word, then hold the alt/option key down and use the left or right arrow key to move to either side of the word before selecting it as set forth in the preceding paragraph....


3

Emacs 24 defines forward-whitespace, which moves by whitespace-delimited words (i.e. anything but whitespace is considered a word constituent), but oddly not backward-whitespace. It only treats space, tab and newline as whitespace, not other Unicode whitespace or characters defined as whitespace. When going forward, it moves to the beginning of the next word,...


3

C-u - 1 M-x mark-word will give -1 as an argument to mark-word, thus marking the previous word. After that, M-w will copy it to the kill ring without actually deleting it, making it available to paste elsewhere.


3

Emacs uses syntax tables to decide what is and is not a word. Each major mode will have its own syntax table. Commands that move over or delete words will use the values in this table to determine how far to go. The key syntax class in this case is the 'word constituent'. Deleting a word in Emacs works by deleting whole groups of adjacent word constituents. ...


3

Look before you leap! You can write a custom function which will look for non word characters followed by a new line before next word. On such condition go to end of line other wise run normal word forward. (defun my-forward-word () (interactive) (if (looking-at "\\W+\n") (when (search-forward-regexp "\n" nil 'noerror) (goto-char (match-...


3

The following command (minimally-tested) will collect all of the words in the buffer and display the unique ones (ignoring case) in a new buffer. (defun show-unique-words (&optional alphabetical) "Collect all of the unique words in the current buffer and display them in a new buffer. With prefix, alphabetize the list." (interactive "P") (let ((...


3

You can either toggle word-search-mode by doing M-s w after starting isearch, or you can use start in word search mode directly using isearch-forward-word, also bound to M-s w.


3

There's really nothing to replicate, I think. You somehow have to tell Isearch that you're done typing text for the search string - you have to hit some key to tell it that. So at a minimum you have C-s acc <SOME KEY>. You can end Isearch with any key that is not already bound to something in isearch-mode-map. (And even then, you can rebind any keys ...


3

As Drew pointed out in his answer you can also end the search with another key such as M-b or M-f, which for words boundaries works good, but my solution is kind of more general: define a key to put the cursor at the beginning (or end) of the pattern I'm searching: (use-package isearch :bind (:map isearch-mode-map ("C-<return>" . ...


2

Not quite sure what you're asking. If you're using re-search-backward because you want to include the beginning of the word that encompasses the point, perhaps this does what you want: (setq regexp ".*[ab].*") (defun test-it () (interactive) (save-excursion (save-restriction (narrow-to-region (progn (backward-word 1) (point)) ...


2

A solution for users of evil: In normal state, just type: viw


2

I don't know the function replace-in-string, but assuming it uses emacs regex (or using replace-regexp-in-string) you should be able to use word boundaries. You could match foo by using \bfoo\b (or in a string "\\bfoo\\b"). For example, evaluating (replace-regexp-in-string "\\bfoo\\b" "bar" "afoo foob foo moo") would result in the string "afoo foob bar moo"....


2

Here's a quick attempt at a custom function that may do what you want: (defun backward-kill-char-or-word () "Delete the character or word before point." (interactive) (if (looking-back "\\w" 1) (backward-kill-word 1) (backward-delete-char 1))) This looks back one character to decide what to do. If it sees a word constituent character it ...


2

word-search-mode is the usual way to constrain your searches to words rather than letters, as @rpluim shows. An alternative, more general approach is to use regular expression searches, isearch-forward-regexp, or C-M-s, and also the reverse isearch-backward-regexp and C-M-r. You can then use a regular expression to limit your search: \bin will match in when ...


2

Dired generally uses the ls program. On some platforms it use Lisp code instead: library ls-lisp.el. ls typically does not have any switch that produces a column showing word counts. So no, Dired is not easily configured to add new, ad hoc columns. In principle you could tweak or enhance ls-lisp.el, if you use that. But even that wouldn't be ...


2

Luckily this has been studied by Malabarba and published in his blog, Endless. I have the following in my config: (use-package isearch :no-require t :demand t ;; Go to the start of current isearch match. (adapted from Endless) :hook (isearch-mode-end . (lambda () (when (and isearch-forward ...


2

Not quite sure what you're looking for. This searches for a word surrounded by whitespace. User input for read-string is tested to be sure it is a word. (defun foo (word) (interactive (let ((strg (read-string "Word: "))) (while (not (string-match-p "\\`\\w+\\'" strg)) ; Not a word. Prompt again. (setq strg (read-string "Word: "))) (...


Only top voted, non community-wiki answers of a minimum length are eligible