I want to copy the line above the cursor, but just from the current column to the end of that line.
Here's an illustration:
This was my attempt, but it doesn't work so well :-(
(defun dupchar()
(save-excur开发者_运维百科sion
(line-move (-1) nil nil nil)
(setq mychar (thing-at-point 'char))
(insert mychar))
Try something like this.
(defun dupchar ()
(interactive)
(let ((char-above (save-excursion
(line-move -1)
(following-char))))
(unless (eq char-above ?\n)
(insert char-above))))
(define-key global-map [(meta \")] 'dupchar)
A few comments on the function you wrote:
You need to use
(interactive)
otherwise you can't bind the function to a key.It's not a good idea to just randomly
setq
things—that creates a global variable. In this case you don't need a variable at all; you can make use of the return value fromsave-excursion
. (In the later version of this I needed to use a let.)Parentheses call a function in (e)lisp, so you need to use
-1
instead of(-1)
.The 2nd-4th arguments to
'line-move
will default tonil
, so there's no need to specify them.
(Note: I modified this to stop at the end of the line; it's again hard to understand what you wrote, but this is my best guess.)
not sure if you intended to copy the whole end of the previous line with one keystroke. At least I thought this would be useful so I modified Nicholas (thanks!) code to dupline
:
(defun dupline ()
(interactive)
(let ((line-above-tail (save-excursion
(line-move -1)
(buffer-substring-no-properties (point) (line-end-position)))))
(unless (eq line-above-tail ?\n)
(insert line-above-tail))))
Copying to the end of the line was inspired by How do I duplicate a whole line in Emacs?
精彩评论