Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically "cd" into directory from default-directory #9

Open
matthewbauer opened this issue Mar 4, 2018 · 2 comments
Open

Automatically "cd" into directory from default-directory #9

matthewbauer opened this issue Mar 4, 2018 · 2 comments

Comments

@matthewbauer
Copy link

This would be helpful to me. Basically, when 'tramp-term' has no arguments, load up what's in default-directory, parsing host, username and directory using tramp.

@apauzies
Copy link
Contributor

You could do something like that:

(add-hook 'tramp-term-after-initialized-hook
(lambda (host)
(term-send-raw-string (concat "cd " default-directory (kbd "RET")))))

@bdk0
Copy link

bdk0 commented Sep 3, 2021

The following function will do this,and a bit more: This will look at the current buffer's working directory and open a terminal pointing at that directory. If the buffer is local, it just opens an ansi-term. If its a TRAMP buffer, it opens a tramp-term to that host, and then 'cd's to the same working directory.

Just call my-tramp-term-here. It will work with local buffers and tramp/ssh buffers, but not other tramp transports (I don't think tramp-term itself supports those either). It also only works with a single hop.

I can work this up into a pull request to add it to tramp-term itself if desired. I'm very new to elisp, so this might not be the smartest implementation but it seems to work.

(defun my-tramp-term--cd-hook(host)
  (term-send-raw-string (concat "cd " (tramp-file-name-localname ts) ";clear" (kbd "RET")))
)

(defun my-tramp-term-here()
    (interactive)
    (if (tramp-tramp-file-p default-directory)
         (let ((ts (tramp-dissect-file-name default-directory)))
           (if (string= (tramp-file-name-method ts) "ssh")
               (progn
                 (add-hook 'tramp-term-after-initialized-hook 'my-tramp-term--cd-hook)
                 (if (tramp-file-name-user ts)
                     (tramp-term (list (tramp-file-name-user ts) (tramp-file-name-host ts)))
                   (tramp-term (list (tramp-file-name-host ts)))
                   )
                 (remove-hook 'tramp-term-after-initialized-hook 'my-tramp-term--cd-hook)
                 )
             (message "tramp-term-here: Unsupported Tramp method. Only ssh supported"))
           )
      (ansi-term (or explicit-shell-file-name "bash") "localhost")
      )
    )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants