So you want to open a terminal from Emacs.
While Emacs comes with various modes such as shell
, ansi-term
, and eshell
, sometimes you just want to open a directory in your trusty terminal application of choice.
The below code will open a terminal in your working directory, either using the key combination C-c t
or by using M-x open-terminal-in-workdir
.
You are going to have to change the “konsole –workdir “ part depending on what terminal application you are using. I am using Konsole.
Here are some replacements of “konsole –workdir “ for various terminal emulators:
App Name | Command |
---|---|
Mac OS X Terminal | “open -a Terminal “ |
GNOME Terminal | “gnome-terminal –working-directory=” |
XFCE Terminal | “xfce4-terminal –working-directory=” |
Windows CMD.exe | You’re on your own for this one! |
This is great so far, howevever I might be nested deep within a project and would like to open a terminal at the root of the project.
I got around this by creating a little if-else statement. Note: You need Projectile for this to work.
If you are inside a project, the terminal will open in its root. If you are editing some random files that are not part of “a project”, this function will fall back on Emacs’ default-directory
variable, which is your current working directory.
Hope this helps!
Leave a comment