all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Emanuel Berg <embe8573@student.uu.se>
To: help-gnu-emacs@gnu.org
Subject: Re: How to change and save a read-only file?
Date: Sat, 29 Jun 2013 15:10:50 +0200	[thread overview]
Message-ID: <87ip0xnjtx.fsf@VLAN-3434.student.uu.se> (raw)
In-Reply-To: mailman.2780.1372497288.22516.help-gnu-emacs@gnu.org

Marius Hofert <marius.hofert@math.ethz.ch> 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


       reply	other threads:[~2013-06-29 13:10 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <mailman.2780.1372497288.22516.help-gnu-emacs@gnu.org>
2013-06-29 13:10 ` Emanuel Berg [this message]
2013-06-29 13:30   ` How to change and save a read-only file? Emanuel Berg
2013-07-01 22:35     ` Richard Copley
     [not found]     ` <mailman.14.1372718151.12400.help-gnu-emacs@gnu.org>
2013-07-01 23:42       ` Emanuel Berg
2013-07-19 12:36         ` Emanuel Berg
2013-11-20  3:27 Emanuel Berg
2013-11-20 14:22 ` Kevin Rodgers
     [not found] ` <mailman.6640.1384957268.10748.help-gnu-emacs@gnu.org>
2013-11-20 16:24   ` Emanuel Berg
  -- strict thread matches above, loose matches on Subject: below --
2013-06-29  9:14 Marius Hofert
2013-06-29  9:20 ` Peter Dyballa
2013-06-29 11:04 ` Thorsten Jolitz
2013-06-29 11:16   ` Jude DaShiell

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87ip0xnjtx.fsf@VLAN-3434.student.uu.se \
    --to=embe8573@student.uu.se \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.