Emacs - delete to first non blank character - emacs

I'm looking for key binding / function which delete from current cursor position to first non blank character
example:
function f() {|
test();
| - cursor position
I want delete everything to "t" letter

M-z t t (substitute t as needed :).

Lots of good options have been presented. In Emacs 24 (currently in pretest), you can specify M-- to just-one-space or M-SPC to do exactly what you ask.

In 'c-mode' there is 'M-x c-hungry-delete-forward', which is also bound to C-c C-d. So, you can create that binding in whatever programming mode you're using.
(define-key <whatever>-mode-map (kbd "C-c C-d" 'c-hungry-delete-forward)
Alternatively, you can grab the package 'hungry-delete' and use that to override the deletion commands to delete all the whitespace (as opposed to a single space).

To do this, I use a custom macro :
(fset 'jline
[?\C- ?\C-\M-n ?\C-a ?\C-w return])
(just put this small macro in your .emacs config)
Of course, you can rename the macro like you want.
So, the following state :
function f() {|
test();
}
Becomes :
function f() {
|test();
}

Use M-x delete-blank-lines (which is also bound to C-x C-o).
From the help-docs (C-h k C-x C-o):
On blank line, delete all surrounding blank lines, leaving just one.
On isolated blank line, delete that one.
On nonblank line, delete any immediately following blank lines.

Related

How to uncomment blocks code in emacs

I am using emacs with the major mode "Java/L Abbrev" activated. When I type M-x comment-region or M-x uncomment-region the desired effects happen in the editor. But I am getting tired of typing this out every time.
I have found that I can type C-c C-c and comment a region. I want to find a similiar way to uncomment a region. I go to the emacs docs:
https://www.gnu.org/software/emacs/manual/html_node/ccmode/Comment-Commands.html
And it says to give the C-c C-c command a negative argument to uncomment lines. How do I do this? or is there a better way?
Please try M-;, which is bound by default to comment-dwim. I think this should do what you want.
comment-dwim (DWIM stands for "Do What I Mean") is bound to M-; by default and works differently depending on whether or not the region is active (and sometimes what mode you're in)
From the emacs help page for comment-dwim:
comment-dwim is an interactive compiled Lisp function in
‘newcomment.el’.
It is bound to M-;.
(comment-dwim ARG)
Call the comment command you want (Do What I Mean).
If the region is active and ‘transient-mark-mode’ is on, call
‘comment-region’ (unless it only consists of comments, in which
case it calls ‘uncomment-region’).
Else, if the current line is empty, call ‘comment-insert-comment-function’
if it is defined, otherwise insert a comment and indent it.
Else if a prefix ARG is specified, call ‘comment-kill’.
Else, call ‘comment-indent’.
You can configure ‘comment-style’ to change the way regions are commented.
Your question is how to use C-c C-c to uncomment the region.
#AaronHarris answered your question about using a negative prefix arg.
But I think you misread the doc of comment-region (which CC mode binds to C-c C-). It does not uncomment the region. It deletes a certain number of comment characters.
To uncomment the region you use C-u - a plain prefix arg (no explicit number) to uncomment the region. C-h f comment-region says:
comment-region is an interactive compiled Lisp function in
newcomment.el.
It is bound to menu-bar edit region comment-region.
(comment-region BEG END &optional ARG)
Comment or uncomment each line in the region.
With just C-u prefix arg, uncomment each line in region BEG .. END.
Numeric prefix ARG means use ARG comment characters.
If ARG is negative, delete that many comment characters instead.
The strings used as comment starts are built from comment-start
and comment-padding; the strings used as comment ends are built
from comment-end and comment-padding.
By default, the comment-start markers are inserted at the
current indentation of the region, and comments are terminated on
each line (even for syntaxes in which newline does not end the
comment and blank lines do not get comments). This can be
changed with comment-style.
So the answer is to use C-u C-c C-c.
And FWIW, comment-region is much better than M-; (comment-dwim) for commenting and uncommenting the region. It lets you nest and unnest comment blocks any number of levels.
TLDR: Use C-- C-c C-c; i.e., prefix your command with "control-hyphen"
To give a negative argument to a command, you need to call either the negative-argument command or the universal-argument command, supplying a negative argument. (Try C-h f for more information on these.)
The negative-argument command is bound to keys C--, M--, and C-M--, so all of these will work as prefixes; generally, you'll use the one that's most convenient to type for any given command.
The universal-argument command is bound to C-u and accepts its argument immediately after that, so you can also do C-u -, optionally followed by zero or more digits (e.g., C-u - 5 3 9); that one is overkill here, but good to know about.
Finally, here is the section of the Emacs manual that discusses this topic.
Why not mapping the uncomment-region command to a key? It's not really what you were asking for (previous answer are better for this) but it's a way to stop typing M-x uncomment-region every time
like this (with the key binding you want)
(global-set-key (kbd "C-c C-u") 'uncomment-region)
Documentation about key binding can be found here:
Commands for Binding Keys
Customizing Key Bindings
Here is a map of command:
Map of command
You can use C-h k (M-x describe-key) to show what command is bind to a particular key (so you're sure to not erase it) and C-h f (M-x describe-function) will show you a description of the function + its binding.
First, select the region
To comment, use
ALT + x comment-region
To un-comment, use
ALT + x uncomment-region
Credits: https://www.reddit.com/r/emacs/comments/1kklgl/command_to_uncomment_entire_comment_block/

Insert complete lines with Emacs + Evil

In Vim, I often move lines by deleting them (either with dd or visual line mode), moving my cursor to the new position, then p to put them in:
first
second
third
And if my cursor is on the line second, I can use ddp to move it down:
first
third
second
But with Emacs + Evil mode, putting the line back doesn't work as expected: if, for example, my cursor is on the i in third when I hit p, I end up with:
first
thisecondrd
How can I make Emacs + Evil mode insert new lines when putting entire yanked lines?
I use C-a to go to the beginning of the line (^ in evil-mode, probably) before yanking, if I want that behaviour. If you do this often, you can probably come up with your own thing for yank, although you have to figure out during the kill part if you're doing that. (Or you can check if the yanked thing has newlines, I guess?)
There's a transpose-lines command, by the way (C-x C-t in regular Emacs binding - someone suggested binding this to xtl - https://github.com/syl20bnr/spacemacs/blob/master/my-keybindings.el).
If I find my cursor on a line that I want to move, my natural response is to first delete the line into the kill ring with either C-a C-k C-k or C-a C-space C-n C-w (either of which can also grab several-line sequences by duplicating either the C-k or C-n or prefixing the C-n with a numeric argument) and then travel to the beginning of the line where I want to paste and doing a C-y yank.
Note that Emacs considers a file to be a steam of characters, in which newline or carriage return is not special. Unlike in vi, you can C-f forward right over a newline exactly as though it is a normal character; backspace over it; or include it in a deleted and yanked buffer. It is exactly like any other character. Perhaps Emacs is for people who think of files as sequences of characters — some of which happen to be newlines — and vi is for people who think of their file as lines, that are magically separated by who-knows-what but it certainly is not like any other character.
If the main use case you are trying to address is moving lines up or down (as opposed to the more general question of how to "make Emacs + Evil mode insert new lines when putting entire yanked lines"), I suggest you try out move-text.
It is a very small add-on package that provides two commands (move-text-up and move-text-down) for moving lines up and down, respectively. You can be anywhere on a line and call these; there is no need to kill or yank anything, and they work for regions as well.
For example, calling move-line-down in this situation (point right after second):
first line
second| line
third line
will produce
first line
third line
second| line
As you would expect, moving the current line (or region) up or down n lines works by calling the appropriate command with a numeric prefix.
The commands are bound to M-up and M-down by default but you should be able to rebind them to key sequences of your liking via
(define-key evil-normal-state-map "mu" 'move-line-up)
(define-key evil-normal-state-map "md" 'move-line-down)
move-text is package-installable from MELPA.

Emacs. How to delete until and including bracket?

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}

Running a macro till the end of text file in Emacs

I have a text file with some sample content as shown here:
Sno = 1p
Sno = 2p
Sno = 3p
What i want is to remove the p from each of the columns.
With this intention i write a macro:
M-x //go to buffer
C-x (//start the macro
C-s = // search for equalto sign
RET C-f C-f // reach to te alphabet 'p'
DEL // Delete
C-n C-x )//go to new line and Close the macro definition
C-x e
Pressing e twice will remove p, but in case i want to do the same stuff till the end of file, how can i do it i can't keep pressing e if i have 20000 such lines. What should be done??
Please donot suggest regex, as this is a sample example, not the actual case.
Please donot suggest any elisp, i am comfortable with remembering shortcutf for emacs.
M-0 C-x e to repeat last macro until an error happens (after the final line, either C-s = or C-n will be an error).
You may hear an annoying beep.
You can use the "apply-macro-to-region-lines" function to apply the last defined keyboard macro to all lines in a region. So, the steps you follow are:
Define your keyboard macro (which you had already done but I've added another C-f to the 3rd line):
C-x (
C-s =
RET C-f C-f C-f
DEL
C-n C-x )
Select the whole buffer with the "mark-whole-buffer" command (by default, it's bound to C-x h). Alternatively, if you just want to change a certain number of lines, just select the lines you want changed.
Run the "apply-macro-to-region-lines" function (by default, it's bound to C-x C-k r).
All the p's will be removed.
I usually give a name to the macro after defining it with M-x name-last-kbd-macro, so I can call it with M-x conveniently. And then I call it with a numeric prefix. E.g.
M-1000 M-x macroname
The files I work on usually don't have 1000 places where the macro can act, so 1000 is large enough and it will stop automatically at the end of the file. You can use, say, 1000000 as a prefix if you have larger files, and if it's still not enough then you can call it again with 1000000 as prefix.
you may try: M-20000 C-x e so as to do the stuff for 20000 times, after you define the macro.

Emacs copy with regex

I have a text file. Can Emacs select text based on regex and put it in kill-ring, so I can copy it somewhere else? Something like regex-kill-ring-save?
inspired by the already given comments (the Charles answer doesn't work as I would want it), I added a new function to the isearch/isearch-regexp mode map which puts only the matching string into the kill ring (whereas Charles proposal kills from current point to end of matching string):
(defun hack-isearch-kill ()
"Push current matching string into kill ring."
(interactive)
(kill-new (buffer-substring (point) isearch-other-end))
(isearch-done))
(define-key isearch-mode-map (kbd "M-w") 'hack-isearch-kill)
The nice thing about the isearch/isearch-regexp approach (which you can enable with C-s and C-M-s respectively) is that you can see your search string growing and you can copy it with M-w as soon as you are satisfied (and go back to where you have been before with C-u C-Space).
This works for me with Emacs 23.1. Don't know if it will work in all situations. Anyway I hope you find it useful :)
UPDATE: going through the emacswiki I stumbled over KillISearchMatch which suggests more or less the same (plus some more tips ...).
Cheers,
Daniel
I'm not sure if there is such a function already, but what you can do it with a keyboard macro:
Start recording a kbd macro: C-x (
Search for your regexp with search-forward-regexp
Move to the beginning of your match (the text you want to kill) with the various emacs navigation commands, e.g. search or backward-word etc.
Mark: C-spc
Move to the end of your match
Kill the text: C-w
You can then name the keyboard macro with M-x name-last-kbd-macro so that you can execute the macro with a name rather than with C-x e.
If you want to save the macro for future sessions, you can open your .emacs and insert the macro into the buffer with M-x insert-kbd-macro. After than you can bind a key to the macro just like you bind keys to normal emacs functions, e.g. (global-set-key "\C-c m" 'funky-macro-macro).
More about emacs keyboard macros
Isearch+ does this already. It optionally sets the region around the search target. You can use C-SPC C-SPC or M-= C-SPC at any time during Isearch to toggle this.
isearchp-deactivate-region-flag is a variable defined in isearch+.el.
Its value is t
Documentation:
Non-nil means isearching deactivates the region.
See also option isearchp-restrict-to-region-flag.
You can toggle this option using M-= C-SPC during Isearch.
You can customize this variable.