Wednesday, August 21, 2013

A tiny eshell add-on: jump to shell ready to take input

So I'm finally grokking eshell and one feature I want is the following:

Suppose I'm looking at a buffer containing index.html which lives in ~/projects/hacking. I'd like to effortlessly switch to eshell and have it jump to this directory, so I'm ready to start entering commands.

If I had this feature in place, I could go from a file's buffer to a shell quickly and without futzing with changing directories. Using this example I was able to quickly write up a command that does exactly what the above description suggests:

;; Inspired by: http://www.emacswiki.org/emacs/EshellControlFromOtherBuffer
(defun bs-eshell-switch-to-and-change-dir ()
  "Switch to eshell and make sure we're in the directory the current buffer is in."
  (interactive)
  (let ((dir default-directory))
    (let ((b (get-buffer eshell-buffer-name)))
      (unless b
        (eshell)))
    (display-buffer eshell-buffer-name t)
    (switch-to-buffer-other-window eshell-buffer-name)
    (end-of-buffer)
    (unless (equalp dir default-directory)
      (cd dir)
      (eshell-send-input)
      (end-of-buffer))))

(global-set-key (kbd "\C-c \C-s") 'bs-eshell-switch-to-and-change-dir)

From any buffer I can hit Control-c Control-s and bam! I'm now at a shell prompt ready type away.

Update: One of the comments below suggested shell-pop-el as a pre-existing implementation of the above concept. I'd definitely recommend using it over what I've got above.

Update: And try: shell-switcher, too!

6 comments:

  1. Nice!

    I have a similar use case, but maybe more minimal(maybe it's also your case), usually, I want to run some command in the file I'm currently editing.

    That's why I wrote this tiny elisp that replaces % for the current file:

    http://puntoblogspot.blogspot.com.es/2013/04/running-shell-command-on-current-file.html

    ReplyDelete
  2. > I have a similar use case, but maybe more minimal(maybe it's also your case), usually, I want to run some command in the file I'm currently editing.

    Oooh, that's a handy one, too. I'll have to look into using it.

    Thanks!

    -Ben

    ReplyDelete
  3. Anonymous2:34 PM

    There's aready a package that does this and more: https://github.com/kyagi/shell-pop-el

    It's a nice practice to try to search before reimplementing functionality :) (it saves time and millions of "forks" of same idea). Remember: if you think something is a good idea, someone probably already did it for emacs :P

    -Fuco

    ReplyDelete
  4. > There's aready a package that does this and more: https://github.com/kyagi/shell-pop-el

    Sweet, thanks for sharing.

    > It's a nice practice to try to search before reimplementing functionality :)

    It's also a nice practice to implement little bits of functionality to learn something. :)

    > Remember: if you think something is a good idea, someone probably already did it for emacs :P

    So true.

    You should add this to the eshell category page on the emacswiki. That's where I originally searched around for the functionality I had in mind.

    ReplyDelete
  5. Anonymous2:51 AM

    I implemented the same idea with more features in shell-switcher. Check the screencast at http://www.youtube.com/watch?v=jNSrrQwcCr4 and the code at https://github.com/DamienCassou/shell-switcher.

    ReplyDelete
  6. Thanks Damien! I'll have to check that package out.

    ReplyDelete