Skip to content
Tutorials Center
My learning

Topics

Lesson 29 of 38 ยท Version Control and Tools

Dired and TRAMP: Managing Local and Remote Files in Emacs

Use Dired to browse, mark, copy, rename and bulk-edit files, and TRAMP to open remote files over SSH or edit as root, with the exact keys and file name syntax.

Emacs includes a file manager called Dired ("directory editor") and a transparent remote-file system called TRAMP. Together they replace a file explorer and half of what most people use an SSH terminal for. Dired shows a directory as an ordinary text buffer, so everything you know about moving and searching in Emacs applies. TRAMP lets any Emacs command work on a file that lives on another machine.

Keys and file name syntax below are the real ones. The runnable blocks are JavaScript stand-ins that parse TRAMP-style names and simulate Dired's mark and delete cycle.

Opening Dired

  • C-x d (dired) asks for a directory; C-x C-f on a directory name also opens Dired.
  • C-x C-j (dired-jump) opens Dired on the directory of the file you are visiting, with the cursor on that file.
  • ^ goes up to the parent directory; RET or f opens the file or directory at point; o opens it in another window; v views it read-only.
  • g refreshes, s toggles sorting by date or name, and C-u s lets you edit the ls switches (for example -alh). Set defaults with (setq dired-listing-switches "-alh").
  • ( toggles hiding details so you only see names; dired-hide-details-mode can be added to dired-mode-hook.

Marks and bulk operations

Dired works in two steps: mark files, then run one command on all marked files. If nothing is marked, commands act on the file at point.

Key Action
m / u Mark / unmark the file at point (marks show as *)
U Unmark all
t Toggle marks (invert)
% m Mark files whose names match a regexp
* . Mark by extension
* / Mark all directories
d Flag for deletion (shows D); x executes the flagged deletions
% d Flag by regexp for deletion
D Delete marked files immediately (asks first)
C Copy; R rename or move
+ Create a directory
Z Compress or uncompress
! or & Run a shell command on marked files (& async)
Q Query-replace in marked files
A Search marked files with a regexp
M, O, G Change mode, owner, group

The separation between flagging (d) and executing (x) is a safety feature: you review the list of D lines before anything is deleted.

JS Stand-in: mark, flag and execute

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Editing a directory as text: wdired

Press C-x C-q (dired-toggle-read-only, or M-x wdired-change-to-wdired-mode) and the Dired buffer becomes editable. Rename files by editing their names with all your usual commands, multiple cursors included (query-replace, rectangles, macros). Commit with C-c C-c, discard with C-c ESC. Batch-renaming 200 files this way is a favourite Emacs trick. Related: M-x find-name-dired and find-grep-dired produce a Dired buffer of search results; dired-do-find-regexp searches marked files.

Two other tips: enable (setq dired-dwim-target t) so that with two Dired windows open, C and R default to the other window's directory; and add (add-hook 'dired-mode-hook #'dired-omit-mode) to hide backup and dot files (toggle with C-x M-o).

TRAMP: remote files

TRAMP ("Transparent Remote Access, Multiple Protocol") is part of Emacs. You open a remote file by typing a special file name in C-x C-f:

Text
/ssh:user@host:/path/to/file
/ssh:user@host#2222:/var/www/index.html     # non-default port
/scp:user@host:/tmp/data.csv                # copy via scp
/sudo::/etc/hosts                           # local file as root
/ssh:user@host|sudo:host:/etc/nginx/nginx.conf   # remote file as root (multi-hop)
/docker:mycontainer:/app/config.yml         # inside a Docker container

The general shape is /METHOD:USER@HOST:/PATH. After opening a remote file you edit as usual; saving writes back over the connection. Dired works too: C-x C-f /ssh:host:/var/log/ RET opens a remote directory. Even M-x shell, M-x compile and M-x grep run on the remote machine when the current directory is remote (default-directory is remote), and Magit can drive a remote repository.

Configure the usual host details in ~/.ssh/config and TRAMP uses them, including keys and ProxyJump. Practical settings:

ELISP
(setq tramp-default-method "ssh")
(setq remote-file-name-inhibit-cache nil)      ; faster, but may show stale listings
(setq tramp-verbose 1)                          ; raise to 6 when debugging

Some tips: use M-x tramp-cleanup-all-connections if a connection hangs, avoid heavy modes such as fancy modelines or projectile indexing on remote files, and keep remote shell prompts simple (TRAMP must recognize them).

JS Stand-in: parse a TRAMP file name

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. In Dired, what is the difference between d and x?

    Flagging first lets you review the D lines before x removes them.
  2. 2. Which keys let you rename many files by editing their names as text?

    wdired makes the Dired buffer editable; C-c C-c applies the renames.
  3. 3. What does opening /sudo::/etc/hosts do?

    The sudo method edits a local file as root; saving asks for your sudo password.
  4. 4. How do you open a remote file over SSH on port 2222?

    TRAMP file names use #PORT after the host: /ssh:user@host#2222:/path.

Summary

  • C-x d and C-x C-j open Dired; m, d, x, C, R, + mark, flag, execute, copy, rename and create.
  • % m marks by regexp, ! runs a shell command on marked files, C-x C-q edits names as text.
  • TRAMP names look like /METHOD:USER@HOST:/PATH; /sudo:: edits as root.
  • Dired, shells, compile and Magit all work on remote directories.
  • dired-dwim-target makes copy and move default to the other Dired window.

Next: Shells, Compilation and Searching