From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Emanuel Berg Newsgroups: gmane.emacs.help Subject: Re: How to change and save a read-only file? Date: Sat, 29 Jun 2013 15:10:50 +0200 Organization: Aioe.org NNTP Server Message-ID: <87ip0xnjtx.fsf@VLAN-3434.student.uu.se> References: NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: ger.gmane.org 1372514649 26702 80.91.229.3 (29 Jun 2013 14:04:09 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sat, 29 Jun 2013 14:04:09 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Sat Jun 29 16:04:11 2013 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1Usvl3-0006l5-1c for geh-help-gnu-emacs@m.gmane.org; Sat, 29 Jun 2013 16:04:09 +0200 Original-Received: from localhost ([::1]:56204 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Usvl2-0004xc-Lg for geh-help-gnu-emacs@m.gmane.org; Sat, 29 Jun 2013 10:04:08 -0400 Original-Path: usenet.stanford.edu!news.kjsl.com!feeder.erje.net!eu.feeder.erje.net!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!aioe.org!.POSTED!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 78 Original-NNTP-Posting-Host: SWN/nubmpQxYKwY7hPy4YA.user.speranza.aioe.org Original-X-Complaints-To: abuse@aioe.org User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.4 (gnu/linux) X-Notice: Filtered by postfilter v. 0.8.2 Cancel-Lock: sha1:R4HYJemxBlIk2xu/hUS847MYXXE= Original-Xref: usenet.stanford.edu gnu.emacs.help:199584 X-Mailman-Approved-At: Sat, 29 Jun 2013 10:02:12 -0400 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:91854 Archived-At: Marius Hofert writes: > I often would like to edit files in emacs, which are > write-protected (owned by root, for example). With "C-x C-q" I > can disable 'read-only mode', which allows me to make changes in > the file. But when I want to save the file, I obtain "File > ... is write-protected; try to save anyway? (y or n)". Even if I > choose 'y', it fails (*Messages* saying: "basic-save-buffer-2: > Doing chmod: operation not permitted"). I want to do this on a file-to-file basis - and I don't want to restart Emacs to do it. When I open an out-of-line file - say /etc/rc.local - I want it to be in read-only mode, as I don't want any sleepy fingers to jitter it with unintended chars. However, sometimes I *do* want to tell explicitly that I want to edit it, and then it need to be put forward with superuser access. For this, a use `su-edit'. It works like this: 1. Get the point position. 2. Kill the read-only buffer. 3. Open it as super user. 4. Move point and the window to make it look just the way it looked. (Point 4 - that is all that complicated stuff in the code. And, sometimes, it doesn't work! If you feel like helping me with that, please do. I'd like the window to be exactly the same (seemingly). Only, it works for 1-3, so I never did that detail.) Anyway: (defun su-edit () "Edit the current buffer file as superuser - in a new buffer, that should look the same." (interactive) (let ((start-point-pos (point))) (recenter-top-bottom 0) (let ((top-window-pos (point)) (buffer (buffer-file-name)) ) (kill-buffer) (find-file (format "/sudo::%s" buffer)) (goto-char top-window-pos) (recenter-top-bottom 0) (goto-char start-point-pos) ))) But, there is a much *better* way, as the files you have to edit as superuser are many, but not *that* many: (global-set-key (kbd "C-j") 'jump-to-register) (set-register ?a (cons 'file "/sudo::/etc/apt/sources.list")) (set-register ?c (cons 'file "~/.irssi/config")) (set-register ?C (cons 'file "/sudo::/etc/default/console-setup")) (set-register ?e (cons 'file "~/.emacs")) ;; etc. Note: 1) How to use it for superuser, *and* ordinary files, 2) that it is case sensitive ('c' and 'C'), and 3) the mnemonic use of letters (I use zsh, otherwise there would have been a 'b' for .bashrc - you get it). Before I started with this system, I lost a lot of time typing file paths, iterating the buffer list, etc. Now I just (for example) [C-j e] to bring up .emacs - and if there is such a buffer already, I get to the correct place (where I had point on my last visit). Only for some rare cases do I bump into a file I have to edit, which I haven't added to the list - that's when I use the (imperfect) `su-edit', all the while contemplating if I should add that file to a register... -- Emanuel Berg - programmer (hire me! CV below) computer projects: http://user.it.uu.se/~embe8573 internet activity: http://home.student.uu.se/embe8573