Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to map this command to a key?

10 views
Skip to first unread message

Ben

unread,
May 13, 2012, 9:12:52 PM5/13/12
to
I'm trying to assign the command below to the F10 key. Assuming you
have sudo, the command will allow you to save root owned files, even
though you opened as a normal user. But I can't seem to get it to work
correctly.

-Thanks


map <F10> execute ':silent w !sudo tee % > /dev/null' . "| :edit!"

Eli the Bearded

unread,
May 16, 2012, 7:55:47 PM5/16/12
to
In comp.editors, Ben <n...@way.com> wrote:

How do the owners of way.com feel about your use of their domain?

> I'm trying to assign the command below to the F10 key. Assuming you
> have sudo, the command will allow you to save root owned files, even
> though you opened as a normal user. But I can't seem to get it to work
> correctly.

What isn't working? The command, which looks exceedingly complex for
the task, or getting it mapped to F10, which is what your subject
implies.

> map <F10> execute ':silent w !sudo tee % > /dev/null' . "| :edit!"

The map lhs (left hand side, here "<F10>") should be fine.

This bit is super ugly:
:silent w !sudo tee % > /dev/null

Why use 'tee' if you don't want two copies? Use 'cat'. After you run
that bit, as you have probably noticed, vim suddenly becomes aware that
the file has changed underneath it. Your "| :edit!" seems to be the
attempt to fix that, but it doesn't work, does it? I'm not sure if there
is a way to avoid the "reload file" issue.

If you've got sudo, the best option is "sudo vi file" instead of this
convoluted mess. If I really wanted to do this, I'd just live with

map <F10> :w !sudo cat > %

Elijah
------
would most likely save the file in /tmp then copy it as root

Christian Brabandt

unread,
May 17, 2012, 9:33:20 AM5/17/12
to
On 2012-05-16, Eli the Bearded <*@eli.users.panix.com> wrote:
> The map lhs (left hand side, here "<F10>") should be fine.
>
> This bit is super ugly:
> :silent w !sudo tee % > /dev/null
>
> Why use 'tee' if you don't want two copies? Use 'cat'. After you run
> that bit, as you have probably noticed, vim suddenly becomes aware that
> the file has changed underneath it. Your "| :edit!" seems to be the
> attempt to fix that, but it doesn't work, does it? I'm not sure if there
> is a way to avoid the "reload file" issue.
>
> If you've got sudo, the best option is "sudo vi file" instead of this
> convoluted mess. If I really wanted to do this, I'd just live with
>
> map <F10> :w !sudo cat > %
>

That's why I have written
http://www.vim.org/scripts/script.php?script_id=2709
that takes care of all this and also persistent undo files.

regards,
Christian

Geoff Clare

unread,
May 18, 2012, 8:53:22 AM5/18/12
to
Eli the Bearded wrote:

> This bit is super ugly:
> :silent w !sudo tee % > /dev/null
>
> Why use 'tee' if you don't want two copies?

Because cat can't open files for writing.

> map <F10> :w !sudo cat > %

That doesn't work because the shell tries to open the file
before it executes sudo. To use cat you would have to do
something like:

map <F10> :w !sudo sh -c 'cat > %'

which complicates the quoting.

--
Geoff Clare <net...@gclare.org.uk>

0 new messages