0

This is a specific use case and I understand that not a lot of folks will have use (or prefer) this. But I'd still like to understand whether this is possible.

While editing an org file, I want to open a temp file in my vim (with my plugins as I would from a terminal) and by the end of the edit, on close, I want to get the contents of the file into the org file at the cursor. Deleting the temp file would be a plus but not really needed. Maybe I can reuse the same file again.

This is what I have tried:

(defun vim-insert-here ()
  (interactive)
(shell-command
 (format "gvim temp"))
(insert-file-contents "temp"))

This works well enough for simply copying the contents. But I need help adding functionality such as:

  • Not inserting the vim file text when vim is exited with q!
  • Selecting an existing piece of text in the org file and opening that in vim, and the resulting changes are overwritten in the org file
1
  • I don't think you can do anything about the q! case: AFAICT, vim exits with status 0, so there is no way for emacs to figure out if vim exited "normally" or with q!.
    – NickD
    Dec 9, 2020 at 2:22

1 Answer 1

1

suspend-emacs works for me using Emacs inside a terminal (it does not work for GUI Emacs on macOS), e.g., here a simple example

(defun foo ()
  (interactive)
  (suspend-emacs "vim /tmp/x; fg")
  (insert-file-contents "/tmp/x"))

the following tries to do what yout want, (1. use the contents of the region as initial text; 2. replace the region with new text; 3. delete the temp file)

(defun bar (beg end)
  (interactive "*r")
  (let ((tmpfile (make-temp-file "")))
    (write-region beg end tmpfile)
    (suspend-emacs (format "vim %s; fg" (shell-quote-argument tmpfile)))
    (goto-char beg)
    (delete-region beg end)
    (insert-file-contents tmpfile)
    (delete-file tmpfile)))

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.