3

I find myself refactoring code fairly frequently and my screen space is at a premium. I'd like to be able to move to a particular scope (class, function, or even if-statement/for-loop blocks) and either cut the code and append it to another file or copy the code and append it to another file. Ideally, I'd like to do this in Fortran, C and C++.

I know I can open multiple windows and stack them and use the usual kill on lines or marked regions then change to the other window and yank the lines or regions into that buffer. But I don't want to have multiple windows open, it makes it harder to see where I am in the file from which I'm moving code. Plus, code from one file may be going to multiple places. So I'd like to just dump it at the end of a a different file and then move it around in that file later.

How can I move/copy the code within the scope of the cursor to the end of another file for those languages? If there isn't a method that would work in each language, is there a way to do it on marked regions of code and single lines?

1
  • I'm not really sure what tags are best for this, feel free to edit or suggest alternatives!
    – tpg2114
    Commented Oct 29, 2014 at 19:38

2 Answers 2

3

Select the code you want to copy or move, then use M-x append-to-file. To remove the selected code from the current buffer, use C-w.


In library misc-cmds.el I define command region-to-file as follows. (I bind it to C-S-backspace and to C-x M-f.)

(defun region-to-file (start end filename arg)
  "With prefix arg, this is `append-to-file'.  Without, it is `write-region'.
START and END are the region boundaries.
Prefix ARG non-nil means append region to end of file FILENAME.
Prefix ARG nil means write region to FILENAME, replacing contents."
  (interactive
   (list (region-beginning)
         (region-end)
         (read-file-name (concat (if current-prefix-arg "Append" "Write")
                                 " region to file: "))
         current-prefix-arg))
  (let* ((curr-file    (buffer-file-name))
         (same-file-p  (and curr-file  (string= curr-file filename))))
    (cond ((or (not same-file-p)
               (progn (when (fboundp 'flash-ding) (flash-ding))
                      (yes-or-no-p
                       (format
                        "Do you really want to REPLACE the contents of `%s' by \
just the REGION? "
                        (file-name-nondirectory curr-file)))))
           (write-region start end filename arg)
           (when same-file-p (revert-buffer t t)))
          (t (message "OK.  Not written.")))))
1

mark-defun is bound to C-M-h and will mark the current function. Different modes use the same keybinding (c-mark-function for example).

append-to-file will prompt for a file name and append the current region to that file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.