1

Problem: I'm trying to write a command that takes the selected text and appends it the end of a file of my choice.

There also needs to be a tab completion (or autocompletion) element to the command, in the sense that the command should be able to autocomplete to files that are nested somewhere within the directory /tab/completion/directory.

Example: Suppose there is a file /tab/completion/directory/nested/file.md, and I select text in my buffer and run M-x append-to-file, then I should be able to M-x append-to-file file.md<TAB> for it to autocomplete to M-x /tab/completion/directory/nested/file.md. After running this command, the selected text should be sent to end of the file.

1

I don't know if I understand your question, append-to-file already appends the selected region to a file you specify:

append-to-file is an interactive compiled Lisp function in ‘files.el’.

(append-to-file START END FILENAME)

Append the contents of the region to the end of file FILENAME.

As for completion, I recommend you strongly consider the ivy/swiper package.

https://github.com/abo-abo/swiper

http://oremacs.com/swiper/

If you are trying to find the file without knowing the path then you probably want to look at projectile-find-file. Then write a function that calls append-to-file and projectile to fill in the filename.

  • I already have projectile and helm. I'm totally new to elisp. Can you demonstrate (or at least sketch) how to call append-to-file and projectile to fill in the filename? – George Jul 10 '17 at 13:18
1

If you are using ido see introduction here, then all code you have to write is this:

(defun my-append-to-file (file)
    (interactive "Ffilename")
    (write-region (mark) (point) file 'append))

You could also use M-x write-region directly.

  • This works to append text to the end of a file, but it doesn't seem to have autocomplete features for nested filenames. – George Jul 10 '17 at 13:20
  • The autocompletion is done by ido. If you use ido you have autocompletion on every operation in minibuffer, which uses filenames or buffernames. This is really nice. Please have a look at the Introduction link I provided in my answer. – jue Jul 10 '17 at 14:16

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.