You are not logged in.

#1 2007-08-29 17:28:54

alexmatos
Member
From: Rio de Janeiro, Brasil
Registered: 2007-08-06
Posts: 124

alias vi='vim' for sudo

Vim was not longer being recognized by the command 'vi', so I had to add to my .bashrc (and root's one also) the line

alias vi='vim'

The problem is that I now want to use sudo autentication. I think I did everything right and sudo is working, but I lost my alias. If I use normal user or root, it's there. Let me try to clear it up with examples:

---
as normal user:

vi /etc/rc.conf

Vim opens correctly.

---
as root:

vi /etc/rc.conf

Vim opens correctly.

---
as normal user:

sudo vi /etc/rc.conf

Vi opens instead.

---
An observation: I realized that Vi was opening instead because of the syntax highlighting colors and the lack of bold fonts. That is, when i used the command 'sudo vim /etc/rc.conf' it was displayed correctly.


Any guesses?

Offline

#2 2007-08-29 17:53:37

kishd
Member
Registered: 2006-06-14
Posts: 401

Re: alias vi='vim' for sudo

As I understand it sudo runs commands with root privileges but does not actually log in as root so roots .bashrc will not be read when running a command.


---for there is nothing either good or bad, but only thinking makes it so....
Hamlet, W Shakespeare

Offline

#3 2007-08-29 17:55:33

shining
Pacman Developer
Registered: 2006-05-10
Posts: 2,043

Re: alias vi='vim' for sudo

kishd wrote:

As I understand it sudo runs commands with root privileges but does not actually log in as root so roots .bashrc will not be read when running a command.

No, but I thought it used the user settings.


pacman roulette : pacman -S $(pacman -Slq | LANG=C sort -R | head -n $((RANDOM % 10)))

Offline

#4 2007-08-29 20:06:13

kishd
Member
Registered: 2006-06-14
Posts: 401

Re: alias vi='vim' for sudo

If you run sudo -V as root it shows the environment variables. running on my comp it shows bash as one of the environment variables to be removed. I think that these variables can be set in the sudoers file.


---for there is nothing either good or bad, but only thinking makes it so....
Hamlet, W Shakespeare

Offline

#5 2007-08-29 23:07:03

alexmatos
Member
From: Rio de Janeiro, Brasil
Registered: 2007-08-06
Posts: 124

Re: alias vi='vim' for sudo

kishd wrote:

I think that these variables can be set in the sudoers file.

Didn't find how to do it in '/etc/sudoers'. I googling to find an answer. If anybody has it, I'd be thankfull.

Offline

#6 2007-08-30 00:07:28

aythun
Member
Registered: 2007-05-04
Posts: 11

Re: alias vi='vim' for sudo

With `visudo`, try adding this line:

Defaults !env_reset

Offline

#7 2007-08-30 00:17:36

skymt
Member
Registered: 2006-11-27
Posts: 443

Re: alias vi='vim' for sudo

Alias expansion (translating "vi" into "vim", in this case) is done by the shell before the command is executed. Bash only does alias expansion on the first word in a command line, so it doesn't touch "vi", the second word. I suggest either setting "sudoedit" to "vim" in your sudoers file and using "sudo -e", or just getting used to typing "vim".

Offline

#8 2007-08-30 03:15:49

alexmatos
Member
From: Rio de Janeiro, Brasil
Registered: 2007-08-06
Posts: 124

Re: alias vi='vim' for sudo

skymt wrote:

Alias expansion (translating "vi" into "vim", in this case) is done by the shell before the command is executed. Bash only does alias expansion on the first word in a command line, so it doesn't touch "vi", the second word. I suggest either setting "sudoedit" to "vim" in your sudoers file and using "sudo -e", or just getting used to typing "vim".

That makes sense. I'm not lazy to type 'vim', it was just a matter of curiosity. Thanks to everyone for the help.

Offline

#9 2007-08-30 04:49:22

nikron
Member
Registered: 2007-05-15
Posts: 130

Re: alias vi='vim' for sudo

You could always remove and link it to vim.  ...   How bad could it be?

Offline

#10 2007-08-30 04:57:07

harlekin
Member
From: Germany
Registered: 2006-07-13
Posts: 408

Re: alias vi='vim' for sudo

I somehow felt I've read somewhere, at some time that you have to add a alias for sudo as well to use aliases for the second command.

Try adding alias sudo="sudo" as well. I neither have tested this nor put real hope in this to work. But it might be a try.

Last edited by harlekin (2007-08-30 04:57:40)


Hail to the thief!

Offline

#11 2007-08-30 11:58:19

Cerebral
Forum Fellow
From: Waterloo, ON, CA
Registered: 2005-04-08
Posts: 3,108
Website

Re: alias vi='vim' for sudo

harlekin wrote:

I somehow felt I've read somewhere, at some time that you have to add a alias for sudo as well to use aliases for the second command.

Try adding alias sudo="sudo" as well. I neither have tested this nor put real hope in this to work. But it might be a try.

That unfortunately doesn't work:

$ alias a="echo This is the first"; alias b=" and this is the last."
$ a b
This is the first b
$

Offline

#12 2007-08-30 13:22:42

ralvez
Member
From: Canada
Registered: 2005-12-06
Posts: 1,694
Website

Re: alias vi='vim' for sudo

I'm not sure if I'm missing something but ... I use vim in both user and root mode.
I have .vimrc in my ~/userid directory and a .vimrc in the /root home directory.
When I do

su -c 'vim /etc/rc.conf'

for example I get vim with root's settings and when I issue vim xyz I get it with my user settings.

Hope this helps.

R

Offline

#13 2011-09-12 14:01:31

TheFunnyOne
Member
From: Cupertino
Registered: 2011-09-12
Posts: 1

Re: alias vi='vim' for sudo

Hi all, I tried the method mentioned by harlekin and it works. The website that I googled describing this is:

http://www.shellperson.net/using-sudo-with-an-alias/

Just make sure you have the space in "sudo ". The space is very critical.

so put this line in your .bashrc file.

alias sudo="sudo "

This way, if the second argument is an aliases, it will get expanded prior to passing to sudo. Very clever trick I think :0)

Also, there is another way to do the 'sudo vi' command. You can just 'vi filename' as a normal user. Then in the vim editor, do ":w !sudo tee %". This will write the file as root. You can do this if you forgot to 'sudo vi'ed the file and instead just 'vi'ed  the file. Vim will prompt you to reload the file as the file is written/changed outside of vim.

Hope this helps. :0)

cheers!




harlekin wrote:

I somehow felt I've read somewhere, at some time that you have to add a alias for sudo as well to use aliases for the second command.

Try adding alias sudo="sudo" as well. I neither have tested this nor put real hope in this to work. But it might be a try.

Last edited by TheFunnyOne (2011-09-12 14:04:10)

Offline

#14 2011-09-12 17:42:55

fsckd
Forum Fellow
Registered: 2009-06-15
Posts: 4,173

Re: alias vi='vim' for sudo

Hello TheFunnyOne, welcome to Arch Linux. Necro bumping is usually not allowed on the forums. Please read and make yourself familiar with the forum rules. Thanks.
Closing.


aur S & M :: forum rules :: Community Ethos
Resources for Women, POC, LGBT*, and allies

Offline

Board footer

Powered by FluxBB