25 votes

How to add a suffix to every line of a text?

Very easily, fortunately. Use C-M-% (which runs the command query-replace-regexp), use the regexp (regular expression) $ (this matches exactly at the end of line) and replace it with your text. And ...
YoungFrog's user avatar
  • 3,526
20 votes

How do I delete all blank lines in a buffer?

From MasteringEmacs.com By Mickey Petersen: This is a frequent question so I figured I’d mention the solution here: You want to remove all empty (blank) lines from a buffer. How do you do it? Well, ...
manandearth's user avatar
  • 2,118
17 votes
Accepted

unicode.txt slowness

Reading the GitHub discussion thread linked in @wvxvw’s comment, I discovered the variable inhibit-compacting-font-caches. Setting it to non-nil solves the issue: (setq inhibit-compacting-font-caches ...
feklee's user avatar
  • 1,079
15 votes

Hightlighting all occurances of selected text

Functionality built-in to emacs: With point on the word in question, type M-s h . to highlight all occurrences. Note that this matches the entire word, not substrings. Repeat this multiple times, ...
InHarmsWay's user avatar
  • 1,319
12 votes

Drag selected text with the mouse

tldr; Add (setq mouse-drag-and-drop-region t) in you init file. Restart or C-c d R to reload if you're using Doom-Emacs. I just find out (SURPRISINGLY) that Emacs as text drag and drop. And this ...
Asme Just's user avatar
  • 384
11 votes

What is the difference between refill-mode and auto-fill-mode?

If you are continuously typing text without making mistakes or editing, then the end result would be the same in both cases. auto-fill-mode isn't so helpful when editing existing text, though (...
phils's user avatar
  • 51.1k
11 votes

Is there a convenient way to swap two words that aren't adjacent?

Function transpose-words can do that. Set a mark at one word (C-SPC). Move the cursor to the other word. C-0 M-t Enjoy!
Marco Wahl's user avatar
  • 2,876
8 votes
Accepted

Can I write elisp code to act on the text in a buffer

Yes you can! (If you mean frame what Emacs think it is.) To demonstrate, do the following steps: Run Emacs This will put you into the *scratch* buffer. Create a new frame M-x make-frame-command ...
Yasushi Shoji's user avatar
8 votes
Accepted

What kinds of mistakes does transpose fix?

While transpose-chars is useful for typos in any kind of buffer, I suspect transpose-words is not particularly useful when writing prose but might be useful in programming or dealing with other kinds ...
glucas's user avatar
  • 20.6k
8 votes
Accepted

org - keyboard shortcut to promote text to heading

You may be looking for the org-toggle-heading function, bound by default to C-c *. Convert headings to normal text, or items or text to headings.
Michał Politowski's user avatar
7 votes
Accepted

How to delete all instances of a character in a bunch of files?

Unless you want an interactive confirmation, which you presumably don't since you're automatic the replacement of all occurrences, call replace-string or replace-regexp instead of query-replace-string ...
Gilles 'SO- stop being evil''s user avatar
7 votes

Opposite of fill-paragraph

There is the unfill package for this now. Provides commands for explicitly unfilling (ie. unwrapping) paragraphs and regions, and also a command that will toggle between filling and unfilling the ...
smonff's user avatar
  • 1,589
6 votes
Accepted

Incrementing characters in Emacs, next to numbers?

Characters are numbers (non-negative integers under some limit), in Emacs. If you want to increment a single character, including a digit character, then just increment it as a character: (defun ...
Drew's user avatar
  • 77.5k
6 votes

How remove all rows contain specific text?

You can use the command delete-matching-lines via M-x delete-matching-lines RET hello RET. Note that tab-expansion helps a lot with typing command names at the minibuffer. You just need to type M-x ...
Tobias's user avatar
  • 33.2k
6 votes

How to swap strings in all lines?

I personally think the query-replace-regexp solution is better, but just for fun here is another solution with macros: ;; Move cursor to start of first line <f3> ;; kmacro-start-macro-or-...
0x5453's user avatar
  • 329
6 votes

How to blockquote text in one go

You can use string-rectangle (C-x r t). Set a mark (C-SPC) before the first character of the first line, move the cursor until the first character of the last line, use C-x r t, then type > and ...
adl's user avatar
  • 646
6 votes
Accepted

Filter text of buffer in a grep-like way

I want the text not matching the pattern to be gone M-x keep-lines will delete lines not matching the pattern (i.e. only keep the lines which match). flush-lines does the opposite, deleting lines ...
phils's user avatar
  • 51.1k
6 votes
Accepted

How can I insert the backslash character ( \ ) before quotation marks (" ") in Emacs?

This is a feature of paredit. If you look at the minibuffer (the line at the bottom of your Emacs window) when you press \, you'll see you're being prompted for "Character to escape:". The ...
Tyler's user avatar
  • 22.3k
6 votes
Accepted

How to transpose a pair of words?

Some observations first. When asking a question like that you must refer the mode, because the concept of a word depends on the major mode (lookup syntax tables in Elisp documentation). In this case, ...
nichijou's user avatar
  • 1,188
5 votes
Accepted

Emacs auto fill on period

The elisp manual section about auto-filling describes the auto-fill-chars variable. -- Variable: auto-fill-chars A char table of characters which invoke ‘auto-fill-function’ when self-inserted—...
JeanPierre's user avatar
  • 7,465
5 votes
Accepted

Transform selected text like a Python macro

This method will take the selected lines, wrap them in single quotes, and put them inside a list. (defun lines-to-list (point mark) (interactive "r") (let ((text (buffer-substring point mark))) ...
zck's user avatar
  • 9,092
5 votes
Accepted

Prevent Emacs from overwriting text

Hit the <insert> key to toggle inserting vs overwriting. C-h k <insert> tells you: <insert> runs the command overwrite-mode, which is an interactive compiled Lisp function in ...
Drew's user avatar
  • 77.5k
5 votes

Printing/exporting to PDF from text-mode

Printing I'm not sure what you've tried, but the best start would probably be ps-print-buffer-with-faces. This answer provides a convenient wrapper for generating a pdf file (copied below): You ...
aplaice's user avatar
  • 2,176
5 votes

How to swap strings in all lines?

You can do it with C-M-% (running query-replace-regexp): Give it the string "\([A-Z]+\)-\([A-Z]+\)" (quotes included) for the text to replace, and "\2-\1" for the replacement.
Harald Hanche-Olsen's user avatar
5 votes

What is the difference between refill-mode and auto-fill-mode?

refill-mode does more than auto-fill-mode, and does it automatically. This may be what you prefer but there are situations where refill-mode is not convenient. E.g., when you edit text shared with ...
phs's user avatar
  • 1,227
5 votes
Accepted

How remove lines which are not equal in two buffers?

In the following I assume that the order of the lines in the result buffer is irrelevant. For medium size buffers you can use cl-intersection: (defun txt-intersection (buffer-a buffer-b) "Only ...
Tobias's user avatar
  • 33.2k
5 votes

How to transpose words backwards?

Use a prefix argument, as the documentation indicates: (transpose-words ARG) Interchange words around point, leaving point at end of them. With prefix arg ARG, effect is to take word before ...
Steve Vinoski's user avatar
5 votes
Accepted

How to replace the buffer without slowing down the undo system?

Emacs-26 introduced replace-buffer-contents specifically to do that job. But use it with care: it works well when there are few differences, but if the new content happens to be completely different ...
Stefan's user avatar
  • 26.4k
5 votes
Accepted

how to do simple calculations on numbers in text

You can do that interactively by query-replace-regexp with embedded Elisp in the replacement string. Go to the beginning of the file. Type M-x query-replace-regexp RET. Input {x: \([0-9]+\), y:\([0-9]...
Tobias's user avatar
  • 33.2k
5 votes
Accepted

How to swap words `true` and `false` in buffer text?

Oh, I just wrote this a few days ago. It does nothing more than the package "parrot" recommended in another answer, though. Here's the code: (require 'dash) (defvar my-flip-symbol-alist '((...
JJPandari's user avatar
  • 261

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