help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to change and save a read-only file?


From: Emanuel Berg
Subject: Re: How to change and save a read-only file?
Date: Sat, 29 Jun 2013 15:10:50 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/23.4 (gnu/linux)

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 via email to

[Prev in Thread] Current Thread [Next in Thread]