reddit / emacs

Overwrite file name with company-files

cat-head · 2022-11-09 09:48
There is something slightly annoying and I was wondering whether anyone had found/knows a solution for it. If I'm using company-files for file name completion but I already had a file name written down, the completion simply inserts the new file name, instead of replacing the old one: * "./some-folder/**_**some-file.txt" * "./some-folder/my-file.txtsome-file.txt" It would be nice if company was smart enough to remove the trailing file name. Is there a simple solution here? Thanks! Edit: Ok, something like this might do it, but I may be missing edge cases ``` (defun kill-to-end () "kill to next \"" (interactive) (let* ((end (line-end-position))) (if (re-search-forward ".*?\\([\" ,\}]\\)" end) (replace-match (match-string 1) nil nil)) )) (defun company-files (command &optional arg &rest ignored) "`company-mode' completion backend existing file names. Completions works for proper absolute and relative files paths. File paths with spaces are only supported inside strings." (interactive (list 'interactive)) (cl-case command (interactive (company-begin-backend 'company-files)) (prefix (company-files--grab-existing-name)) (candidates (company-files--complete arg)) (location (cons (dired-noselect (file-name-directory (directory-file-name arg))) 1)) (post-completion (when (company-files--trailing-slash-p arg) (delete-char -1) ) (kill-to-end)) (sorted t) (no-cache t))) ```
yetanothermdmw · 2022-11-09 11:38
Does C-M-j (`ivy-immediate-done`) do what you want?
cat-head · 2022-11-09 12:11
I don't think so, this is about company completion when typing in a regular buffer
yetanothermdmw · 2022-11-09 13:24
Woops, I misread company as counsel. Sorry for the noise :)