Welcome to Vifm Q&A, where you can ask questions about using Vifm. Registration is optional, anonymous posts are moderated. E-mail and GitHub logins are enabled.
0 votes
in vifm by

is possible to execute :file and open the selected program as a different user from the vifm one? I'm running vifm as root and I would like to open the selection as the user currently logged in the desktop environment

1 Answer

0 votes
by

This can be done via c key in menus, although how well it works depends on the command:

mnoremap <silent> s c<c-a><c-f>func() { su myuser --command="$(printf '%%q ' "$@")"; } && func <cr>

myuser need to be specified somehow.

If there is a redirection or use of environment variables, that will happen as root. Trailing & should work fine. Alternatively, can sacrifice & and push expansion and redirection inside command run as a different user. Not sure that both cases can be made to work at the same time.

by

thank you for the code, I will build upon that

by

after a lot of trials and errors, I couldn't make it works, even if a explicitly declare the user it still open the selection as root

by

Can you show the mapping which didn't work as intended?

by

Initially I tried to directly customize the code you provided, but even change only myuser to my current desktop user is not working:

mnoremap <silent> gb c<c-a><c-f>func() { su $USER --command="$(printf '%%q ' "$@")"; } && func <cr>

note: I want to highlight that I'm executing vifm as root user

by

When you run as root $USER expands to root which is why I used myuser. If you have multiple users, then maybe define a separate environment variable that specifies user name of a GUI session.

by

I use the variable $USER extensively inside vifm and it expand to john, my desktop environment user. Even if I explicitly set john in that command, it still open the selection as root when I press gb

mnoremap <silent> gb c<c-a><c-f>func() { su john --command="$(printf '%%q ' "$@")"; } && func <cr>

I'm missing something?

by

Then it's not $USER. How complicated is the command you're trying to run, does it have shell control-flow operators like |, && or || in it?

by

the commands should be those displayed in the :file menu, so if I execute gb

mnoremap <silent> gb c<c-a><c-f>func() { su john --command="$(printf '%%q ' "$@")"; } && func <cr>

on a .jpg file, the menu reports all associated applications, but every selection, eg. okular %f is launched as root user

by

I don't see how that's possible, the command must be executed by user john. Check that mnoremap gb shows the correct mapping.

by

I started out on a new vifmrc, commented out the default gb association

" Open file in the background using its default program
"nnoremap gb :file &<cr>l

added the custom one

mnoremap <silent> gb c<c-a><c-f>func() { su john --command="$(printf '%%q ' "$@")"; } && func <cr>

and something changed, so I messed up something before, my bad

still is not working though, I don't get to menu at all now, nothing happens after gb shortcut on a file, is there a way to get a log of what's happening?

by

That sample mapping was for normal mode, you're adding a mapping for menu mode, so that wasn't the reason.

You can remove <cr> at the end to see the final command.

by
edited by

got it, is there a way, once in menu mode, to open the selected element by default as john on enter key press, without using gb?

edit: eureka!

I got it working at last

mnoremap <cr> c<c-a><c-f>su -c "<c-e>" $USER %i &<cr>

so from normal mode it use the default gb mapping and in the menu mode it's seamless integrated

by

Hm, should have checked what shell you're using, maybe it doesn't get that syntax for functions.

If you would like to make a bug report or feature request consider using GitHub, SourceForge or e-mail. Posting such things here is acceptable, but this is not a perfect place for them.
...