|
| 1 | +# Configuring Emacs TRAMP |
| 2 | + |
| 3 | +[Emacs TRAMP](https://www.emacswiki.org/emacs/TrampMode) is a method of running editing operations on a remote server. |
| 4 | + |
| 5 | +## Connecting To A Workspace |
| 6 | + |
| 7 | +To connect to your workspace first run: |
| 8 | + |
| 9 | +``` |
| 10 | +coder config-ssh |
| 11 | +``` |
| 12 | + |
| 13 | +Then you can connect to your workspace by its name in the format: `coder.<WORKSPACE NAME>`. |
| 14 | + |
| 15 | +In Emacs type `C-x d` and then input: `/-:coder.<WORKSPACE NAME>:` and hit enter. This will open up Dired on the workspace's home directory. |
| 16 | + |
| 17 | +### Using SSH |
| 18 | + |
| 19 | +By default Emacs TRAMP is setup to use SCP to access files on the Coder workspace instance. However you might want to use SSH if you have a jumpbox or some other complex network setup. |
| 20 | + |
| 21 | +To do so set the following in your Emacs `init.el` file: |
| 22 | + |
| 23 | +```lisp |
| 24 | +(setq tramp-default-method "ssh") |
| 25 | +``` |
| 26 | + |
| 27 | +Then when you access the workspace instance via `/-:coder.<WORKSPACE NAME>` Emacs will use SSH. Setting `tramp-default-method` will also tell `ansi-term` mode the correct way to access the remote when directory tracking. |
| 28 | + |
| 29 | +## Directory Tracking |
| 30 | + |
| 31 | +### ansi-term |
| 32 | + |
| 33 | +If you run your terminal in Emacs via `ansi-term` then you might run into a problem where while SSH-ed into a workspace Emacs will not change its `default-directory` to open files in the directory your shell is in. |
| 34 | + |
| 35 | +To fix this: |
| 36 | + |
| 37 | +1. In your workspace Terraform template be sure to add the following: |
| 38 | + |
| 39 | + ```hcl |
| 40 | + data "coder_workspace" "me" { |
| 41 | + } |
| 42 | +
|
| 43 | + resource "coder_agent" "main" { |
| 44 | + # ... |
| 45 | + env { |
| 46 | + name = "CODER_WORKSPACE_NAME" |
| 47 | + value = data.coder_workspace.me.name |
| 48 | + } |
| 49 | + } |
| 50 | + ``` |
| 51 | + |
| 52 | +2. Next in the shell profile file on the workspace (ex., `~/.bashrc` for Bash and `~/.zshrc` for Zsh) add the following: |
| 53 | + |
| 54 | + ```bash |
| 55 | + ansi_term_announce_host() { |
| 56 | + printf '\033AnSiTh %s\n' "coder.$CODER_WORKSPACE_NAME" |
| 57 | + } |
| 58 | + |
| 59 | + ansi_term_announce_user() { |
| 60 | + printf '\033AnSiTu %s\n' "$USER" |
| 61 | + } |
| 62 | + |
| 63 | + ansi_term_announce_pwd() { |
| 64 | + printf '\033AnSiTc %s\n' "$PWD" |
| 65 | + } |
| 66 | + |
| 67 | + ansi_term_announce() { |
| 68 | + ansi_term_announce_host |
| 69 | + ansi_term_announce_user |
| 70 | + ansi_term_announce_pwd |
| 71 | + } |
| 72 | + |
| 73 | + cd() { command cd "$@"; ansi_term_announce_pwd; } |
| 74 | + pushd() { command pushd "$@"; ansi_term_announce_pwd; } |
| 75 | + popd() { command popd "$@"; ansi_term_announce_pwd; } |
| 76 | + |
| 77 | + ansi_term_announce |
| 78 | + ``` |
| 79 | + |
| 80 | + Ansi Term expects the terminal running inside of it to send escape codes to inform Emacs of the hostname, user, and working directory. The above code sends these escape codes and associated data whenever the terminal logs in and whenever the directory changes. |
| 81 | + |
| 82 | +### eshell |
| 83 | + |
| 84 | +The `eshell` mode will perform directory tracking by default, no additional configuration is needed. |
| 85 | + |
| 86 | +## Language Servers (Code Completion) |
| 87 | + |
| 88 | +If you use [`lsp-mode`](https://emacs-lsp.github.io/lsp-mode) for code intelligence and completion some additional configuration is required. |
| 89 | + |
| 90 | +In your Emacs `init.el` file you must register a LSP client and tell `lsp-mode` how to find it on the remote machine using the `lsp-register-client` function. For each LSP server you want to use in your workspace add the following: |
| 91 | + |
| 92 | +```lisp |
| 93 | +(lsp-register-client (make-lsp-client :new-connection (lsp-tramp-connection "<LSP SERVER BINARY>") |
| 94 | + :major-modes '(<LANGUAGE MODE>) |
| 95 | + :remote? t |
| 96 | + :server-id '<LANGUAGE SERVER ID>)) |
| 97 | +``` |
| 98 | + |
| 99 | +This tells `lsp-mode` to look for a language server binary named `<LSP SERVER BINARY>` for use in `<LANGUAGE MODE>` on a machine named `coder.<WORKSPACE NAME>`. Be sure to replace the values between angle brackets: |
| 100 | + |
| 101 | +- `<LSP SERVER BINARY>` : The name of the language server binary, without any path components. For example to use the Deno Javascript language server use the value `deno`. |
| 102 | +- `<LANGUAGE MODE>`: The name of the Emacs major mode for which the language server should be used. For example to enable the language server for Javascript development use the value `web-mode`. |
| 103 | +- `<LANGUAGE SERVER ID>`: This is just the name that `lsp-mode` will use to refer to this language server. If you are ever looking for output buffers or files they may have this name in them. |
| 104 | + |
| 105 | +Calling the `lsp-register-client` function will tell `lsp-mode` the name of the LSP server binary. However this binary must be accessible via the path. If the language server binary is not in the path you must modify `tramp-remote-path` so that `lsp-mode` knows in what directories to look for the LSP server. To do this use TRAMP's connection profiles functionality. These connection profiles let you customize variables depending on what machine you are connected to. Add the following to your `init.el`: |
| 106 | + |
| 107 | +```lisp |
| 108 | +(connection-local-set-profile-variables 'remote-path-lsp-servers |
| 109 | + '((tramp-remote-path . ("<PATH TO ADD>" tramp-default-remote-path)))) |
| 110 | +(connection-local-set-profiles '(:machine "coder.<WORKSPACE NAME>") 'remote-path-lsp-servers) |
| 111 | +``` |
| 112 | + |
| 113 | +The `connection-local-set-profile-variables` function creates a new connection profile by the name `remote-path-lsp-servers`. The `connection-local-set-profiles` then indicates this `remote-path-lsp-servers` connection profile should be used when connecting to a server named `coder.<WORKSPACE NAME>`. Be sure to replace `<PATH TO ADD>` with the directory in which a LSP server is present. |
| 114 | + |
| 115 | +TRAMP and `lsp-mode` are fickle friends, sometimes there is weird behavior. If you find that language servers are hanging in the `starting` state then [it might be helpful](https://github.com/emacs-lsp/lsp-mode/issues/2709#issuecomment-800868919) to set the `lsp-log-io` variable to `t`. |
| 116 | + |
| 117 | +More details on configuring `lsp-mode` for TRAMP can be found [in the `lsp-mode` documentation](https://emacs-lsp.github.io/lsp-mode/page/remote/). |
| 118 | +The [TRAMP `tramp-remote-path` documentation](https://www.gnu.org/software/emacs/manual/html_node/tramp/Remote-programs.html#Remote-programs) contains more examples and details of connection profiles. |
0 commit comments