Michael Zakariaie
Michael Zakariaie

Reputation: 185

Terminal thinks "nano" is "open" in a command I put in .bash_profile (mac)

I've made a bunch of commands to use as shortcuts and they work but I wanted to make a command that would shortcut and open the in-terminal text editor, nano, with the .bash_profile.

Essentially instead of typing

sudo nano /Users/myUserName/.bash_profile

(the sudo is so I can save edits)

I want a command that I put in the .bash_profile to do that for me, manually adding this line of code to it:

alias commands='sudo nano /Users/myUserName/.bash_profile'

The problem is, when I run the command it just opens the file in textedit and it's a locked file meaning I can't edit.

Is there a way to do this?

Upvotes: 0

Views: 1996

Answers (2)

user3277192
user3277192

Reputation:

fix permissions on .bash_profile

.bash_profile should normally be owned by the user, not by root.

I'd suggest reverting it back to the right user using sudo chown myUserName /Users/myUserName/.bash_profile and then stop using that sudo command completely. There should be no need to have superuser power to edit that file to start with.

IMHO you're making it more difficult than it needs to be.

do not edit your files with root permissions.

Once you restore the normal ownership, groups and permissions, all you need is to type

nano ~/.bash_profile or whatever your favorite editor might be.

sudo in front of a command uses superuser (root) as the effective userid, use it sparingly and intentionally. It's a bit like the dark side of the force: yes the superuser can do more, but once you start using it, going back to the light side is difficult as all your stuff will be owned by the superuser and you'll start seeing things fail that should not fail.

Apply an edited .bash_profile in an already running shell

A copy of bash already running will not automagically pick up any change you make to your .bash_profile . It needs to be told to pick up the changes.

Either you start a new shell by starting another terminal.

Or you can also apply some changes (but maybe not undo all what was in the original!) .bash_profile by "sourcing" the file:

. ~/.bash_profile will do that for you - but there are limits to what it can do (e.g. if you defined an alias for ls, removing that line and sourcing the bash_profile again will NOT remove the alias form your running shell, you'll have to do it yourself, or quit that shell.

See also:

Upvotes: 3

l'L'l
l'L'l

Reputation: 47264

I would suggest first changing the (correct) permissions/ownership of ~/.bash_profile. Then you could make a function for a command to edit it; also try setting nano as your default editor:

$ sudo chown user:staff ~/.bash_profile

This should change the permissions (substitute user and group for yours). Then if you want a shortcut to edit the file add this to ~/.bash_profile:

# Preferred editor (nano)
export EDITOR=nano

# Edit ~/.bash_profile in nano
edbp () { nano ~/.bash_profile ; }

Once added and saved relaunch Terminal. The command below should nano ~/.bash_profile:

$ edbp

To quickly/easily edit your profile.

Upvotes: 1

Related Questions