在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)
相关文章
相关标签/搜索