Skip to content
TheChatArchiveThe Chat Archive
Qwenqwen3-235b-a22b-no-thinking· Alibaba (Qwen)Hugging Face LMArena Human Preference 140K

How can I write a function or keybind in emacs which simply runs the function below to…

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with DeepSeekdeepseek-v3-0324’s answer.
  1. User

    How can I write a function or keybind in emacs which simply runs the function below to get the full file path?

    (defun easy-kill-on-buffer-file-name (n) "Get buffer-file-name' or default-directory'. If N is zero, remove the directory part; -, remove the file name part; +, full path." (if (easy-kill-get mark) (easy-kill-echo "Not supported in easy-mark'") (pcase (or buffer-file-name default-directory) (nil (easy-kill-echo "No buffer-file-name'")) (file (let* ((file (directory-file-name file)) (text (pcase n (- (file-name-directory file)) (`0 (file-name-nondirectory file)) (_ file)))) (easy-kill-adjust-candidate 'buffer-file-name text))))))

  2. Qwen Assistant

    You're looking to create a function (and possibly a keybinding) in Emacs that uses or wraps the existing easy-kill-on-buffer-file-name function — specifically, to get and copy the full file path of the current buffer.

    From your code:

    • The function easy-kill-on-buffer-file-name takes one argument n, which controls what part of the file path is returned:
      • 0: just the filename
      • -: just the directory
      • anything else (e.g. + or default): full path

    So, to copy the full file path, you want to call this function with an argument that causes it to return the full path — i.e., not 0 or -. Looking at the function definition, any value other than 0 or - will do that. A common convention is to use + or 1.


    ✅ Step 1: Create a wrapper function

    We can define a simple wrapper that calls easy-kill-on-buffer-file-name with + as the argument (or any non-zero, non-minus symbol):

    (defun my/copy-full-file-path ()
      "Copy the full file path of the current buffer using `easy-kill-on-buffer-file-name'."
      (interactive)
      (easy-kill-on-buffer-file-name '+))
    

    This uses '+' as the argument to trigger the full path behavior.


    ✅ Step 2: Bind it to a key (optional)

    Now, if you'd like to bind it to a convenient key combination, for example C-c C-f, you could add:

    (global-set-key (kbd "C-c C-f") #'my/copy-full-file-path)
    

    Or bind it locally in some major modes only, depending on your preference.


    📝 Notes

    • This assumes easy-kill-on-buffer-file-name is already defined and available in your environment (e.g., from the easy-kill package).
    • You can install easy-kill via M-x package-install RET easy-kill RET if needed.
    • The copied text will be added to the kill ring, so you can paste it with C-y.

    ✅ Summary

    Add the following to your Emacs config:

    (defun my/copy-full-file-path ()
      "Copy the full file path of the current buffer."
      (interactive)
      (easy-kill-on-buffer-file-name '+))
    
    (global-set-key (kbd "C-c C-f") #'my/copy-full-file-path)
    

    Now pressing C-c C-f will copy the full path of the current buffer’s file.

  3. User

    Wrong type argument: overlayp, nil

  4. Qwen Assistant

    The error:

    Wrong type argument: overlayp, nil
    

    means that somewhere in the code, Emacs expected an overlay, but instead got nil.

    This typically happens if a function tries to manipulate an overlay (e.g., delete or modify it), but the overlay doesn't exist or was already deleted.


    🔍 Root Cause

    Looking at your original function easy-kill-on-buffer-file-name, and the error you're seeing, this likely comes from the call to:

    (easy-kill-echo "Not supported in `easy-mark'")
    

    or another part of the easy-kill package that assumes some internal state (like overlays) exists when they actually don't.

    In particular, easy-kill-echo might be trying to interact with an overlay that hasn't been created yet — hence the nil where an overlay is expected.


    ✅ Solution: Wrap Safely

    To avoid this error, we should create a new, standalone function that simply copies the file path without relying on any internal easy-kill logic that may assume overlays or other state.

    Here’s a safer version that does exactly what you want — copies the full file path to the kill ring:


    ✅ Recommended Function (Safe & Reliable)

    (defun my/copy-full-file-path ()
      "Copy the full file path of the current buffer to the kill ring."
      (interactive)
      (let ((file-path (or buffer-file-name default-directory)))
        (if file-path
            (progn
              (kill-new file-path)
              (message "Copied: %s" file-path))
          (user-error "No file associated with this buffer"))))
    

    📝 Explanation

    • (or buffer-file-name default-directory) gets the file path.
    • If there's no file associated (file-path is nil), it shows an error message.
    • Otherwise, it adds the path to the kill ring using kill-new.
    • It also displays a confirmation in the echo area.

    ✅ Optional Keybinding

    Add this to bind to C-c C-f:

    (global-set-key (kbd "C-c C-f") #'my/copy-full-file-path)
    

    ✅ Summary

    Use this clean and safe function instead of wrapping easy-kill-on-buffer-file-name. It avoids the overlayp error entirely and does exactly what you need: copy the full file path.

    Let me know if you'd like variations (e.g., copy only filename, directory, etc.)!

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.