1

I need to copy part of the file (with re-search-forward) to another file under the marker. I know how to use re-search-forward, but I have problems with buffer.

Am i right with my logic?

  • 1) open needed file with temp buffer
  • 2) create new buffer
  • 3) find needed lines with re-search-forward and copy them in this new buffer
  • 4) insert lines into opened file with insert-buffer-substring

Right now i have a function like this:

(defun my-insert-file-name (filename)
  (interactive "*fInsert file name: ")
  (save-excursion
    (goto-char (point-min))
    (when (re-search-forward "#here")
      (forward-line 1)
      (------something should be here-----)))
  • 1
    Are you trying to insert text from this other file in to the current buffer? Or are you trying to insert text from the current buffer in to another file? You might be able to use append-to-buffer or append-to-file but I'm not entirely clear on your requirements here. – glucas May 16 '17 at 19:54
  • Yes, i want to insert text from other file to current buffer – Ilya May 16 '17 at 21:58
2

If I've understood the question properly, here's a starting point:

(defun my-insert-file-name (filename)
  (interactive "*fInsert file name: ")
  (let ((buf (current-buffer)))
    (save-excursion
      (with-temp-buffer
        (insert-file-contents filename)
        (goto-char 1)
        (re-search-forward "regexp")
        (append-to-buffer buf (point) (point-max))))))

What this is doing:

  • Set buf to the current buffer.
  • Insert the contents of filename in to a new temporary buffer.
  • Go to the top of the temporary buffer and search forward for "regexp".
  • Insert the contents of the temporary buffer from the line after the match to the end of the file back in to buf.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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