Emacs. How to delete until and including bracket? - emacs

I used to use M-d to delete long sub strings in lines like:
if ( aaaaa[dddd(d,s,d)] + bbbbbb[ssd] ) {
but it always annoying me that i need to delete the last bracket. For example to delete first term aaaaa[dddd(d,s,d)] i need to press M-d 4 times and C-d 2 times.
I wonder, is there a command which will delete every-thing until a closing bracket, which corresponds to first opening bracket?
So it should delete whole dddd(d,s,d) if your cursor stays at d, whole aaaaa[dddd(d,s,d)] if you start from a and whole if ( aaaaa[dddd(d,s,d)] + bbbbbb[ssd] ) if you start at the beginning of the line.
In principle set of commands M-d C-space M-C-f C-w will do the job, but I looking for one standard solution.

Try either M-C-k (kill-sexp), or M-z ] (zap-to-char).

I'm using the code from this question
to do the task that you describe.
It's basically a generalized kill-sexp - it will kill
any list with the point inside it. Also works for strings.

smartparens-mode (https://github.com/Fuco1/smartparens or MELPA) knows how to deal with expressions enclosed in various types of parentheses/brackets/etc. It's behavior is often language-specific. For instance, if you bind
(define-key sp-keymap (kbd "C-M-k") 'sp-kill-sexp)
then if you are on the first( in your expression, C-M-k will kill everything including the final ). Or see this c++ example. I realize this is not the exact behavior you described but the package has many (mode-specific) tweaks configuration options.
As an alternative, the regular zap-up-to-char and zap-to-char accept numerical argument. I bound zap-up-to-char to M-z, so, say C-u 2 M-z ) kills everything up to final ).

