July 5, 2011

Move line to end of file and comment

Posted in Emacs, Java at 15:40 by theBlackDragon

I had to work on a bunch of translation Java property files in an ancient J2EE project that had old translations in between the new ones resulting in a hardly readable mess that confused the hell out of Netbeans’ property file editor plugin, so I decided to just move all those old lines to the bottom of the file and comment them out.
For this I quickly hacked up two elisp functions

(defun move-line-to-end-of-file ()
  "Move the current line to the end of the file."
  (interactive)
  (save-excursion
    (beginning-of-line)
    (kill-whole-line)
    (end-of-buffer)
    (yank-as-comment)))

(defun yank-as-comment ()
  "Yank the last killed selection as a comment, but leave text 
  alone that is already a comment according to the current mode."
  (interactive)
  (save-excursion
    (yank)
    (if (not (comment-only-p (mark) (point)))
        (comment-region (mark) (point)))))

There probably is a much more idiomatic way to do this (which I’d love to hear about), but this seems to work just fine for my use. Note however that move-line-to-end-of-file will just append to the last line if the file doesn’t end with a newline (which isn’t an issue in my case).

Advertisements