开发者

How can I copy the end of the line starting one line above the cursor?

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:

How can I copy the end of the line starting one line above the cursor?

How can I copy the end of the line starting one line above the cursor?

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:

  1. You need to use (interactive) otherwise you can't bind the function to a key.

  2. 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 from save-excursion. (In the later version of this I needed to use a let.)

  3. Parentheses call a function in (e)lisp, so you need to use -1 instead of (-1).

  4. The 2nd-4th arguments to 'line-move will default to nil, 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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