I use these methods:
(defun zdo/zap-up-to-pair-and-delete-pair ()
(interactive)
(call-interactively 'zap-up-to-char)
(let
((delete-pair-blink-delay 0))
(delete-pair))
)
(defun zdo/zap-up-to-pair-and-delete-pair-round ()
(interactive)
(zap-up-to-char 1 ?\()
(let
((delete-pair-blink-delay 0))
(delete-pair))
)
In case (| is cursor location)
WHERE foo = ${|escape(bar)}
the second method makes it
WHERE foo = ${|bar}

Related

A quick way to repeatedly enter a variable name in Emacs?

I was just typing in this sort of code for Nth time:
menu.add_item(spamspamspam, "spamspamspam");
And I'm wondering if there's a faster way to do it.
I'd like a behavior similar to yasnippet's mirrors, except
I don't want to create a snippet: the argument order varies from
project to project and from language to language.
The only thing that's constant is the variable name that needs to be
repeated several times on the same line.
I'd like to type in
menu.add_item($,"")
and with the point between the quotes, call the shortcut and start typing,
and finally exit with C-e.
This seems advantageous to me, since there's zero extra cursor movement.
I have an idea of how to do this, but I'm wondering if it's already done,
or if something better/faster can be done.
UPD The yasnippet way after all.
Thanks to thisirs for the answer. This is indeed the yasnippet code I had initially in mind:
(defun yas-one-line ()
(interactive)
(insert "$")
(let ((snippet
(replace-regexp-in-string
"\\$" "$1"
(substring-no-properties
(delete-and-extract-region
(line-beginning-position)
(line-end-position))))))
(yas/expand-snippet snippet)))
But I'm still hoping to see something better/faster.
yasnippet can actually be used to create a snippet on-the-fly:
(defun yas-one-line ()
(interactive)
(let ((snippet (delete-and-extract-region
(line-beginning-position)
(line-end-position))))
(yas-expand-snippet snippet)))
Now just type:
menu.add_item($1,"$1")
and call yas-one-line. The above snippet is expanded by yasnippet!
You could try
(defvar sm-push-id-last nil)
(defun sm-push-id ()
(interactive)
(if (not sm-push-id-last)
(setq sm-push-id-last (point))
(text-clone-create sm-push-id-last sm-push-id-last
t "\\(?:\\sw\\|\\s_\\)*")
(setq sm-push-id-last nil)))
after which you can do M-x sm-push-id RET , SPC M-x sm-push-id RET toto and that will insert toto, toto. Obviously, this would make more sense if you bind sm-push-id to a convenient key-combo. Also this only works to insert a duplicate pair of identifiers. If you need to insert something else, you'll have to adjust the regexp. Using too lax a regexp means that the clones will tend to overgrow their intended use, so they may become annoying (e.g. you type foo") and not only foo but also ") gets mirrored on the previous copy).
Record a macro. Hit F3 (or possibly C-x (, it depends) to begin recording. Type whatever you want and run whatever commands you need, then hit F4 (or C-x )) to finish. Then hit F4 again the next time you want to run the macro. See chapter 17 of the Emacs manual for more information (C-h i opens the info browser, the Emacs manual is right at the top of the list).
So, for example, you could type the beginning of the line:
menu.add_item(spamspamspam
Then, with point at the end of that line, record this macro:
F3 C-SPC C-left M-w C-e , SPC " C-y " ) ; RET F4
This copies the last word on the line and pastes it back in, but inside of the quotes.

emacs multi-keystroke binding [duplicate]

This question already has answers here:
How to write a key bindings in emacs for easy repeat?
(5 answers)
Closed 8 years ago.
I'm still very new to EMACS, but are getting familiar when i'm going through the emacs and elisp manual. But right now i'm stuck on this:
Is there a simple way to bind input sequences in regexp style?
eg: the default binding for function enlarge-window-horizontally is "C-x {", is it possible to rebind it to something like "C-x ({)+" so that enlarge-window-horizontally can be called repeatedly by repeating "{" character, instead of release Ctrl key multiple times?
There is another way to archive what you desire:
The first time you want to repeat the last command, press C-x z, afterwards you may repeat your command as often as desired by just pressing z.
The advantage of this approach is that it works with every command you use and not just for a specific one.
For additional reference here is the output of C-h f
8.11 Repeating a Command
Many simple commands, such as those invoked with a single key or with
M-x COMMAND-NAME , can be repeated by invoking them with a
numeric argument that serves as a repeat count (*note Arguments::).
However, if the command you want to repeat prompts for input, or uses
a numeric argument in another way, that method won't work.
The command C-x z (`repeat') provides another way to repeat an
Emacs command many times. This command repeats the previous Emacs
command, whatever that was. Repeating a command uses the same
arguments that were used before; it does not read new arguments each
time.
To repeat the command more than once, type additional z's: each
z repeats the command one more time. Repetition ends when you type
a character other than z, or press a mouse button.
For example, suppose you type C-u 2 0 C-d to delete 20
characters. You can repeat that command (including its argument) three
additional times, to delete a total of 80 characters, by typing C-x z
z z. The first C-x z repeats the command once, and each subsequent
z repeats it once again.
The "Emacs way" is to use C-u as a prefix key. E.g. C-u20C-x{.
Having said that, it's possible to do what you ask for. However, it would require you to bind C-x { and { separately. The former would be defined like it is today, but the latter would have to look something like:
(defun my-open-brace ()
(interactive)
(if (eq last-command 'shrink-window-horizontally)
(progn
(setq this-command 'shrink-window-horizontally)
(call-interactively 'shrink-window-horizontally))
(call-interactively 'self-insert-command)))
Unfortunately, if you have many sequences ending in {, you would have to write one function to handle them all.
You can also define your own repeatable command and bind it to C-x {. You can then use it exactly as you requested: C-x { { { {..., instead of having to use C-x { C-x z z z z...
Here is what you do:
(defun your-repeat-command (command)
"Repeat COMMAND."
(let ((repeat-message-function 'ignore))
(setq last-repeatable-command command)
(repeat nil)))
(defun your-shrink-window-horizontally ()
"Shrink window horizontally.
You can repeat this by hitting the last key again..."
(interactive)
(require 'repeat nil t)
(my-repeat-command 'shrink-window-horizontally))
(define-key ctl-x-map "{" 'your-shrink-window-horizontally)
You can do this with any command you like --- use my-repeat-command to make a repeatable version of it. I do this all the time, in several of my libraries.
Write a multi repeat command for emacs by using minor mode. I name it smart-repeat-mode
https://github.com/zhsfei/emacs-ext

Repeat-like copycat function for Emacs

I would like some copycat function, that takes the previous input and repeats it (like repeat), but does not get written over when something else is done, and thus remains repeatable. Anyone has any ideas?
EDIT: The way I intend this is to have some mode in which D keypress will act exactly like repeat (if some other input has been done, repeat that), while d will repeat the last thing assigned to the last D key press.
EDIT2: If I would yank, and then press C-x z (in my mode also bound to D), then it will repeat the yank. However, when I would move the cursor down, and I try to press D, it then repeats the down cursor. In this case, I would like the small d to do the behavior of the last repeat (that is, yank) while D would repeat the down cursor command. So, d would store the last repeated command, while D would repeat the last command.
This was just too long for a comment:
It feels like you essentially want a shorter version of keyboard macros? I'll try to explain briefly, and see if it is close:
C-x ( - start recording the macro.
Do whatever you want (may be just a single command). For example, yank something, i.e. M-d
C-x ) - finish recording the macro.
Now you can C-x e to replay the macro (you can do other stuff after you've recorded the macro, C-x e will do what you have previously recorded (i.e. M-d in this case).
Maybe you can create a shorthand version of start-macro end-macro recording, if you are sure there will be only one command, but these are really minor improvements. Once you get used to macros, you'll do it unconsciously, so that one keystroke saved won't matter really.
Also, if I didn't guess what you were after, this may prove to be interesting to you: http://www.gnu.org/software/emacs/manual/html_node/elisp/Command-History.html
My best attempt. It works, though it didn't incorporate all the error handling that repeat has.
(defun Navi-repeat ()
;; Checks whether the last repeatable command is the same as repeat var.
;; If yes, set repeat-navi to that command, and call it.
;; If no, check whether the Navi-repeat variable has been set before:
;; If bound, call it.
;; If not bound,
;; give it the value of the last-repeatable command, and call it.
(interactive)
(if (eq last-repeatable-command 'repeat)
(progn (setq repeat-navi repeat-previous-repeated-command)
(call-interactively repeat-navi))
(if (boundp 'repeat-navi)
(call-interactively repeat-navi)
(progn (setq repeat-navi last-repeatable-command)
(call-interactively repeat-navi))
)
)
)

emacs equivalent of ct

looking for an equivalent cut and paste strategy that would replicate vim's 'cut til'. I'm sure this is googleable if I actually knew what it was called in vim, but heres what i'm looking for:
if i have a block of text like so:
foo bar (baz)
and I was at the beginning of the line and i wanted to cut until the first paren, in visual mode, I'd do:
ct (
I think there is probably a way to look back and i think you can pass more specific regular expressions. But anyway, looking for some emacs equivalents to doing this kind of text replacement. Thanks.
Here are three ways:
Just type M-dM-d to delete two words. This will leave the final space, so you'll have to delete it yourself and then add it back if you paste the two words back elsewhere.
M-z is zap-to-char, which deletes text from the cursor up to and including a character you specify. In this case you'd have to do something like M-2M-zSPC to zap up to and including the second space character.
Type C-SPC to set the mark, then go into incremental search with C-s, type a space to jump to the first space, then C-s to search forward for the next space, RET to terminate the search, and finally C-w to kill the text you selected.
Personally I'd generally go with #1.
as ataylor said zap-to-char is the way to go, The following modification to the zap-to-char is what exactly you want
(defun zap-up-to-char (arg char)
"Like standard zap-to-char, but stops just before the given character."
(interactive "p\ncZap up to char: ")
(kill-region (point)
(progn
(search-forward (char-to-string char) nil nil arg)
(forward-char (if (>= arg 0) -1 1))
(point))))
(define-key global-map [(meta ?z)] 'zap-up-to-char) ; Rebind M-z to our version
BTW don't forget that it has the ability to go backward with a negative prefix
That sounds like zap-to-char in emacs, bound to M-z by default. Note that zap-to-char will cut all the characters up to and including the one you've selected.

Matching braces in Emacs

In GNU Emacs there is a feature to highlight matching brackets in code with the same colour.
However when the code which the brackets enclose is really long with several nested if's for's etc. then this feature is not really useful since one of the brackets will not be visible.
Say I have the following,
for(int i=0; i< N; ++i)
{
/*Long code*/
}
If my cursor is on the } brace I would like to have some feature which will enable me to jump / see the { brace, and then , if satisfied, come back to the } brace for any future coding.
Is this possible in Emacs?
This is actually a very standard binding: C-M-f and C-M-b to go back and forwards by default. In most modes C-M-f will take you forwards to the matching brace and C-M-b will take you backwards to the matching brace. This also works for things like quotes, pretty much the same way.
These bindings are easy to remember if you already use C-f and C-b for navigation. (If you don't, you should.) They're just like moving forward and backwards by a character lifted to moving by expression (which depends on mode).
The first thing that might help is knowing about this option, if you don't already: blink-matching-paren-distance. If the sexp is very large then you need to increase the option value, or else paren matching gives up too soon and it shows a mismatch when there is no mismatch.
The second thing that can help is to be sure that blink-matching-paren and blink-matching-paren-on-screen are both non-nil. Then, to see the opening delimiter, just delete the closing delimiter and then type it again. When you insert it, the opening one will be made evident.
backward-sexp is what I use. bound to ESC-left. Then ESC-right to get back to where you were
Try mic paren which shows matching parenthesis code even if found outside the present screen.
(defun px-match-paren (arg)
"Go to the matching paren if on a paren; otherwise insert <key>."
(interactive "p")
(cond
((char-equal 41 (char-before)) (backward-list 1))
((char-equal 125 (char-before)) (backward-list 1))
((and
(char-equal 123 (char-before))
(char-equal 10 (char-after)))
(backward-char 1) (forward-list 1))
((looking-at "\\s\(") (forward-list 1))
((looking-at "\\s\)") (backward-list 1))
(t (self-insert-command (or arg 1)))))
The <key> to witch you bind this command will toggle opening and closing brace if point is before or after it, otherwise insert <key> (much like vi's "%")
I also bind C-<key> to forward-sexp and C-S-<key> to backward-sexp so I can really quickly navigate through functions in the file.