Emacs Copy Current Line - emacs

For some reason, copying lines in Emacs is very unintuitive and difficult. If I'm doing something wrong here, please let me know. It is surprising that Emacs does not have this by default somewhere.
I am trying to write a function that copies a line. I always used to have:
(global-set-key (kbd "C-x c") "\C-a\C- \C-n\M-w")
This is a little annoying since it copies any new line after the line. I decided to change it to:
(global-set-key (kbd "C-x c") "\M-m\C- \C-e\M-w")
Now, I saw: http://www.emacswiki.org/emacs/CopyingWholeLines and it appears that their copy-line function prints a message with the number of lines it copied. I am trying to insert that message into my global-set-key above but it is not working. Basically, I am unable to run a raw sequence as above in a function. So I conveyed each keystroke into a function and did this:
(defun copy-line ()
(interactive)
(kill-ring-save
(back-to-indentation)
(move-end-of-line 1))
(message "1 line copied"))
;; optional key binding
(global-set-key "\C-c\C-k" 'copy-line)
This however, throws a wrong number of arguments error.
My first question: How can I put (message "1 line copied") into my global-set-key above?
My second question: using the standard copy-line found in the link above:
(defun copy-line (arg)
"Copy lines (as many as prefix argument) in the kill ring"
(interactive "p")
(kill-ring-save (line-beginning-position)
(line-beginning-position (+ 1 arg)))
(message "%d line%s copied" arg (if (= 1 arg) "" "s")))
From the message, it appears that you can have multiple lines copied. However, when selecting multiple lines and copying, only one is copied. Why is the message structured in this way? How can it select multiple lines?

Here's your function, fixed:
(defun copy-line ()
(interactive)
(save-excursion
(back-to-indentation)
(kill-ring-save
(point)
(line-end-position)))
(message "1 line copied"))
The problem was that back-to-indentation doesn't return point.
As for the other function, it will copy multiple lines when called with
a prefix argument, e.g. C-u or M-5.

Here's a shorter version of the simple function:
(defun copy-line ()
(interactive)
(kill-ring-save (point-at-bol) (point-at-eol))
(message "1 line copied"))
For the multiline copy version that you cited, use the prefix to indicate how many lines you want copied (as the other answer suggests). So, with your keybinding of C-c C-k, do the following to copy, say, 3 lines: C-u 3 C-c C-k.

Related

Apply delete-indentation to a whole region

The function delete-indentation, usually bound to (M-^), joins the current line to the next one and deletes any indentation.
How do I apply this function to a whole region to join multiple lines in a region?
Or is there another way to achieve the desired result?
Thanks
Try unfill-paragraph, taken from the Emacs wiki.
;;; An unfill-paragraph that works in lisp modes
(defun unfill-paragraph (&optional region)
"Takes a multi-line paragraph and makes it into a single line of text."
(interactive (progn (barf-if-buffer-read-only) '(t)))
(let ((fill-column (point-max))
(emacs-lisp-docstring-fill-column t))
(fill-paragraph nil region)))
If you're willing to give up the prefix argument of the standard M-^, you could bind the following function to that keyboard shortcut like so:
(defun join-line-or-unfill-paragraph (&optional unfill)
(interactive "P")
(if unfill (unfill-paragraph) (delete-indentation)))
(global-set-key (kbd "M-^") 'join-line-or-unfill-paragraph)
This way, whenever you type C-u before M-^ the full paragraph will be joined.

(ELisp) automatically nesting next line using brace return

I'm completely new to both Lisp and Emacs. In Emacs, when coding in Java for example, I want to be able to type "{" then hit "ENTER" and have the next line be ready for whatever is nested in the braces. For example, if I have the following line:
public void method()
and I type "{" then hit return I should get this:
public void method() {
// indentation applied, no additional tabbing necessary
}
I'm already able to insert by pairs, for example, typing "{" gives "{}" with my cursor between the braces. I did this by adding these lines to the emacs init file:
;; insert by pairs (parens, quotes, brackets, braces)
(defun insert-pair (leftChar rightChar)
(if (region-active-p)
(let (
(p1 (region-beginning))
(p2 (region-end))
)
(goto-char p2)
(insert rightChar)
(goto-char p1)
(insert leftChar)
(goto-char (+ p2 2))
)
(progn
(insert leftChar rightChar)
(backward-char 1) ) )
)
(defun insert-pair-brace () (interactive) (insert-pair "{" "}") )
(global-set-key (kbd "{") 'insert-pair-brace)
To get the auto-nesting I described above, I added these lines:
;; automatically nest next line
(defun auto-nest ()
(insert "\n\n")
(backward-char 1)
(insert "\t")
)
(defun auto-nest-brace () (interactive) (auto-nest) )
(global-set-key (kbd "{ RET") 'auto-nest-brace)
When I start up Emacs, however, I get this message:
error: Key sequence { RET starts with non-prefix key {
What am I doing wrong, and what can I do to fix it? I don't want to use a different key combination to do this. There are a lot of text editors in which this auto-nesting is standard, and it should be easy enough to code up in ELisp.
It's great that you are trying to add this functionality to Emacs yourself, but there's no need to reinvent the wheel here. Emacs already has a command for the purpose of auto-indenting; it's called newline-and-indent. It is bound to C-j by default, but you can rebind it to RET
globally:
(global-set-key (kbd "RET") 'newline-and-indent)
for a specific mode only:
(require 'cc-mode)
(define-key java-mode-map (kbd "RET") 'newline-and-indent)
java-mode-map is defined in cc-mode.el and not available by default, that's why you have to require cc-mode before you can modify java-mode-map.
Note that newline-and-indent indents according to major mode. That is, if you're e.g. in java-mode and press RET in some random location that's not meaningful w/r/t Java syntax, it won't insert additional whitespace at the beginning of the new line.
To read all there is to know about newline-and-indent do
C-h f newline-and-indent RET
I have something similar in my emacs config which I have been using for a while. It calls 'newline-and-indent twice then moves the point one line up before indenting correctly. Here is the snippet of code to do this from my config file:
;; auto indent on opening brace
(require 'cc-mode)
(defun av/auto-indent-method ()
"Automatically indent a method by adding two newlines.
Puts point in the middle line as well as indent it by correct amount."
(interactive)
(newline-and-indent)
(newline-and-indent)
(forward-line -1)
(c-indent-line-or-region))
(defun av/auto-indent-method-maybe ()
"Check if point is at a closing brace then auto indent."
(interactive)
(let ((char-at-point (char-after (point))))
(if (char-equal ?} char-at-point)
(av/auto-indent-method)
(newline-and-indent))))
(define-key java-mode-map (kbd "RET") 'av/auto-indent-method-maybe)
Pretty straightforward as you can see. Hopefully it will work for you. I have not used it in any other modes except java.
You want a combination of auto pairs (or alternatives) plus auto indentation. Check out the emacswiki on the former: http://www.emacswiki.org/emacs/AutoPairs
And on the latter:
http://www.emacswiki.org/emacs/AutoIndentation

delete (NOT kill) a line in emacs. External clipboard is not appended to the kill ring

Many times I find myself in need of pasting a path from wherever to emacs' minibuffer. To clear the minibuffer fast I navigate to the beginning and do C-k (kill line).
This effectively overrides whatever path I had in the system clipboard with the temporary path I just killed in the minibuffer. Navigating the kill ring with M-y won't bring the path I had in the system clipboard.
Is there a way to delete the current line without killing it( i.e. removing it and adding it to the kill ring)?
So far I'm marking the line and pressing delete having delete-selection-mote active. I would like a one key solution similar to C-k.
As of Emacs 23.2, you can set save-interprogram-paste-before-kill to a non-nil value (hat tip Tyler) to copy the clipboard selection onto the kill ring, so that it is available via C-y M-y:
(setq save-interprogram-paste-before-kill t)
If you're on an older Emacs, the following advice has the same functionality:
(defadvice kill-new (before kill-new-push-xselection-on-kill-ring activate)
"Before putting new kill onto the kill-ring, add the clipboard/external selection to the kill ring"
(let ((have-paste (and interprogram-paste-function
(funcall interprogram-paste-function))))
(when have-paste (push have-paste kill-ring))))
And, you could do something like this (horrible keybinding, customize to suit) to delete the line from the point forward:
(define-key minibuffer-local-map (kbd "C-S-d") 'delete-line)
(defun delete-line (&optional arg)
(interactive "P")
;; taken from kill-line
(delete-region (point)
;; It is better to move point to the other end of the kill
;; before killing. That way, in a read-only buffer, point
;; moves across the text that is copied to the kill ring.
;; The choice has no effect on undo now that undo records
;; the value of point from before the command was run.
(progn
(if arg
(forward-visible-line (prefix-numeric-value arg))
(if (eobp)
(signal 'end-of-buffer nil))
(let ((end
(save-excursion
(end-of-visible-line) (point))))
(if (or (save-excursion
;; If trailing whitespace is visible,
;; don't treat it as nothing.
(unless show-trailing-whitespace
(skip-chars-forward " \t" end))
(= (point) end))
(and kill-whole-line (bolp)))
(forward-visible-line 1)
(goto-char end))))
(point))))
As of Emacs 23.2, this problem can be addressed with save-interprogram-paste-before-kill. If you set this variable to t then stuff in the clipboard gets added to the kill-ring, and isn't discarded by your next kill.
The documentation:
Save clipboard strings into kill ring before replacing them.
When one selects something in another program to paste it into Emacs,
but kills something in Emacs before actually pasting it,
this selection is gone unless this variable is non-nil,
in which case the other program's selection is saved in the `kill-ring'
before the Emacs kill and one can still paste it using C-y M-y.
From Xahlee's page, it shows several commands that are annoying.
(defun my-delete-word (arg)
"Delete characters forward until encountering the end of a word.
With argument, do this that many times.
This command does not push erased text to kill-ring."
(interactive "p")
(delete-region (point) (progn (forward-word arg) (point))))
(defun my-backward-delete-word (arg)
"Delete characters backward until encountering the beginning of a word.
With argument, do this that many times.
This command does not push erased text to kill-ring."
(interactive "p")
(my-delete-word (- arg)))
(defun my-delete-line ()
"Delete text from current position to end of line char."
(interactive)
(delete-region
(point)
(save-excursion (move-end-of-line 1) (point)))
(delete-char 1)
)
(defun my-delete-line-backward ()
"Delete text between the beginning of the line to the cursor position."
(interactive)
(let (x1 x2)
(setq x1 (point))
(move-beginning-of-line 1)
(setq x2 (point))
(delete-region x1 x2)))
; Here's the code to bind them with emacs's default shortcut keys:
(global-set-key (kbd "M-d") 'my-delete-word)
(global-set-key (kbd "<M-backspace>") 'my-backward-delete-word)
(global-set-key (kbd "C-k") 'my-delete-line)
(global-set-key (kbd "C-S-k") 'my-delete-line-backward)
There isn't.
from the GNU Emacs Manual:
We have already described the basic deletion commands C-d
(delete-char) and (delete-backward-char). See Erasing.
The other delete commands are those that delete only whitespace
characters: spaces, tabs and newlines. M-\ (delete-horizontal-space)
deletes all the spaces and tab characters before and after point. With
a prefix argument, this only deletes spaces and tab characters before
point. M- (just-one-space) does likewise but leaves a single
space after point, regardless of the number of spaces that existed
previously (even if there were none before). With a numeric argument
n, it leaves n spaces after point.
What about something like:
(defun del-line (p1)
(interactive "d")
(move-end-of-line 1)
(when (eq p1 (point)) ; special case when p1 is already at the end of the line
(forward-line))
(delete-region p1 (point)))
The behavior should be similar to C-k but without affecting the system clipboard or the kill-ring.
ETA: I read Trey's solution more carefully, and it looks like this is just a simple case of his solution. It worked in my (very!) limited tests, but probably fails for some special cases where the more complicated kill-line code works correctly.
Found an answer to this.
Posted it first here: https://unix.stackexchange.com/questions/26360/emacs-deleting-a-line-without-sending-it-to-the-kill-ring/136581#136581
;; Ctrl-K with no kill
(defun delete-line-no-kill ()
(interactive)
(delete-region
(point)
(save-excursion (move-end-of-line 1) (point)))
(delete-char 1)
)
(global-set-key (kbd "C-k") 'delete-line-no-kill)

Delete a word without adding it to the kill-ring in Emacs

When switching files using the minibuffer (C-x C-f), I often use M-Backspace to delete words in the path. Emacs automatically places what I delete into the kill ring. This can be annoying, as sometime I am moving to another file to paste something, and I end up pasting part of the file path. I know there are workarounds, and the other code is still in the kill ring, etc, but I would just like to disable this functionality.
Emacs doesn't have a backward-delete-word function, but it's easy enough to define one:
(defun backward-delete-word (arg)
"Delete characters backward until encountering the beginning of a word.
With argument ARG, do this that many times."
(interactive "p")
(delete-region (point) (progn (backward-word arg) (point))))
Then you can bind M-Backspace to backward-delete-word in minibuffer-local-map:
(define-key minibuffer-local-map [M-backspace] 'backward-delete-word)
See a discussion of this topic at help-gnu-emacs#gnu.org:
http://lists.gnu.org/archive/html/help-gnu-emacs/2011-10/msg00277.html
The discussion boils down to this short solution:
(add-hook 'minibuffer-setup-hook'
(lambda ()
(make-local-variable 'kill-ring)))
You just have to replace the function called by M-<backspace>, namely backward-kill-word, with backward-delete-word, which you can easily define using the source definition of backward-kill-word found in lisp source for emacs. You do this by substituting kill-region for delete-regionas in the following code, which also defines delete-word (delete word after the cursor). You can just paste this code in your .emacs file.
(defun delete-word (arg)
"Delete characters forward until encountering the end of a word.
With argument ARG, do this that many times."
(interactive "p")
(delete-region (point) (progn (forward-word arg) (point))))
(defun backward-delete-word (arg)
"Delete characters backward until encountering the beginning of a word.
With argument ARG, do this that many times."
(interactive "p")
(delete-word (- arg)))
(global-set-key (kbd "M-<backspace>") 'backward-delete-word)
(global-set-key (kbd "M-<delete>") 'delete-word)

In Emacs, can we make one keystroke to do different command?

I want to make one keystroke, say C-F12, to do delete-other-windows or winner-undo. I think it's easy if I already learning Emacs Lisp programming, and set a boolean flag. That is, if previously it run delete-other-window, now it'll run winner-undo.
How do you do that in Emacs Lisp?
Thanks
Try something like this
(setq c-f12-winner-undo t)
(define-key (current-global-map) [C-f12]
(lambda()
(interactive)
(if c-f12-winner-undo
(winner-undo)
(delete-other-windows))
(setq c-f12-winner-undo (not c-f12-winner-undo))))
(defun swdev-toggle-sole-window ()
(interactive)
(if (cdr (window-list))
(delete-other-windows)
(winner-undo)))
(global-set-key (kbd "<C-f12>") 'swdev-toggle-sole-window)
The first line starts the declaration of a function called swdev-toggle-sole-window, taking no argument.
This function is declared as interactive, i.e. it can be called with M-x or through a key binding.
If the window list contains more than one element, i.e. if there is more than one window, …
… then delete other windows …
… else undo the window deletion.
Bind the function to the key C-f12.
Here's a solution using the approach taken by Emacs' recenter-top-bottom function:
(defun delete-other-window-or-winner-undo ()
"call delete-other-window on first invocation and winner-undo on subsequent invocations"
(interactive)
(if (eq this-command last-command)
(winner-undo)
(delete-other-windows)))
(global-set-key (kbd "<C-f12>") 'delete-other-window-or-winner-undo)