5.8 Renaming remote files

Sometimes, it is desirable to safe file contents of buffers visiting a given remote host. This could happen for example, if the local host changes its network integration, and the remote host is not reachable anymore.

Command: tramp-rename-files source target

Replace in all buffers the visiting file name from source to target. source is a remote directory name, which could contain also a localname part. target is the directory name source is replaced with. Often, target is a remote directory name on another host, but it can also be a local directory name. If target has no local part, the local part from source is used.

If target is nil, it is selected according to the first match in tramp-default-rename-alist. If called interactively, this match is offered as initial value for selection.

On all buffers, which have a buffer-file-name matching source, this name is modified by replacing source with target. This is applied by calling set-visited-file-name. The new buffer-file-name is prompted for modification in the minibuffer. The buffers are marked modified, and must be saved explicitly.

If user option tramp-confirm-rename-file-names is nil, changing the file name happens without confirmation. This requires a matching entry in tramp-default-rename-alist.

Remote buffers related to the remote connection identified by source, which are not visiting files, or which are visiting files not matching source, are not modified.

Interactively, target is selected from tramp-default-rename-alist without confirmation if the prefix argument is non-nil.

The remote connection identified by source is flushed by tramp-cleanup-connection.

Command: tramp-rename-these-files target

Replace visiting file names to target. The current buffer must be related to a remote connection. In all buffers, which are visiting a file with the same directory name, the buffer file name is changed.

Interactively, target is selected from tramp-default-rename-alist without confirmation if the prefix argument is non-nil.

User Option: tramp-default-rename-alist

The default target for renaming remote buffer file names. This is an alist of cons cells (source . target). The first matching item specifies the target to be applied for renaming buffer file names from source via tramp-rename-files. source is a regular expressions, which matches a remote file name. target must be a directory name, which could be remote (including remote directories TRAMP infers by default, such as ‘/method:user@host:’).

target can contain the patterns %m, %u or %h, which are replaced by the method name, user name or host name of source when calling tramp-rename-files.

source could also be a Lisp form, which will be evaluated. The result must be a string or nil, which is interpreted as a regular expression which always matches.

Example entries:

("/ssh:badhost:/path/to/dir/"
 . "/ssh:goodhost:/path/to/another/dir/")

would trigger renaming of buffer file names on ‘badhost’ to ‘goodhost’, including changing the directory name.

("/ssh:.+\\.company\\.org:" . "/ssh:multi.hop|ssh:%h:")

routes all connections to a host in ‘company.org’ via ‘/ssh:multi.hop:’, which might be useful when using Emacs outside the company network.

(nil . "~/saved-files/%m:%u@%h/")

saves all remote files locally, with a directory name including method name, user name and host name of the remote connection.

User Option: tramp-confirm-rename-file-names

Whether renaming a buffer file name by tramp-rename-files or tramp-rename-these-files must be confirmed.