Hot answers tagged

43

You can use append-next-kill (bound to C-M-w by default) to accumulate the text you want to insert above paragraph 1: Kill paragraph 2 as you normally would. Mark paragraph 4 and press C-M-w before killing it. Repeat this step for all paragraphs you'd like to move before paragraph 1. When you're done, you can yank the accumulated text before paragraph 1.


21

(defun some-function (beginning end) (interactive "r") (if (use-region-p) (message "The region is active, and is from %d to %d" beginning end) (message "The region is still there (from % d to %d), but it is inactive" beginning end))) The use of (interactive "r") means that the parameters beginning and end will automatically ...


18

There are two cases I can think of: reactivating the region, and adjusting the size of the region. I most often use this binding to reactivate the region after performing some command that deactivates it, or doing something that sets mark and moves point without activating the region. For example, do a C-s and search forward for something. Hit RET to ...


17

Ask Emacs: C-h r i exchange-point-and-mark RET or, better, C-h r i C-x C-x RET. This takes you to the information shown below. This is what the Emacs manual, node Setting Mark says in answer to your question: C-x C-x is useful when you are satisfied with the position of point but want to move the other end of the region (where the mark is). Using C-...


14

In Spacemacs once you are in iedit state you can: press F to limit the scope to the current function press L to limit the scope to the current line press J to increase the scope (starting from the current line) one line below press K to increase the scope one line above navigate between the occurrences with n and N and press TAB to remove an occurrence.


13

A text terminal transmits only characters (more precisely, bytes), not keys. Keys and keychords (keys with modifiers) are encoded as character sequences. Keys that insert printable characters are sent as themselves; function keys are sent as escape sequences. Most escape sequences consist of the character Escape (?\e in Emacs syntax) followed by two or more ...


12

There is a separate answer for each of the points you listed: The emacs default binding C-x h will select the whole buffer regardless of where the cursor is. The expand-region package will help achieve the second goal of gradually expanding the selection. expand-region will also eventually select the whole buffer as you keep on expanding the region. Have ...


10

Use expand-region.el: Expand region increases the selected region by semantic units. Just keep pressing the key until it selects what you want. An example: (setq alphabet-start "abc def") With the cursor at the c, it starts by marking the entire word abc, then expand to the contents of the quotes abc def, then to the entire quote "abc def", then ...


10

