Search code examples
regexemacselisptrampprojectile

Need help regex matching a buffer name and switching to it


I write code on one system (x86) and ssh into another (ARM) to compile my code. I use tramp to ssh from within emacs. Projectile command (projectile-project-root) give this info:

/home/userX/projectY

The buffer name after sshing into remote system and be under same project is:

/ssh:userX@sxy-ss-02:/home/userX/projectY

What is a lisp code that would:

  1. Figure out the project name after calling (projectile-project-root)
  2. Matching on the buffer that has the remote login and ends with project name
  3. switch to that buffer.

Basically, matching (from the list of opened buffer) on /ssh:....../projectY


Solution

  • Untested - add this to your init file:

     (setq remote-user "userX")
     (setq remote-host "sxy-ss-02")
    
     (defun switch-buffer-to-remote-project ()
       (interactive)
       (let ((tramp-name (format "/ssh:%s@%s:%s" remote-user remote-host (projectile-project-root))))
          (find-file tramp-name)))
    
     (global-set-key (kbd "C-c C-Z") #'switch-buffer-to-remote-project)
    

    It binds the command to C-c C-z which was undefined in my case, but you might want to choose a different keybinding for it.