I've successfully created a Tag file and could wade through the code using keys like M-,, but every time I need to choose the tag file for searching, so, I am looking for a way to hook the tag file I created with Emacs , found one reference, but not sure how to execute it on git-bash or how execute it on Emacs command-line, Helps appreciated.......


  • What do you mean by "hook"? That term has a specific meaning in Emacs, but I don't think you're using it that way. Can you explain in more detail what you're trying to achieve?
    – phils
    Commented Jan 2, 2020 at 21:38
  • Meaning: Attach the TAG with Emacs during the startup, so that, editor won't nag me "where is the TAG file???" when I am using ctags key.
    – RaGa__M
    Commented Jan 3, 2020 at 6:04
  • If you always want to load the same tag file, you need to execute elisp code at emacs startup to set tags-file-name. Do you already have customizations? Add code to ~/emacs.d/init.el like: (setq tags-file-name (expand-file-name “~/src/path-to-tags”))
    – InHarmsWay
    Commented Jan 3, 2020 at 12:47

1 Answer 1


M-, executes xref-pop-marker-stacker it just go back the position before you start code navigation.

M-. executes xref-find-definitions which does the code navigation thing.

So your question is not clear what exactly you want to achieve. M-, will always be successful since you already finished code navigation and the original start position is "marked".

You need only set global variable tag-file-name or tags-table-list once. And you will never be asked to set either of them again unless:

  • for some reason, you set the above flags as buffer local variable directly or indirectly. Besides, only one of the above two variables should be set. It's better to run C-h v tag-file-name or check tags-table-list before you running xref-find-definitions, make sure it's not empty.

  • The current file is binding a different xref-backend. Eval expression (xref-find-backend) and make sure it's etags before running xref-find-definition

I suggest you using counsel-etags for code navigation. It works out of box and has many improvements.

For example, it can use ctags to update tags file automatically, and ctags is running as background process which does block the Emacs UI interaction.

Here is function to update tags file,

(defun counsel-etags-async-shell-command (command tags-file)
  "Execute string COMMAND and create TAGS-FILE asynchronously."
  (let* ((proc (start-process "Shell" nil shell-file-name shell-command-switch command)))
     `(lambda (process signal)
        (let* ((status (process-status process)))
          (when (memq status '(exit signal))
             ((string= (substring signal 0 -1) "finished")
              (let* ((cmd (car (cdr (cdr (process-command process))))))
                (if counsel-etags-debug (message "`%s` executed." cmd))
                ;; reload tags-file
                (when (and ,tags-file (file-exists-p ,tags-file))
                  (run-hook-with-args 'counsel-etags-after-update-tags-hook ,tags-file)
                  (message "Tags file %s was created." ,tags-file))))
              (message "Failed to create tags file.\nerror=%s\ncommand=%s"

As you can see, the code is not simple. There are lots of technical know how. Since I've already provided a robust solution (Yes, I'm the developer), don't waste time to re-invent the wheel.

  • 1
    TBH, the question is how to get things fixed given a scenario X, and if I am reading it correctly, you answer is: get off of that scenario X.
    – RaGa__M
    Commented Jan 3, 2020 at 6:08
  • I'm not sure what Q you have answered here but it's not the original one which is simply how to get emacs to auto open the local tags file for a certains file/project.
    – RichieHH
    Commented Feb 3, 2020 at 11:43
  • @RichieHH, I did answer the question , see "You need only set global variable tag-file-name or tags-table-list once."
    – chen bin
    Commented Feb 3, 2020 at 13:07
  • Fair enough, but it's more than global. Many projects have their own tags files. Emacs shold switch based on project. Maybe projectile probably does? Another option, if supported, is to switch to lsp and have "live" tags and cross referencing.
    – RichieHH
    Commented Feb 3, 2020 at 14:28
  • Could use buffer local variable or directory local variable, see gnu.org/software/emacs/manual/html_node/elisp/… and gnu.org/software/emacs/manual/html_node/emacs/…
    – chen bin
    Commented Feb 4, 2020 at 2:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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