Emacs:以管理员身份编辑当前文件
我经常以普通用户的身份打开一个自己没有编辑权限(write permission)的文件(自动进
入只读模式),然后读着读着,发现需要对其进行改动,于是就得输入以下命令来以 root
身份重新打开这个文件:
C-x C-f /sudo:root@localhost:/path-to-the-file/
为了少打几个字,我写了个小函数,它检测当前的 buffer 是否有与之关联的文件,如果有,
则以 root 身份打开它:
(defun wenshan-edit-current-file-as-root () "Edit the file that is associated with the current buffer as root" (interactive) (if (buffer-file-name) (progn (setq file (concat "/sudo:root@localhost:" (buffer-file-name))) (find-file file)) (message "Current buffer does not have an associated file.")))
注 :目前只对本地机器上的文件有效。
eval 一下之后,输入 M-x wenshan-edit-current-file-as-root 即可调用之。如果经
常用的话,可以考虑绑定个快捷键。
Happy Hacking!