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.
1) Keybinding
- (global-set-key (kbd "C-x C-r") 'find-alternative-file-with-sudo)
2) Code
- (defun find-alternative-file-with-sudo ()
- "Open current buffer as root!"
- (interactive)
- (when buffer-file-name
- (find-alternate-file
- (concat "/sudo:root@localhost:"
- buffer-file-name))))
- (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))
- (find-file-root-header-warning)))
- (add-hook 'find-file-hook 'find-file-hook-root-header-warning)
No comments:
Post a Comment