.emacs: Copy Line

02Jan09

Since I wrote that post a month and a half ago, I figured I’d write a short one tonight. I’m about halfway through The Little Schemer now and I’ve spent a good amount of time trying to get Emacs configured in a way I like. My .emacs file has grown a bit and so I thought I’d share some of the cool things I’ve either found or figured out for myself.

M-x copy-line

;; Copy 1 Line
(defun copy-line ()
  (interactive)
  (kill-ring-save (line-beginning-position) (line-end-position)))

It took me a while to figure out how to get this working, as I could only find a snippet for “M-x copy-n-lines” (below). It’s quite simple after I worked out the syntax. It simply copies the text on a line to the kill-ring. It doesn’t care where your cursor is, just what line you are currently on. I’ve bound it to “M-k” to be analogous to “C-k” (kill line), in the same way “C-w” is kill region (cut) and “M-w” is copy region (copy).

;; Set M-k to be the shortcut for copy line
(global-set-key [(meta k)] 'copy-line)

Pretty simple.

Here is the snippet I based the above command on: (I can’t remember where I found it to attribute…eek)

;; Copy N Lines
(defun copy-n-lines (n)
    "Copy N lines at point to the kill-ring."
    (interactive "p")
    (kill-ring-save (line-beginning-position) (line-beginning-position (1+ n))))

This one isn’t as immediately useful to me as copying a single line. I have been using the copy single line command to copy text I enter directly into the scheme interpreter so I can reuse it. I don’t use this one as much, I tend to just select the region and copy the region.

M-x dot-emacs

(defun dot-emacs ()
  "Visit .emacs"
  (interactive)
  (find-file "~/.emacs"))

This is another snippet I found and that I’ve found to be extremely useful. I generally am deep within my scheme project folders and to delete the saved path in the find-file command buffer gets old fast. This has saved a fair bit of time already and I haven’t even been using Emacs very much. I haven’t bound it to a command yet. It autocompletes after typing ‘dot’.

lambda-mode

;; real lisp hackers use the lambda character
;; courtesy of stefan monnier on c.l.l
(defun sm-lambda-mode-hook ()
  (font-lock-add-keywords
   nil `(("\\"
   (0 (progn (compose-region (match-beginning 0) (match-end 0)
        ,(make-char 'greek-iso8859-7 107))
      nil))))))
(add-hook 'emacs-lisp-mode-hook 'sm-lambda-mode-hook)
(add-hook 'lisp-interactive-mode-hook 'sm-lamba-mode-hook)
(add-hook 'scheme-mode-hook 'sm-lambda-mode-hook)

This is probably the coolest Emacs thing I’ve seen yet. It works absolutely flawlessly. Whenever you write “lambda” it substitutes an actual lambda for you. If you backspace on the delete, it switches it back and you are left with “lambd”. It simply hides it. Amazing. I love Emacs already.

I’m still trying to figure out how to change the font within Emacs so that it displays a better looking lambda character. I haven’t had much luck yet with trying to get anything else working.

-Saterus



2 Responses to “.emacs: Copy Line”

  1. I have a bit of a different copy-line:

    (defun mark-line (&optional arg)
    “Marks a line from start of indentation to end”
    (interactive “p”)
    (back-to-indentation)
    (set-mark (point))
    (end-of-line arg))

    (defun copy-line (&optional arg)
    “Kills a line, not including leading indentation”
    (interactive “p”)
    (mark-line arg)
    (kill-ring-save (point) (mark)))

    I’m not sure why you’d want copy-line and copy-n-lines. Default is for copy-line to copy one line, with arguments it copies n lines.

    You might find bookmarks more convenient than a (dot-emacs) function:

    C-x C-f ~/.emacs RET
    C-x r m dotemacs RET

    Then you can: C-x r b dotemacs RET to jump back to it from anywhere.

  2. 2 saterus

    Well, first and foremost, if you think I’m doing something silly and unnecessary, it’s probably because I don’t know any better yet. I’ve only been using Emacs for less than 2 months in my spare time. I welcome the advice on how to do things differently.

    Your copy line function looks a little more sophisticated than mine. I didn’t know how to use optional parameters in elisp. And like I said, I have the copy-n-lines function because it was all I was able to find online and I used it as a sample to write a default ‘copy-single-line’ function. There are few subtle differences between yours and mine though. You set yours to not include indentation, while mine simply goes from the beginning of the line to the end of the line. I’m not sure which is more useful. I’ll probably try yours out and see which I like more in practice.

    I like the bookmarks suggestion. I hadn’t come across those yet. They should come in really handy for going out and pulling up files buried within other directories.

    Thanks for the suggestions.


Leave a comment