5

Brand new emacs user here.

I want to have the ability of opening a terminal with current file path from emacs, like what the open terminal here package does in sublime text 2.

By terminal, I mean a separate external terminal emulator running bash or zsh, like gnome-terminal, not the emulated shells inside emacs, like M-x shell M-x eshell, which I can't appreciate for now.

I googled but had nothing found... It seems that emacs guys really enjoy living in emacs.

  • Have you tried M-x ansi-term? It's much better than shell. I use it and it works really well. Or do you mean a new graphical terminal (like opening a gnome-terminal)? – PythonNut Jan 23 '15 at 5:37
  • @PythonNut I've just tried it, it even couldn't handle the tab completion... So I still prefer a new terminal window. Ahhh, I've just tried M-! gnome-terminal, it works. I'm wondering why M-! bash couldn't do it. – Ply_py Jan 23 '15 at 5:51
  • @Ply_py I think you need to tell terminal emulator and shell. bash is a shell, M-! bash surely won't do what you want. Please find out what terminal emulator you're using first. – kuanyui Jan 23 '15 at 6:41
  • @kuanyui Ahh, I just searched these two words... and found the answers here. Clearly I mixed up the notions of shell and terminal(emulator). Thanks, I'll edit the question. – Ply_py Jan 23 '15 at 8:10
  • shell and ansi-term definitely do handle tab completion, at least with bash. I use shell every day. – wdkrnls Jan 23 '15 at 14:43
6

Another try that disowns the process so your terminal will survive even after emacs is killed.

(defun run-gnome-terminal-here ()
  (interactive "@")
  (shell-command (concat "konsole --workdir"
            (file-name-directory (or load-file-name buffer-file-name)) 
              " > /dev/null 2>&1 & disown") nil nil))
  • Is there a way to tie the spawned terminal to the lifetime of the emacs process? – Gregory Nisbet Aug 12 '17 at 23:52
  • This doesn't work (emacs freezes until the terminal exits) if your terminal generates any debug messages when loading. To fix, swap the order of 2>&1 and >/dev/null (so that >/dev/null comes first). – Mark Aug 14 '17 at 13:06
  • When I try this, Emacs doesn't even let me call the function. After evaluation, the mini buffer says gnome-terminal-here but when entering gnome-terminal-here, it says [No match]. – UTF-8 Sep 17 '17 at 23:14
  • @UTF-8 That's because the function wasn't declared as an interactive command. See my edit. – Gilles Sep 18 '17 at 22:26
2

What terminal emulator are you using? Take KDE's Konsole as example, just write a function:

(defun open-konsole ()
  (interactive)
  (call-process "konsole" nil 0 nil "--workdir" default-directory))

The args from 5st place are konsole's argument. See your prefered terminal simulator's man page.

M-x open-konsole will open a new konsole process and use current default-directory (pwd in ELisp) as working directory.

  • One can open new tab if konsole is already running with (call-process "konsole" nil 0 nil "--new-tab" "--workdir" default-directory). Also (call-process "wmctrl" nil 0 nil "-a" "\" – Konsole\"") to activate its window. – Adobe Jan 23 '15 at 18:36
1

The other answers didn't work for me. This code does:

(defun open-gnome-terminal ()
  (interactive)
  (shell-command "gnome-terminal"))
0

Most of the time I use shell-mode. So I heavily use shell-here. But when I need external terminal. I use urxvt-client with tmux using this:

  • Create file named 'term-here' in /usr/local/bin/ containing
urxvtc -e bash -c "tmux -q has-session && exec tmux attach-session -d || exec tmux new-session -n$USER -s$USER@$HOSTNAME"
  • Create new function in emacs
(defun term-here ()
  (interactive)
  (start-process "" nil "term-here"))
  • Bind to your favorite key

This will open urxvt-client (with tmux) in your current directory. I bind it in dired-mode-map.

(use-package dired
  :ensure nil
  :ensure-system-package urxvt
  :bind ((:map dired-mode-map
           ("," . term-here))))

I choose urxvt-client because it is fast and simple. Don't forget to run your urxvt-daemon at startup.

0

You can use the external package terminal-here. This does only one thing, but does it well; exactly what you requested. And works for me on multiple OS.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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