delete line backwards (Emacs) - emacs

This is probably basic but I really tried to find the answer. "C-k" deletes from the cursor to the end of the line, but is there an analogous shortcut to delete a line backwards from the cursor point?
Best

Try C-u 0 C-k - i.e. C-k with the prefix 0 kills from point to the start of the line. See the documentation for C-k (kill-line) for more information.

As other answers suggest, C-0C-k kills from point to the start of the line. See the documentation for C-k (kill-line) for more information. You may also want to kill the whole line before and after point with C-S-backspace which evals kill-whole-line.
Another convenience for killing is to rebind kill-region which defaults to C-w and instead bind to that key the function backward-kill-word which will then mimic the behavior of readline's C-w (unix-word-rubout). I rebind kill-region to C-q after moving quoted-insert to A-q. Yes, this requires moving a number of keys around, but if you give it a try I think you'll find it is convenient.

Here's asjo's answer bound to a key:
(global-set-key "\M-k" '(lambda () (interactive) (kill-line 0)) ) ;M-k kills to the left

I tried C-u with a negative number and C-k as arguments. It worked.
Example to delete 4 lines before cursor, try C-u -4 C-k

That's not exactly to the beginning of a line, but you might want to check out the hungry-delete package. Install, then add the following to the init file:
(add-hook 'after-init-hook (lambda ()
(require 'hungry-delete)
(global-hungry-delete-mode)))
After that DEL would delete up to the next non-whitespace character, and so would Backspace, but in the opposite direction.

M-0C-k (because C-0C-k doesn't work on my computer).
To delete all blank spaces in the beginning of a line:
M-mM-0C-k.

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/

end-of-visual-line go to beginning of next line in Emacs

I want to use beginneing-of-visual-line and end-of-visual-line.
So I wrote in .emacs like following.
(global-set-key "\C-a" 'beginning-of-visual-line)
(global-set-key "\C-e" 'end-of-visual-line)
beginning-of-visual-line works as I hoped.
But if I use C-e in visually multiple lines, cursor go to the beginning of next line.
Why is this happens? And how can I fix it?
I'm using Emacs 24.3 (9.0) in Mac OS X 10.7.5.
I think in general, end-of-line will position the point on the next character after the end of the line.
I find this helpful when coding in lisp, because it places the point in the perfect place to evaluate the previous s-exp easily.
However, in the case of end-of-visual line, the first character after the end of the line is actually on the next visual line. If you want the point to be on the last character of the current visual line, you could just go back one character, like this:
(global-set-key (kbd "C-e") '(lambda ()
(interactive)
(end-of-visual-line)
(backward-char)))
Edit
A much better way of achieving the same thing - which will fix the point in your comment, is just to enable visual-line-mode:
(global-visual-line-mode 1)
This will automatically make your C-a and C-e work with visual lines.
The only problem with visual-line mode is that by default, there are no indicators showing where the line is wrapped. To fix this, you can use:
(setq visual-line-fringe-indicators '(left-curly-arrow right-curly-arrow))
You'll need to put the visual-line-fringe-indicators assignment before the global-visual-line-mode call in your .emacs for it to work.
Source for the above: http://www.emacswiki.org/emacs/VisualLineMode

How can I reload .emacs after changing it?

How can I get Emacs to reload all my definitions that I have updated in .emacs without restarting Emacs?
You can use the command load-file (M-x load-file, and then press Return twice to accept the default filename, which is the current file being edited).
You can also just move the point to the end of any sexp and press C-x, C-e to execute just that sexp. Usually it's not necessary to reload the whole file if you're just changing a line or two.
There is the very convenient
M-x eval-buffer
It immediately evaluates all code in the buffer. It's the quickest method if your .emacs file is idempotent.
You can usually just re-evaluate the changed region. Mark the region of ~/.emacs that you've changed, and then use M-x eval-region RET. This is often safer than re-evaluating the entire file since it's easy to write a .emacs file that doesn't work quite right after being loaded twice.
If you've got your .emacs file open in the currently active buffer:
M-x eval-buffer
Solution
M-: (load user-init-file)
Notes
you type it in Eval: prompt (including the parentheses)
user-init-file is a variable holding the ~/.emacs value (pointing to the configuration file path) by default
(load) is shorter, older, and non-interactive version of (load-file); it is not an Emacs command (to be typed in M-x), but a mere Elisp function
Conclusion
M-: > M-x
M-x load-file
~/.emacs
Others already answered your question as stated, but I find that I usually want to execute the lines that I just wrote.
For that, Ctrl + Alt + X in the Elisp part works just fine.
The following should do it...
M-x load-file
I suggest that you don't do this, initially. Instead, start a new Emacs session and test whatever changes you made to see if they work correctly. The reason to do it this way is to avoid leaving you in a state where you have an inoperable .emacs file, which fails to load or fails to load cleanly. If you do all of your editing in the original session, and all of your testing in a new session, you'll always have something reliable to comment out offending code.
When you are finally happy with your changes, then go ahead and use one of the other answers to reload. My personal preference is to eval just the section you've added/changed, and to do that just highlight the region of added/changed code and call M-x eval-region. Doing that minimizes the code that's evaluated, minimizing any unintentional side-effects, as luapyad points out.
Keyboard shortcut:
(defun reload-init-file ()
(interactive)
(load-file user-init-file))
(global-set-key (kbd "C-c C-l") 'reload-init-file) ; Reload .emacs file
C-x C-e ;; current line
M-x eval-region ;; region
M-x eval-buffer ;; whole buffer
M-x load-file ~/.emacs.d/init.el
Define it in your init file and call by M-x reload-user-init-file
(defun reload-user-init-file()
(interactive)
(load-file user-init-file))
I'm currently on Ubuntu 15.04 (Vivid Vervet); I like to define a key for this.
[M-insert] translates to Alt + Ins on my keyboard.
Put this in your .emacs file:
(global-set-key [M-insert] '(lambda() (interactive) (load-file "~/.emacs")))
Besides commands like M-x eval-buffer or M-x load-file, you can restart a fresh Emacs instance from the command line:
emacs -q --load "init.el"
Usage example: Company backends in GNU Emacs
Here is a quick and easy way to quick test your config. You can also use C-x C-e at the end of specific lisp to execute certain function individually.
C-x C-e runs the command eval-last-sexp (found in global-map), which
is an interactive compiled Lisp function.
It is bound to C-x C-e.
(eval-last-sexp EVAL-LAST-SEXP-ARG-INTERNAL)
Evaluate sexp before point; print value in the echo area.
Interactively, with prefix argument, print output into current buffer.
Normally, this function truncates long output according to the value
of the variables ‘eval-expression-print-length’ and
‘eval-expression-print-level’. With a prefix argument of zero,
however, there is no such truncation. Such a prefix argument also
causes integers to be printed in several additional formats (octal,
hexadecimal, and character).
If ‘eval-expression-debug-on-error’ is non-nil, which is the default,
this command arranges for all errors to enter the debugger.
Although M-x eval-buffer will work, you may run into problems with toggles and other similar things. A better approach might be to "mark" or highlight what’s new in your .emacs file (or even scratch buffer if you're just messing around) and then M-x eval-region.
You can set a key binding for Emacs like this:
;; Reload Emacs configuration
(defun reload-init-file ()
(interactive)
(load-file "~/.emacs"))
(global-set-key (kbd "C-c r") 'reload-init-file)
If you happen to have a shell opened inside Emacs, you can also do:
. ~/.emacs
It may save a few key strokes.

Unable to clear a beginning of a line by C-u in Emacs

I run the following command in Emacs unsuccessfully
C-u
How can you clear the beginning of a line in Emacs?
You can also accomplish this by using a prefix arg for kill-ine (usually C-k). From the Emacs help (C-h k C-k):
With zero argument, kills the text before point on the current line.
So, you can do
C-u 0 C-k
or even better
C-0 C-k
You can set the mark, go to the beginning, kill till mark:
C-Spc C-a C-w
Try this:
M-0 C-k
Delete from beginning of line to point
C-u works in Bash "Emacs Mode", but not actually in Emacs. Here's what I usually do:
C-a C-k
But this is really only good if you want to kill the whole line. Svante's advice will clear from the beginning of the line to where your cursor was, as you asked for.
I don't know, where you read about C-u, but it is bound to the universal argument in Emacs.
If you want to kill the whole line, call kill-whole-line which is bound C-S-backspace. No matter at what column is the cursor, it will kill the whole line from beginning to end.
I have a small function bound to a key-chord:
(defun kill-start-of-line ()
"kill from point to start of line"
(interactive)
(kill-line 0))
(define-key global-map "\M-#" 'kill-start-of-line)
M-# is usually Alt-Shift-3, not a new DotNet language
I'm sure I saw this somewhere else, but didn't save the original reference.

Emacs Command to Delete Up to Non-Whitespace Character

I often want to make a multiline function call and reduce it down to one line. For example, convert...
function_call(
'first_arg',
'second')
to
function_call('first_arg', 'second')
Does emacs have some commands to help with this. Specifically, is there a command that will delete all whitespace from the point to the first non-whitespace character?
You might try delete-indentation, my favorite command for joining multiple lines into one line. In your example, put the cursor on the line with "second" and hit M-^ twice. Here are the docs:
M-^ runs the command delete-indentation, which is an interactive compiled Lisp function in simple.el.
It is bound to M-^.
(delete-indentation &optional arg)
Join this line to previous and fix up whitespace at join. If there is a fill prefix, delete it from the beginning of this line. With argument, join this line to following line.
Take a look at the fixup-whitespace function. It comes with Emacs, in simple.el. Its docs are:
Fixup white space between objects around point.
Leave one space or none, according to the context.
A similar function, just-one-space, that
Deletes all spaces and tabs around point, leaving one space
is typically bound to M-SPC.
Specifically, is there a command that will delete all whitespace from the point to the first non-whitespace character?
There's a command that does almost that:
M-\ runs the command delete-horizontal-space
which is an interactive compiled Lisp function in `simple.el'.
It is bound to M-\.
(delete-horizontal-space &optional backward-only)
Delete all spaces and tabs around point.
If backward-only is non-nil, only delete them before point.
You can always use M-z to delete upto a character.
For eg in your case:
M-z ' to delete upto the single quote (unfortunately this will delete the single quote as well, but that is a minor inconvenience).
I use the following macro to "pull" the next line onto the end of the current line, compressing whitespace.
(defun pull-next-line()
(interactive)
(move-end-of-line 1)
(kill-line)
(just-one-space))
This is exactly the opposite of #jrockway's move-line-up and of delete-indentation, which I find more natural. The just-one-space command in the macro is exactly #Mike's M-SPACE.
I bind pull-next-line to M-J (in analogy with Vim's J, for "join", command) using the following in my .emacs.
(global-set-key (kbd "M-J") 'pull-next-line)
Example. Calling pull-next-line on the first line of
function_call(
'first_arg',
'second')
yields
function_call( 'first_arg',
'second')
Calling it a second time yields
function_call( 'first_arg', 'second')
Alt-space will reduce a string of whitespace to a single space character, but it won't delete the newline. Still, that should help a little.
To delete everything from point to the first non-whitespace (or newline), type a non-whitespace char, Alt-space, backspace (to remove final whitespace char), then backspace (to delete the char you added.
To turn the multi-line function declaration into a single-line declaration, use a combination of Alt-space, backspace, and Alt-E (goto-endofline) commands.
A rather drastic way of doing this is Hungry-Delete mode:
Hungry-Delete is a minor-mode that causes deletion to delete all whitespace in the direction you are deleting.
A slightly different approach would be creating a keyboard macro to do the job for you.
so, for creating the macro stage a general scenario like so:
foo
bar
[a line with "foo" then a couple of lines later and with some white spaces, write "bar"]
then standing anywhere between foo and bar, do the following:
C-x ( ; start recording macro
M-b ; move backwards to the beginning of foo
END ; move to the end of foo
C-space ; place mark
C-M-f ; move to the end of bar
HOME ; move to the beginning of the line
C-w ; yank out all the white space
M-SPACE ; leave only one space
C-x ) ; end recording the macro
M-x name-last-kbd-macro ; name it, call it jline or something
Now you can always remove all whitespace between two words with M-x one-line
Make sure you remember to save your keyboard macro by issuing M-x insert-kbd-macro somewhere in your .emacs file - this is how it looks:
(fset 'jline
[?\M-b end ?\C- ?\C-\M-f home ?\C-w escape ? ])
I do this:
(defun move-line-up ()
"Removes leading spaces from the current line, and then moves
the current line to the end of the previous line."
(interactive)
(let (start end)
(save-excursion
(beginning-of-line)
; get first non-space character, only look on this line
(let ((search-end (save-excursion (end-of-line) (point))))
(re-search-forward "[^[:space:]]" search-end))
(setq end (1- (point)))
(previous-line)
(end-of-line)
(setq start (point))
(delete-region start end))
(goto-char start)))
(defun move-next-line-up ()
"Moves the next line to the end of the current line"
(interactive)
(next-line)
(move-line-up))
And bind these as:
(global-set-key (kbd "C-x ,") 'move-line-up)
(global-set-key (kbd "C-x .") 'move-next-line-up)
So to solve your problem, on the line that says "second)", just run C-x , C-x ,
If you want all of your deletes to act that way, you might check out greedy-delete.