ZSH always requires to restart terminal in order to access alias - macos

I have added several aliases to my .zshrc file and they ONLY work if I restart terminal or use the source ~/.zshrc If I just open terminal, then type the alias, it will not recognize it, until I call source ~/.zshrc
So I know it's not a problem with the alias I created, I just have to load up the .zshrc file every time I want to use them.
What is going on? How can I fix this?

Well, you don't expect that you only have to edit a file and then, by magic, all your current zsh instances somehow ingest the changes, do you?
From the zsh man page, section STARTUP/SHUTDOWN FILES :
if the shell is interactive, commands are read from /etc/zshrc and then $ZDOTDIR/.zshrc
($ZDOTDIR defaults to your $HOME). Hence, if you are in your terminal, you have three choices. Two of them you already found out (restart the terminal, source .zshrc manually). The third choice would be to just open a zsh subshell (by typing zsh).
Actually, there is a trick to do some "magic" in reading the file automatically: Zsh allows you to define a so-called precmd hook, which allows you to establish an arbitrary command to be executed just before a command prompt will be displayed. You could use it to source any file you like. If you want to use this feature, I strongly recommend against sourcing all of .zshrc. Sooner or later, you will have stuff in .zshrc that you don't want to be executed every time.
Instead, put your alias definitions into a separate file, say $HOME/.aliases, and in Zsh define the hook
function precmd {
source $HOME/.aliases
}
If you later change the .aliases file, you would still have to type a Carriage Return in your shell, in order to provoke a new prompt to be written and the precmd to be executed, but this is less cumbersome than sourcing the file manually.

Related

Bash alias not saving beyond one session?

I'm trying to make a bash alias for traversing through a few folders, but the alias does not save after I close terminal.
I've already saved the alias in the .bashsrc file and have also have run the command . ~/.bashsrc.
Here's what I've done:
sudo nano .bashsrc
Inside bashsrc:
alias x = 'cd Documents/Photos/Family'
And then saved and exit.
. ~/.bashsrc
The alias works in that terminal window, but shows "command not found" if I restart terminal or open a new tab.
You should prefer setting your changes in ~/.bashrc and ~/.bash_profile.
alias x='cd Documents/Photos/Family'
Also, remember aliases won’t be exported to subshells and while using this alias you always need to be present in the directory where Documents is present. I guess your Documents directory is present in home, so it would be better if you do something like:
alias x="cd "$HOME"/Documents/Photos/Family"
Just add this line to the end of both the files and save.
In simple language there are two types of shells for your case, login and non-login interactive shell.
Before starting of a login shell, .bash_profile is sourced and before starting of a non-login shell .bashrc is sourced.
So you should add your changes in both.
If you are on macOS, .bashrc is next to useless for your case. MacOS would treat every shell you open in terminal as a login shell. Although there are some exceptions which i don’t remember.
As mentioned by David in the comments, in some distros ~/.bash_profile is just ~/.profile (e.g. Debian, openSUSE, etc).

How to open a file in linux without specifiying editor?

I have emacs as my defualt editor in linux, and I also have alias in my .cshrc file.
alias e "emacs -mm"
Sometime I just want to hit the file name in the command line and open it in emacs directly with out the editor beign metioned.
Example instead of
$ e foo.cc&
What I want is to open
$foo.cc
May be this is lazy to do but it saves a lot time if you have so many files to handle. Thanks for the help.
You probably cannot open a file with $foo.cc (and that would be ambiguous for a shell script script.sh: would script.sh means "edit the file script.sh" or "run the shell script script.sh" ?). However,
You might want to use xdg-open, or the $EDITOR variable (see environ(7)). If you always have a single emacs running, you might set EDITOR to emacsclient in your ~/.bashrc (if using /bin/bash) or your ~/.zshrc (if using /bin/zsh)
BTW many editors (including emacs, gedit, vim) are able to edit several files, i.e. $EDITOR *.c
And depending upon your login shell (zsh, fish, or bash) you could set up a shell function or alias to simply type e foo.c; I feel that it is not worth the effort, since with autocompletion I just have to type 3 keys e m tab to get emacs (and often the up arrows are enough)
Actually I start only once every day emacs then open many files inside it (and also I compile inside emacs)
BTW, you should avoid csh since it is considered harmful. Install a good interactive shell (e.g. with sudo aptitude install zsh zsh-doc) and use once chsh(1) to make it your login shell.

How to make Emacs 's shell mode source my profile file?

I have defined some aliases and function snippets int some of my profile files, say, ~/.zprofile. But Emacs never reads them. There is already a topic about it. However, it's not enough:
It cannot source completely .zshrc and would emit errors for
compinit and the like.
Seems this approach only works for environment VARIABLES.
So in shell-mode or run command in Emacs(Alt-!) the effect is still different with that in terminal(emulators).
So is there any way to deal with the problem? Thanks.
You can create a file ~/.emacs_zsh (or .emacs_bash, emacs_sh, ...) that shell-mode will use on startup. My .emacs_bash is simply:
. ~/.profile
Just be sure to put a newline at the end of the sourcing line or it won't get executed.

How to "source" ~/.bashrc automatically once it has been edited?

I would like to create an alias that does the following:
Opens TextMate with ~/.bashrc and allows me to edit it
Once I close TextMate, "sources" ~/.bashrc (so if I add a new alias, for example, it will be available immediately)
I tried the following:
alias b="/usr/bin/mate -w ~/.bashrc; source ~/.bashrc"
but it doesn't work: when I close TextMate, the shell doesn't return.
Any ideas?
I hesitate to suggest it, but if this is a feature you really want, you can make something similar happen by setting the PROMPT_COMMAND variable to something clever.
PROMPT_COMMAND is run every time the shell shows the shell prompt So, if you're okay with the shells updating only after you hit Enter or execute a command, this should nearly do it.
Put export PROMPT_COMMAND="source ~/.bashrc" into your ~/.bashrc file. Re-source it into whichever shell sessions you want the automatically updating behavior to work in.
This is wasteful -- it re-sources the file with every prompt. If you can get your editor to leave the old version in a specific file, say ~/.bashrc~ (where the first ~ means your home directory and the last ~ is just a ~, a common choice for backup filenames) then you could do something more like (untested):
export PROMPT_COMMAND="[ ~/.bashrc -nt ~/.bashrc~ ] && touch ~/.bashrc~ && source ~/.bashrc "
then it would stat(2) the two files on every run, check which one is newer, and re-source only if the ~/.bashrc is newer than its backup. The touch command is in there to make the backup look newer and fail the test again.

if .bash_profile usually source .bashrc any way, why not just use .bashrc?

it seems that we will put
source ~/.bashrc
in our .bash_profile anyway. So why not just use one file, say .bashrc ?
Because there may be things you only want to do once per login (so in .bash_profile) rather than every time an xterm or the like opens (as per .bashrc), for example asking the user for a passphrase to decrypt and load SSH keys into an ssh agent, etc etc.
You can put some things in .bash_profile that are not appropriate for a shell instance that is not a terminal. For example, if you ran an external command from your editor through the shell - the shell instance would source .bashrc but not .bash_profile. For example, I might put alias ls=ls -F in my profile, but you wouldn't want that alias applied for just any instance of the shell, just ones you would interact with.

Resources