0

I found the following code here,

(defun sudo-find-file (file-name)
  "Like find file, but opens the file as root."
  (interactive "FSudo Find File: ")
  (let ((tramp-file-name (concat "/sudo::" (expand-file-name file-name))))
    (find-file tramp-file-name)))

But it tries to find the file is sudo mode in the current directory and I have to delete so many characters to reach root directory /.

How can I define the default opening path in the mentioned code?

0

Here an example with "/" as default directory:

(defun sudo-find-file (file-name)
  "Like find file, but opens the file as root."
  (interactive
  (list (read-file-name "sudo Find File: " "/")))
  (let ((tramp-file-name (concat "/sudo::/" (expand-file-name file-name))))
    (find-file tramp-file-name)))
0

Use double slashes //. In the local case, a double slash in the minibuffer means to start at the root directory. In the remote case, a double slash in the minibuffer means to start at the root directory of the remote machine.

See the discussion in the Tramp manual, section "File name completion".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.