Emacs: what is the shortcut key to clear buffer? - emacs

Like, Control-A (select all) followed by delete?

How about using:
M-x erase-buffer
Which you could bind to whatever you want.

C-x h + del key clears the buffer
Note: This requires transient-mark-mode to be enabled (which it is by default).

Select all in Emacs is:
C-x h
(technically, that's mark-whole-buffer) and kill-region (to kill the marked region, which is now the entire buffer) is:
C-w
If you want to delete the region without copying it to the kill-ring, you can use
M-x delete-region
instead. If you do this alot, you'll want to bind delete-region to a key/key combo.

These macros build on the answers given above. To start using them paste them into your .emacs then restart emacs or (while in the .emacs buffer) type M-x eval-buffer.
(defun clear-buffer ()
"clear whole buffer add contents to the kill ring"
(interactive)
(kill-region (point-min) (point-max))
)
(defun clear-buffer-permenantly ()
"clear whole buffer, contents is not added to the kill ring"
(interactive)
(delete-region (point-min) (point-max))
)

Old folks might like to call it hk rather than clear-buffer-permanently,
and assign it to the nostalgic key sequence like so:
(define-key esc-map "\^[hk" 'hk)
That's the pre-Gnu TECO EMACS command to clear the buffer
(ESC ESC wHole Kill).

Go to the begin of the buffer, go to the end (both set the mark), then cut:
M-< M-> C-w

There is no shortcut, but you can define one...
Follow this link to get a macro for clearing a buffer.

Related

How to set a keybinding to create and jump to the next line in emacs?

I have the following code that attempts to create a new line and then jump to it. The idea is that move-end-of-line will jump to the end of the current line, and ["C-m"] would act as return/enter. Yet executing this command gives the error: "wrong number of arguments". How do I fix this?
(global-set-key (kbd "C-.") 'new-line)
(defun new-line ()
(interactive)
(move-end-of-line)
["C-m"]
)
I think you need to read the Emacs & elisp manuals: these questions are pretty easy to answer. Here's one way to do it.
(defun insert-line-after-line (&optional n)
(interactive "p")
(end-of-line 1) ;end of current line
(open-line n) ;open n new lines
(forward-line 1)) ;go to start of first of them
But seriously: Emacs has very extensive self-documentation, it is easy to find out how to do these things.
An option is to record a macro and use that.
M-x kmacro-start-macro
C-e
C-m
M-x kmacro-end-macro
If you don't care about the macro persisting, just run it:
C-x e
But if you want it to persist you would save it:
M-x name-last-kbd-macro new-line
M-x insert-kbd-macro new-line
and paste the output into your initialisation file (with your shortcut definition):
(global-set-key (kbd "C-.") 'new-line)
(fset 'new-line
[end return])
["C-m"] is like the way you specify a key for doing a key binding, but this is not the same as how you programmatically tell Emacs to insert a character into a document. You could use (insert-char ?\^M) (see ref here), but that would result in a literal ^M character (try it; another way to do the same thing interactively is Ctrl-Q followed by Ctrl-M). (insert "\n") seems to be what you're looking for.
Also, the reason you're getting the message "wrong number of arguments" is because (move-end-of-line) requires an argument when called out of interactive context. (move-end-of-line 1) works.
That said, possibly an easier way to achieve the result you're looking for is with the following setting:
(setq next-line-add-newlines t)
This will allow you to just do C-n at the end of the file and not worry about the distinction between moving and inserting.

How to copy path of a file in emacs helm-mode

I'm using helm-mode in emacs to open files. However, when I try to copy the path of a file (say /home/user1/Documents/file1.txt) through mouse left-click and hold to paste it in terminal, I get a message saying
<down-mouse-1> is undefined
I guess helm does not support mouse operations as described here, in which case how can I copy path of a file from emacs (in helm-mode) to paste it in terminal
The answer given in this other thread may seem more straightforward.
In short: with the file selected in the minibuffer use C-u C-c C-k. This invokes helm-kill-selection-and-quit. The file's full path is copied to the kill ring and can be pasted in Emacs or elsewhere.
I guess you want to copy from Minibuffer to your system clipboard. Minibuffer keybinding isn't different from other buffers. If in other buffers you use M-w to copy the region, it should also work in Minibuffer. Note that if you niled x-select-enable-clipboard you need to enable it first. I have the following functions in my init.el
(defun copy-to-clipboard()
(interactive)
(setq x-select-enable-clipboard t)
(kill-ring-save (region-beginning) (region-end))
(setq x-select-enable-clipboard nil))
and
(defun paste-from-clipboard ()
(interactive)
(setq x-select-enable-clipboard t)
(yank)
(setq x-select-enable-clipboard nil))
Unfortunately you can't use your mouse to select the texts (ie. to make a region) in helm-mode; you need to set-mark-command (by default C-SPC or C-#) and move your point (ie. cursor). Or just hold the shift and move the point like most other text editors. There is also a mark-word command (by default M-#) that expands the region word by word.
I also recorded an asciinema (because they're fun 🙂) that you can watch it here

emacs: how to kill a process running in current window? [duplicate]

How to kill an internal process in Emacs? For example I run M-x shell.
I can check running processes with M-x list-processes but how can I kill a process from this list?
There is no default key binding for this; however see pjammer's answer -- list-processes+ includes (among other things) a kill binding on C-k -- and also Joao Tavora's answer -- which provides just a kill binding (for the same key).
event_jr points out in the comments that you can use M-: (kill-process) RET to kill the current buffer's process.
More generally: You can use M-: (kill-process PROCESS) RET, where PROCESS "may be a process, a buffer, or the name of a process or buffer", with those names being as they appear in the output of list-processes. Process names take precedence over buffer names, should you happen to have a conflict; so it's probably best to be in the habit of supplying the process name.
Alternatively, Emacs 23+ has a general system process manager (M-x proced) which is more akin to running top, and which does have a default binding for sending (arbitrary) signals (k). Of course it may be far less obvious in that listing which process you're interested in.
Edit: Better late than never :) The following enables M-x kill-process RET to be used (tested in Emacs 26.1):
;; Enable M-x kill-process (to kill the current buffer's process).
(put 'kill-process 'interactive-form
'(interactive
(let ((proc (get-buffer-process (current-buffer))))
(if (process-live-p proc)
(unless (yes-or-no-p (format "Kill %S? " proc))
(error "Process not killed"))
(error (format "Buffer %s has no process" (buffer-name))))
nil)))
This thread is ancient but here's a very quick hack that works perfectly for me
(define-key process-menu-mode-map (kbd "C-k") 'joaot/delete-process-at-point)
(defun joaot/delete-process-at-point ()
(interactive)
(let ((process (get-text-property (point) 'tabulated-list-id)))
(cond ((and process
(processp process))
(delete-process process)
(revert-buffer))
(t
(error "no process at point!")))))
An alternative way:
You can use M-x eval-expression RET
Then type: (delete-process "<name-of-the-process>") RET
(where "name-of-the-process" was previously obtained from M-x list-processes RET).
Confirm that the process was killed by repeating M-x list-processes RET).
And that's it.
it looks like there is a new mode or add on you can use instead called list process +
If you use counsel you can run M-x counsel-list-processes. You can then type M-o to bring up actions, one of which is kill.

How to Kill buffer in emacs without answering confirmation?

How to kill the buffer in emacs without being questioned.
This will kill the current visible buffer without confirmation unless the buffer has been modified. In this last case, you have to answer y/n.
(global-set-key [(control x) (k)] 'kill-this-buffer)
I use this
(defun volatile-kill-buffer ()
"Kill current buffer unconditionally."
(interactive)
(let ((buffer-modified-p nil))
(kill-buffer (current-buffer))))
(global-set-key (kbd "C-x k") 'volatile-kill-buffer) ;; Unconditionally kill unmodified buffers.
It will kill the buffer unless it's modified.
OK, I've done some poking around in the Emacs manual and found a working solution (as of Emacs 23.4.1). It's almost identical to Noufal's solution:
(defun kill-this-buffer-volatile ()
"Kill current buffer, even if it has been modified."
(interactive)
(set-buffer-modified-p nil)
(kill-this-buffer))
I've renamed the function a bit to make it a closer cousin to kill-this-buffer.
Apparently, the EmacsWiki has a page on this topic at http://www.emacswiki.org/emacs/KillBufferUnconditionally (modified in 2007), but the code is just a copy of Noufal's.
Use (kill-current-buffer) instead of (kill-this-buffer) if you want to bind it to some key. See the docs for (kill-this-buffer)
...
This command can be reliably invoked only from the menu bar,
otherwise it could decide to silently do nothing.
and (kill-current-buffer)
...
This is like ‘kill-this-buffer’, but it doesn’t have to be invoked
via the menu bar, and pays no attention to the menu-bar’s frame.
So I would put the following in my init.el:
(global-set-key (kbd "C-x k") 'kill-current-buffer)
This works at least in emacs 26.1.
I use the following piece of code -- unlike Noufal's solution of ignoring the buffer being modified or not, this will save the buffer and then kill it. It also deletes the window which makes a difference when you have several sub-windows showing -- by default it will remove the window instead of switching to some other buffer. (To use this conveniently, you need to bind some key to it, of course.)
;; Kill the current buffer immediatly, saving it if needed.
(defvar kill-save-buffer-delete-windows t
"*Delete windows when `kill-save-buffer' is used.
If this is non-nil, then `kill-save-buffer' will also delete the corresponding
windows. This is inverted by `kill-save-buffer' when called with a prefix.")
(defun kill-save-buffer (arg)
"Save the current buffer (if needed) and then kill it.
Also, delete its windows according to `kill-save-buffer-delete-windows'.
A prefix argument ARG reverses this behavior."
(interactive "P")
(let ((del kill-save-buffer-delete-windows))
(when arg (setq del (not del)))
(when (and (buffer-file-name) (not (file-directory-p (buffer-file-name))))
(save-buffer))
(let ((buf (current-buffer)))
(when del (delete-windows-on buf))
(kill-buffer buf))))

How do I save multiple buffers (of my choosing) at the same time in Emacs?

When I press C-x s (save-some-buffers) or C-x C-c (save-buffers-kill-terminal), Emacs displays the names of modified buffers one by one and asks what to do with each (save, diff, pass, ...). Pressing y one by one is slow. Pressing ! doesn't let you see what buffers are being saved.
How can I have the names of all modified buffers displayed first so that I can mark off some of them and save all the other quickly?
C-x C-b (M-x list-buffers) displays a list of all the buffers. Modified ones will be shown with a * next to them. You can mark a buffer for saving by pressing s. When you're done, press x to save all the buffers you marked.
Unfortunately, as far as I know, there's no way to show only the unsaved buffers or to sort them so they're all at the top.
(I actually prefer M-x ibuffer to M-x list-buffers, but ibuffer provides a similar feature.)
In emacs 23, with ibuffer :
'M-x ibuffer' (to open a list of buffers)
'*u' (start and u at the same time) to marked all unsaved buffers
'S' to save all marked buffers
Strangely enough, *u does not mark 'special' buffers like scratch, compilation, etc... I suppose i regexps on the name ...
Use ibuffer, which should come with all late-model emacsen. Put the following in your .emacs file:
(autoload 'ibuffer "ibuffer" "" t)
(global-set-key (kbd "C-x C-b") 'ibuffer)
(defun my-ibuffer-load-hook ()
"Hook for when ibuffer is loaded."
(define-ibuffer-filter unsaved-file-buffers
"Only show unsaved buffers backed by a real file."
(:description "unsaved file buffers")
(and (buffer-local-value 'buffer-file-name buf)
(buffer-modified-p buf)))
(define-key ibuffer-mode-map (kbd "/ *") 'ibuffer-filter-by-unsaved-file-buffers)
)
;; (add-hook 'ibuffer-load-hook 'my-ibuffer-load-hook)
(eval-after-load 'ibuf-ext '(my-ibuffer-load-hook))
Then use C-x C-b to bring up the ibuffer list, and / * to show just unsaved buffers backed by a real file (so you don't see *scratch* in the list, for example). Mark the desired buffers with m and then save them with S.
The answer to the question in the title is to pass an argument to save-buffers-kill-emacs (or -kill-terminal), ie. use the key combo C-u C-x C-c which will silently save all buffers and exit (or C-u C-x s to just silently save all buffers).
In emacs 23
C-x C-b (M-x list-buffers) to view buffer list
m to mark buffers to save
S to save marked buffers
u to unmark buffers individually or M-x dired-unmark-all-marks for all
I have googled now for this question and found the solution here
http://johntellsall.blogspot.com.es/2013/03/emacs-save-all-modified-buffers.html
You have to add this config to your ~/.emacs.d/init.el emacs configuration
(global-set-key
(kbd "M-*")
(lambda ()
(interactive)
(save-some-buffers t)))
save and eval the buffer (M-evalb-buffer) of the init.el file, and then when you want to save all the modified files you only have to press Meta key with "*" as is indicated in the second line
I hope this solution works!
Juan