I found this stuff very handy:
The procedure is to open any file belonging to root, for example, and then hit C-x C-r to reopen it as root using tramp.
- (global-set-key (kbd "C-x C-r") 'find-alternative-file-with-sudo)
- (defun find-alternative-file-with-sudo ()
- "Open current buffer as root!"
- (when buffer-file-name
- (concat "/sudo:root@localhost:"
- (defface find-file-root-header-face
- '((t (:foreground "white" :background "red3")))
- "*Face use to display header-lines for files opened as root.")
- (defun find-file-root-header-warning ()
- "*Display a warning in header line of the current buffer.
- This function is suitable to add to `find-file-root-hook'."
- (let* ((warning "WARNING: EDITING FILE AS ROOT!")
- (space (+ 6 (- (window-width) (length warning))))
- (bracket (make-string (/ space 2) ?-))
- (warning (concat bracket warning bracket)))
- (setq header-line-format
- (propertize warning 'face 'find-file-root-header-face))))
- (defun find-file-hook-root-header-warning ()
- (when (and buffer-file-name (string-match "root@localhost" buffer-file-name))
- (add-hook 'find-file-hook 'find-file-hook-root-header-warning)