在emacs中使用root權限打開當前文件

寫了一個小函數,能夠以root權限從新打開當前的文件,相似sudo。 shell

(provide 'sudo)
(defun sudo-reopen ( ) 
  "reopen current file with sudo."
;  (setq username (replace-regexp-in-string "\n" "" (shell-command-to-string "whoami")))
  (setq sudo-file-real-path
	(replace-regexp-in-string "\n" ""
				  (shell-command-to-string (concat "readlink -f " buffer-file-truename))
				  )
	)
  (kill-this-buffer)
  (find-file (concat "/sudo::" sudo-file-real-path))
  (interactive) 
  )

主要就是提供一個函數 sudo-reopen 以 /sudo::/path/to/file 的方式從新打開當前的文件。將上述代碼保存爲sudo.el後在 .emacs 中進行下面的配置: ide

(require 'sudo)
(global-set-key (kbd "C-c s u") 'sudo-reopen)
相關文章
相關標籤/搜索