You are not logged in.

#1 2019-04-13 09:03:14

NginUS
Member
Registered: 2019-04-13
Posts: 10

Changing default editor only works sometimes

My goal here is to change the default editor "vi" with "nano" globally, ie. in all possible scenarios.

I'm on a fresh install in VirtualBox.

I have a user created with

useradd -m

I looked in the Wiki on the 'Environment Variables' page & wasn't able to make sense of what file to edit or exactly what lines to add while editing. What I tried that didn't work was:

1) Executed in bash at root prompt:

export EDITOR="$(if [[ -n $DISPLAY ]]; then echo 'kate'; else echo 'nano'; fi)"

2) Added the line:

export EDITOR="$(if [[ -n $DISPLAY ]]; then echo 'kate'; else echo 'nano'; fi)"

to both:

nano /etc/profile

and

nano /etc/environment

3) Added the lines:

export VISUAL=nano
export EDITOR=nano

to both:

nano /etc/profile
nano /etc/environment

4) Added the lines:

export VISUAL=nano
export EDITOR=nano

to:

/etc/profile.d/editor.sh

then executed in bash prompt as root:

chmod a+x /etc/profile.d/editor.sh

... In the end I've undone my edits to both:

nano /etc/profile
nano /etc/environment

And I only have the

/etc/profile.d/editor.sh

change remaining since it works under a single, & specific circumstance.

I test each of my efforts by executing "visudo" in all the possible ways I can think of.

If I do:

1)

user@hostname$ visudo

>> I get

Permission denied

as expected.

2)

user@hostname$ sudo visudo

>> I get it in vi

3)

user@hostname$ sudo su
root@hostname# visudo

>> I get it in vi

4)

user@hostname$ su -
root@hostname# visudo

>> I get it in nano


Not sure how to fix this so it uses nano in all of the above scenarios. Could use a hand.

Thanks in advance.

Last edited by NginUS (2019-04-13 09:41:46)

Offline

#2 2019-04-13 09:27:04

V1del
Forum Moderator
Registered: 2012-10-16
Posts: 23,430

Re: Changing default editor only works sometimes

Please post outputs in [ code ] tags.

sudo is a  special case that discards a lot of your environment variables by default, tell it to keep EDITOR https://wiki.archlinux.org/index.php/Su … _variables

Or specifically special case it for visudo: https://wiki.archlinux.org/index.php/Sudo#Using_visudo

Or rather set in your /etc/environment as that is going to be read and applied by more things, though you can't use your if statement attempt there. and your syntax would be wrong it should be

EDITOR=nano

without an export.

Last edited by V1del (2019-04-13 09:41:06)

Offline

#3 2019-04-13 10:12:18

NginUS
Member
Registered: 2019-04-13
Posts: 10

Re: Changing default editor only works sometimes

V1del wrote:

Please post outputs in [ code ] tags.

Done

V1del wrote:

set in your /etc/environment as that is going to be read and applied by more things, though you can't use your if statement attempt there. and your syntax would be wrong it should be

EDITOR=nano

without an export.

I've changed the file:

/etc/environment

to include the line:

EDITOR=nano

...And after logging out of both root & user accounts, then  logging back into user then

su -

back to root, when I do

visudo

It's still using vi. So that alone isn't doing it.

...Then in addition I tried your suggestion:

V1del wrote:

Or specifically special case it for visudo: https://wiki.archlinux.org/index.php/Sudo#Using_visudo

...And after logging out of both root & user accounts, then  logging back into user then running

visudo

as root uses nano.

Then I added my user to sudoers, & running

sudo visudo

as user opens it in nano.

After this, I'm not sure where if at all this part you mentioned would be of use:

V1del wrote:

sudo is a  special case that discards a lot of your environment variables by default, tell it to keep EDITOR https://wiki.archlinux.org/index.php/Su … _variables

I don't quite understand about what its referring to with adding

Defaults env_keep += "foo1 foo2 foo3"

...instead of just aliasing

alias sudo='sudo -E '

...Or in what case I would even need that.

And where it talks about adding

alias sudo='sudo '

to

~/.bashrc

and to

/etc/bash.bashrc

I'm completely lost as to how that does what it's said to do.

Again, not sure if this part matters here...

Thanks very much for your reply, as I'm far better off now than I was previously- even without understanding that last part.

EDIT:
When I added the 2 lines to:

/etc/profile.d/editor.sh

Was that done correctly such that it's having a positive effect on the new behavior I'm seeing?
Do I need that step?

Last edited by NginUS (2019-04-13 10:23:06)

Offline

#4 2019-04-13 11:31:41

V1del
Forum Moderator
Registered: 2012-10-16
Posts: 23,430

Re: Changing default editor only works sometimes

Adding it to /etc/profile.d/ will ensure that it is exported into the environment when doing a login shell, it will not be evaluated on simple su or sudo (but on su - and in general when you log in), but yes that was the correct way to change that for this case.

You should not generally alias sudo to sudo -E (that would retain your entire environment to the sudo process, which isn't something you generally want) there are valid security implications on why sudo does discard a lot of the environment by default. Editing the env_keep variable allows you to fine grainedly retain certain environment variables you "know" are safe.  So you'd add

Defaults env_keep += "EDITOR VISUAL"

Offline

Board footer

Powered by FluxBB