0

I have just joined the Emacs church. I would like to know if the following can be done.

Say I have a project folder, I would like to run some custom commands that tie to some keyboard shorcuts for that folder only.

For example, say I am in /home/Documents/project, I would open this directory in Emacs and I would press, say C-x-F5 for example, and Emacs would send some custom commands that I declare and run in the shell.

Is it possible to do this? The only thing I manage to mess around is the compile command, M-x compile. But how to tell Emacs to only execute that while in /home/Documents/project?

Below is my attempt at modifying the compile command with adhook to compile C++ file.

(add-hook 'c++-mode-hook
          (lambda ()
            (set (make-local-variable 'compile-command)
                 (concat "g++ " buffer-file-name "&& ./a.out"))))

Please let me know.

  • Emacs has interactive buffers such as M-x shell, M-x eshell, M-x term. When you use the word "shell" in the context of your question, are you contemplating opening one of those interactive buffers? Or, do you just want to run the command as a background process and perhaps see the output -- without user interaction? – lawlist Aug 13 at 18:09
  • Yes, I know about M-x shell. I can do M-x shell and have my code on one side, and shell on the other. I can type commands straight to the shell. This question is just in case I am too lazy to type a long command in shell. I would like to press a key, Emacs would then send that long commands to a shell buffer, the process would run in the shell. I would watch that process running in the shell, and can still interact with my source code in the other buffer. – mle0312 Aug 13 at 18:14
  • this command would only run in a specified directory. Let's pick "ls" for example. So if press some keys in a certain directory, shell would open and do "ls". In other directory, the key combinations would do nothing. – mle0312 Aug 13 at 18:20
  • That can be done, but the question is how much setup you are willing to do and where that setup is done. E.g. you could define an association list in your init file that associates each "special" directory with a command. Or you could have a hidden tifle in each "special" directory that contains the command to be run. In either case, you would write a function to search (either the a-list for an entry or the directory for the hidden file) and if it found what it was looking for, it would execute the command it found (either in the a-list or in the file). Neither of these is hard to do, so ... – NickD Aug 13 at 20:06
  • ... it would depend on your personal preference of how you would like to do the setup. BTW, the two example implementations above are just that: examples. If you like neither, then you have to decide how you would do it and edit your question to add that information. – NickD Aug 13 at 20:09
0

The O.P. may wish to set up some directory local variables as described in the Emacs manual:

https://www.gnu.org/software/emacs/manual/html_node/elisp/Directory-Local-Variables.html

As set forth in the manual (supra), "...Emacs uses these to create buffer-local bindings for those variables in buffers visiting any file in that directory." The following link to one of the answers in a semi-related thread describes how to bind functions to buffer-local variables:

How to bind keys to a function which can be defined per-mode?

If a *shell* buffer is already open, a user can call (comint-send-string "*shell*" "COMMAND\n") where the first argument is the name of the running process; or, a user can omit the \n and then follow comint-send-string with comint-send-input.

[A user may choose to have a default function bound to a global variable (such as 'ignore or a message that says "Go fish...") and do something specific if the variable is locally bound. E.g., testing with the function local-variable-p ....]

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.