在Emacs中将文本文件切割成多个部分

时间:2010-12-21 07:40:07

标签: emacs elisp

我正在使用GNU Emacs 23编辑器。我有这个巨大的文本文件,包含大约10,000行,我想切成多个文件。使用鼠标选择要粘贴到另一个文件中的所需文本真的很痛苦。此外,这也容易出错。

如果我想根据行号将文本文件分成4个文件在哪里 第一档:1-2500行 第二档:第2500-5000行 第三档:第5000-7500行 第四档:行:7500-10000

我该怎么做?至少,是否有任何有效的方法来复制文件的大区域只需指定行号

3 个答案:

答案 0 :(得分:3)

以下内容应该有效:

M-<           (go to start of file)
C-space       (set mark at current cursor position)
ESC 2500 down (go down 2500 lines)
C-w           (delete between mark and cursor, and put it in cut buffer)
...           (open new file, using your favourite method)
C-Y           (paste contents of cut buffer)

答案 1 :(得分:3)

C-u是你的朋友。试试这个:

C-spc (set mark at point)
C-u 2500 <down> (equivalent to pressing the down key 2500 times)
M-w (copy)

答案 2 :(得分:1)

我使用此命令来自misc-cmds.elhttp://www.emacswiki.org/emacs/misc-cmds.el)。您只需选择所需的文本,然后将其复制到所需的文件 - 请参阅文档字符串。


    (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.")))))