Is there a way to configure Emacs to activate lsp-mode for some projects and ggtags for other projects? Maybe using .dir-locals.el?

I code C++ and the project I most often work on doesn't support lsp-mode (I'm unable to create a working compile_commands.json because of unusual project structure). In this project I use ggtags. But when I work on other C++-projects I would like to use lsp-mode instead. But all configurations I have seen all load lsp-mode or ggtags based on prog-mode (c++-mode etc) and not on specific project.

1 Answer 1


Yes, you want to use .dir-locals.el and assuming the enable/disable commands you need are functions, its pseudo-variable eval. If you have lsp-mode globally enabled, add something like this to the .dir-locals.el file in the root of your "odd" project:

;;; Directory Local Variables
;;; For more information see (info "(emacs) Directory Variables")

;;; disable lsp-mode and enable ggtags-mode
((nil . ((eval . (lsp-mode -1))
         (eval . (ggtags-mode 1)))

(Note, I don't use either mode, so be sure you use the correct functions to disable lsp-mode and enable ggtags-mode.)

@phils has a good answer here on formatting the .dir-locals.el file.

  • You can use the command add-dir-local-variable to do this, but I never seem to get it right.
    – nega
    Commented Aug 8, 2019 at 19:51

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.