Let's say you are about to type something that you know you want to kill-yank later. For example, say we need to repeat the following 10 times:
grid.addValue([some variable])
Currently, before I start typing, I can press C-<spc> C-<spc> to activate then deactivate the mark at the beginning of the text. Then, once I finish typing grid.addValue(), I can press C-x C-x twice to go back to the original mark, then back to the end of the text with the text highlighted.
My question: is there a way to set the mark and highlight as I type, so when I am done with the phrase I can immediately copy and yank it a bunch of times?
Just set the mark, type, and then copy/kill as normal.
The region (i.e. whatever is between point and mark) isn't highlighted, but it's still usable.
Related
In EMACS the default seems to be to 'copy' any text highlighted with the mouse. I'm attempting to replicate the functionality of modern text editors, where I can highlight a section of text and press 'paste' to replace it. I have so far added
(delete-selection-mode 1)
to my init.el
The problem is, if I copy something, then highlight to paste in its place, I end up pasting what I had just highlighted, changing nothing.
What do I need to change to fix that behaviour?
The most powerful element of emacs is its introspection features, lets have a look at how we can use them to try and solve this problem. We must use the power of the source.
One of the most important tools for introspection in emacs is the describe-key command which is bound to C-h k. It brings up the documentation of whatever keystroke is called after it. So in our case if we press C-h k and then click and drag we will see the documentation for <down-mouse-1> and more importantly for <drag-mouse-1>. The documentation states that "<drag-mouse-1> at that spot runs the command mouse-set-region". Below it then gives some documentation for this command. It says
Set the region to the text dragged over, and copy to kill ring.
This should be bound to a mouse drag event.
See the ‘mouse-drag-copy-region’ variable to control whether this
command alters the kill ring or not.
Now we know that somehow mouse-drag-copy-region controls whether or not the highlighted text is copied.
If we follow the link of that variable it tells us the default value and some documentation:
If non-nil, copy to kill-ring upon mouse adjustments of the region.
Now all we have to do is set the variable to be nil to get the effect that you want. Place the following code at the end of your init file and you should be all set
(setq mouse-drag-copy-region nil)
I hope that this helps you with this problem and that more importantly it helps you with further issues.
By default, selecting a region with the mouse does not copy the text to the kill ring. If your Emacs does this, you probably have set the variable mouse-drag-copy-region.
In a fresh Emacs (24.5 started using -Q), you can do the following:
Start delete-selection-mode.
Mark a region using the mouse. Copy it using M-w.
Mark a second region. Replace it with the first using C-y.
I see two alternatives, neither of which does exactly what you request. (For both, yes, turn on delete-selection-mode.)
Use the secondary selection for the text to copy, and use the primary selection (the region) for the text to be replaced.
You copy text into the secondary selection using the Meta key plus the mouse - for example, press and hold Meta (the Alt key, usually) while dragging or double-clicking mouse-1.
You paste the secondary selection using Meta plus mouse-2.
Select text with the mouse, then copy it to the kill-ring using M-w. Then select the text to replace with the mouse and use C-y to paste the copied text to replace it.
I do this a lot: [M-%] to query-replace, then hit [y] a few times, then I'm past the area where I'm changing things so I hit [q] to be done.
Then I'd like to get the cursor back to where I was, so here's where it gets awkward. I [C-_] to undo the last replace, then move the cursor on character, then undo again to redo the last replace. Now the cursor's up where I've been editing, but there has to be a better way.
(I'm happy to get back to either where I started the replace, or where the last replace happened.)
C-uC-SPC jumps to the mark (which was automatically set as Blrfl pointed out), and then pops the previous mark off the local mark ring (so you can repeat the process to move to previous marks).
I think this is preferable to exchange-point-and-mark, as the latter reactivates the mark, highlighting the region in transient-mark-mode, which you wouldn't want in this case. (Unless you wanted to jump back and forth between the point you started at and the location of the final replacement, in which case exchange-point-and-mark would be exactly what you wanted.)
After you quit, the mark will be where you started and the point will be where you stopped. C-x C-x will swap the two, putting the point where you started.
I am doing a search backwards in the text in emacs, then I move the point around and do a modification or copy, but then can I return to the point of the search before or do I still have to search for text around that spot?
Best,
Try
M-x pop-to-mark-command
And you can also get that functionality by C-u SPC or C-u C-# (i.e. using a prefix argument with the set-mark-command).
For more information on the mark ring, read about the mark ring. There is also a global mark ring, which is basically a list of marks across buffers, which you can navigate via C-x C-SPC.
I've tweaked some elisp, point-stack to allow for quick forward/backward through buffers. I've written more on my blog, but the basic idea is f5 to push a location, f6 to jump back, f7 for forward.
Here's and animated gif showing it.
Ctrl+R foo Enter Notice that “Mark saved where search started” message?
... edit ...
Ctrl+U Ctrl+Space Go back to the saved mark
If you used the mark during the “edit” part, press Ctrl+U Ctrl+Space again that many times. The successive marks are stored on a ring (for simple uses, what you see is a mark stack): each time the mark is set, it's pushed onto the stack, and Ctrl+U Ctrl+Space goes to the mark popped from the stack.
You can also use Ctrl+U 42 Ctrl+Space or Ctrl+4 Ctrl+2 Ctrl+Space to go back to the 42nd previous mark.
If you put (setq set-mark-command-repeat-pop t) in your .emacs, you can repeat just Ctrl+Space after Ctrl+U Ctrl+Space to go back to successive previous marks.
Before search use Ctrl-space to save your mark. Then do a seach and copy some text. Then, use Ctrl-u-Ctrl-space to pop back to your saved mark.
If I have a multiline sentence in Emacs it naturally overflows onto the the following lines. Now, if my cursor is at the beginning of such a sentence and I press the DOWN ARROW key, the cursor is placed at the beginning of the next sentence (which might be at 4-5 lines down), rather than on the next line itself (which other editors do). Same is the behavior of the END and HOME keys.
Is there a way in which I can change this behavior and get the cursor on the next line instead of the next sentence?
I haven't yet tried it myself, but I think what you are asking for is the default behavior for emacs 23. What version are you running?
You might want to check out the page Move By Visible Lines page on the emacswiki.
You might want to try auto-fill-mode or longlines-mode. To get either use M-X then type the command you want. Toggle them off the same way.
If that doesn't work you may want to examine the binding that is being applied to your down arrow. Type C-h k then hit the down arrow key.
It sounds as though the text is wrapping, so by definition (a line being a group of characters separated by a carriage return), it is moving down to the next line.
I agree it is a pain, however a lot of other editors also have this behaviour.
One way is to turn off wrapping:
M-x toggle-truncate-lines
You wont be able to see all of the text in the editor, however it will move down to the next line.
I want to select or highlight a block in Emacs without using the mouse, but doing it from the keyboard like Vim's visual mode. What is the easiest way to do this from a keyboard?
If I understand the question correctly, it is not about rectangular regions originally.
C-Spc puts a mark at the current position.
Wherever your cursor is afterwards, the text between the last mark and the current position is "selected" (you can highlight this by activating transient-mark-mode, but this will also mean that marks have to be deleted when you don't want highlight).
You can operate on that region with commands like:
C-w . . Kill region. This deletes and puts the region into the kill ring.
C-y . . Yank. This inserts the last snippet from the kill ring.
M-y . . Cycle kill ring. Immediately after C-y, this replaces the yanked part by the other snippets in the kill ring.
M-w . . Save region into kill ring. Like C-w, but doesn't delete.
This is just the basic usage. Marks have other uses, too. I recommend the tutorial (C-h t).
Take a look at region-rectangle in Emacs.
In short, you start selection like usual with Control-Space, then kill region with Control-x r k and paste (or yank) killed block with Control-x r y.
Emacs 24.4 now has rectangle-mark-mode. Use Ctrl + X, Space to invoke it.
Although C-SPC is a common way to start marking something from
wherever your point is, there are often quicker/easier ways that don't
involve explicitly moving to start/end points...
Built-in selection shortcuts
M-h — an important means to mark a paragraph. A "paragraph"
often means a block of code.
C-M-h and C-M-# — for marking sexps and defuns,
respectively. This works for several languages, not just lisps.
hold down shift — another slick way to highlight during
movement. E.g., M-S-f selects forward a whole word. This is
shift-select-mode,
and it is enabled by default in Emacs 24+. On some (non-chiclet)
keyboards, you should be able to hold down C-S- with a single
pinky.
You can press any of these repeatedly to grow the selection.
There are also a few special ways to mark things:
C-x h — mark the whole buffer
C-x SPC — enter rectangle mark mode
(NOTE: use C-g often to cancel marking while
experimenting.)
Add-ons
There are a few add-on packages that improve selecting regions and
things. These are all play nicely together and fit different use
cases. Use them all!
expand-region:
Expand region increases the selected region by semantic units. Just
keep pressing the key until it selects what you want. C-= is a
recommended binding for it. Hit it a few times to get what you
need.
easy-kill: Use M-w and
a mnemonic to select different types of things, like words, sexps,
lists, etc.
zop-to-char:
Like zap-to-char, but provides nice selection and other
menu-driven actions.
diff-hl: Highlight uncommitted changed regions. Use diff-hl-mark-hunk to select/mark a hunk.
symbol-overlay: Select symbol at point with a keystroke (M-i). Then you can do other things with it, like copy, search, jump, replace, etc.
Use Control-Space to set a mark and move your cursor.
The transient-mark-mode will highlight selections for you. M-x transient-mark-mode.
You can setup Emacs to enable this mode by default using a customization. M-x customize-option RET transient-mark-mode.
... and in case you are using Ubuntu and Ctrl + space is not working for you: you need to clear the Intelligent Input Bus (IBus) "next input method" key binding, as in
run ibus-setup and change the key binding for
"next input method" to something else (or delete it entirely by
clicking the "..." button and then the "Delete" button).
The quote is taken from an answer to a Stack Overflow question.
To expand answer of Edin Salkovic, if you use CUA mode, you can use Ctrl + Enter to begin a visual block selection. There are plenty of shortcuts to control block selection described in the documentation of CUA.
With Emacs 25, simply press Ctrl + Space and then move your cursor wherever you want to highlight/select the region of text which interests you. After that, you may need these commands:
Ctrl + W for cutting.
Alt + W for copying.
Ctrl + Y for pasting.