I recently came across similar problem, I basically wanted to fontify code snippets in the documentation which I got some other source. I followed the approach mentioned towards the end of your answer and it worked fine for me. The function I ended up with some thing like the following (defun my-fontify-yaml (text) (with-temp-buffer (erase-buffer) ...


10

That's because when you insert your first chunk of code after start, end remains constant and now point in the middle of something (since more characters have been added before it). A quick solution for your use case is to insert text at the end before: ;; Insert text around a region. In this case, it's ;; the LaTeX code environment from the listings ...


10

M-= (command count-words-region). (The region does not even need to be activated, but activating it lets you see it.)


9

You can use registers combined with the mark ring. Push the current marks with C-<SPC> C-<SPC>, recover them later with C-u C-<SPC>, or save them in a register, say b, via C-x r <SPC> b, and jump back later with C-x r j b. Now mark another region. C-u C-x r s a will copy the marked region to register a and delete the text. Then ...


9

One way to do it: (defun my-search-forward (str) (interactive "smy-search-forward: ") (when (re-search-forward str nil t) (setf (point) (match-beginning 0) (mark) (match-end 0))))


9

The best solution is using C-x C-x to reactivate the mark. But if you want to really keep the selection you can use: (defun copy-keep-highlight (beg end) (interactive "r") (prog1 (kill-ring-save beg end) (setq deactivate-mark nil)))


8

If you install smartparens, then you can use the function sp-splice-sexp, which does exactly what you are asking for. It works on all valid types of braces and quotes, according to the current major mode. See the docs for more details. Edit: The principal advantage of smartparens over paredit (see rekado's answer) is that smartparens has better support ...


8

Save-excursion restores point AND mark (as of emacs 24.4), so it is no help for you here. You could save the point manually with this idiom: (let ((pos (point))) ... (goto-char pos)) However, in your case it's better to just set the mark at the end of the line: (defun mark-from-point-to-end-of-line () "Marks everything from point to end of line" (...


8

Just add this line in your init file: (delete-selection-mode 1)


8

If the region grows then the cursor changes position. This is because the cursor is always at one end of the region. If you select all of the buffer text (C-x h and then use C-x C-x to swap point and mark, so the cursor is at the end of the text), and then you type some more text, then you have accomplished what you want: the region has been extended to ...


8

I use this when I've lost a selection due to some operation that removed it, and I want to restore selection. One such scenario would be: Select rectangle. Kill it. Undo killing rectangle. C-x C-x to restore selection to rectangle. This happens when I want to make an ad hoc backup of a selected area and to experiment on the copy, such as, for example, I ...


8

The other answers focus on the usefulness of C-x C-x when transient-mark-mode is active. But C-x C-x predates transient-mark-mode, and is useful independently of it. The main role of C-x C-x is to swap point and mark. This useful when you are editing two points in a single buffer and for some reason don't want to split the current window. Do some editing,...


8

My crystal ball tells me you're using Emacs-24.5, which indeed had this limitation. This has been fixed in Emacs-25 where rectangle-mark-mode has been improved so you can move the cursor past the "end" of a line (and where you can use C-x C-x to go to another corner of the rectangle as well).


8

The region is defined as the span of text between point and mark, therefore it's sufficient to adjust both to the numbers in question. The following uses point-min and point-max for that purpose: (set-mark (point-min)) (goto-char (point-max)) However you mention "set the selection" afterwards, so perhaps you don't only want to change the region (as there ...


7

Sorry I didn't see your question sooner. This is precisely one of the reasons for library Zones (zones.el). When you narrow the buffer, each narrowing is added to a ring of narrowings, which includes the full (unnarrowed) buffer. The narrowings can be nested, but they need not be. They can be any regions at all. You can cycle among the narrowings (using ...


7

There's paredit which has a function paredit-splice-sexp, which might do what you want. In this example | indicates point: (hello world "no |way") ; run paredit-splice-sexp (hello world no way) Here's another example: (let ((a b)) (fo|rmat #t "meh")) ; run paredit-splice-sexp (let ((a b)) format #t "meh") It does not work on the top level, however,...


7

This seems to do what you want : (defun org-hide-subtrees-in-region (beg end) (interactive "r") (save-restriction (narrow-to-region beg end) (outline-hide-sublevels 1))) EDIT: I first gave a wrong solution, which I keep here as it is also interesting IMO (but doesn't answer the question) The function outline-hide-region-body seems to be ...


7

If you just need this behavior for mouse selections, you can use the built-in setting mouse-drag-copy-region: (setq mouse-drag-copy-region t) If non-nil, copy to kill-ring upon mouse adjustments of the region.


7

The selection can be copied automatically if the OS supports primary selection by setting the below variable. (setq x-select-enable-primary t) From C-h v x-select-enable-primary, you get, Non-nil means cutting and pasting uses the primary selection The existence of a primary selection depends on the underlying GUI you use. E.g. it doesn't exist ...


7

Ok, I found the answer almost immediately after posting question. The function name is deactivate-mark if anyone has the same question. Its described here. Updated code: (defun sandric/swiper-or-region (beg end) "Swiper region or 'empty string' if none highlighted." (interactive (if (use-region-p) (list (region-beginning) (region-end))...


7

Here's one approach: (defun isearch-exit-mark-match () "Exit isearch and mark the current match." (interactive) (isearch-exit) (push-mark isearch-other-end) (activate-mark)) (define-key isearch-mode-map (kbd "<C-return>") #'isearch-exit-mark-match) This binds a different key (C-return) to exit the current isearch and also leave the last ...


6

Here's one: (require 'cl-lib) (defun sum-numbers-in-region (start end) (interactive "r") (message "%s" (cl-reduce #'+ (split-string (buffer-substring start end)) :key #'string-to-number))) Because it uses string-to-number, it will treat the the ...


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