Alias not updated after using source .bashrc - linux

I am testing the bash behavior on login (terminal 1), but I got confused about its interaction with alias:
I opened with vim .bashrc and add this line :
alias ls='ls -l'
and save it with :x
then I used source .bashrc to simulate a new login session and I found it in the aliases list
But I removed the alias from .bashrc and use source .bashrc again I saw that alias ls='ls -l' was still available. On the other hand, opening new shell terminal (terminal 2) the problem was solved.
Question: Why alias ls='ls -l' was not removed on the first terminal ?

Sourcing .bashrc doesn't clear what you have defined so far. It just adds the definitions it contains to your current environment.
If you want to undefine a given alias, just type:
$ unalias ls
$ source .bashrc
If you want to undefine all aliases:
$ unalias -a
$ source .bashrc
Finally, if you want to start over with a brand new shell, you can of course close your session and reopen one, but here is an almost identical command in case this is not that easy (ssh) or undesirable:
$ exec bash
(you may also add the -l option to simulate a login shell, thus reading your ~/.bash_profile file)

Related

Why is alias not available in /etc/profile?

I put alias se='sudo -E ' in /etc/profile and rebooted. Then I typed se and terminal just said "command not found".
Why is alias not available in /etc/profile? How to solve it?
EDIT:
I have tried put alias se='sudo -E ' in /etc/profile, ~/.zprofile, ~/.zshrc
/etc/profile failed
/etc/zprofile failed
/etc/zshenv successful
/etc/zshrc successful
~/.zprofile failed
~/.zshrc successful
REF:
https://unix.stackexchange.com/questions/38175/difference-between-login-shell-and-non-login-shell
https://en.wikipedia.org/wiki/Shell_(computing)
https://en.wikipedia.org/wiki/X_Window_System
As you are using /etc/profile, i am assuming that you are looking to do this only for login sessions of shell (zsh).
That's because zsh by default does not read /etc/profile when starting a login shell. For setting any global parameter for only login shells, use the file /etc/zsh/zprofile.
For setting something applicable on any sort of invocation, use /etc/zsh/zshenv instead. The typical/generic order is zshenv > zprofile > zshrc > zlogin (depending on invocation and availability).
Needless to say, for any user specific parameter you should use the user specific .zprofile i.e. ~/.zprofile instead.
Notes:
If you are looking to do this for any interactive shell session, use .zshrc (/etc/zshrc or ~/.zshrc)
If you are looking at using a common /etc/profile (or similar), source (.) /etc/profile from the relevant file for zsh.

Closing the terminal doesnt remember my .profile changes for nvm/npm

I installed nvm/npm using this instruction
https://www.digitalocean.com/community/tutorials/how-to-install-node-js-on-an-ubuntu-14-04-server (How To Install Using NVM), but everytime I close my terminal, it seems to forget all the settings and I have to do the command:
source ~/.profile then select the npm version to make it run again. How can I keep my settings permanent, or at least for the duration of my logged in session? Thanks! (linuxmint 17)
Sourcing ~/.profile
~/.profile is typically invoked from a login shell. See http://www.gnu.org/software/bash/manual/bashref.html#Bash-Startup-Files
Invoked as an interactive login shell, or with --login
When Bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable. The --noprofile option may be used when the shell is started to inhibit this behavior.
Opening a terminal does not typically invoke a login shell. Have you tried rebooting/relogging? Additionally if either ~/.bash_profile or ~/.bash_login exist, then bash won't source ~/.profile. There are two common ways to fix this:
Option 1
Move commands from the ~/.profile file to ~/.bash_profile
Option 2
Source ~/.profile from ~/.bash_profile:
[[ -f ~/.profile ]] && . ~/.profile
Default Node version
You can configure a default node version by aliasing it to default
To set a default Node version to be used in any new shell, use the alias 'default':
nvm alias default node
See https://github.com/creationix/nvm

How can I force nvim to source my .bash_profile when creating a terminal buffer?

From my testing, it's clear that nvim doesn't source your .bash_profile when opening a new terminal buffer. I would like to force this to happen every time a new terminal buffer is created.
To confirm this behavior:
open your .bash_profile in nvim
export a new variable like
ISSOURCED
write out the file
open a terminal buffer
run echo $ISSOURCED
I've also checked that bash is running in interactive mode (it is) by executing if tty -s; then echo interactive; fi, based on this answer
However it is not a login shell, based on executing shopt -q login_shell && echo 'Login shell' || echo 'Not login shell', based on this answer. This means that it would normally source $HOME/.bashrc. Unfortunately I keep my bashrc in a different location and source it from my .bash_profile, so it isn't being picked up.
See my answer below for my current workaround and information about why it's less than ideal.
Add this to your ~/.vimrc:
set shell=bash\ -l
When invoked with -l (--login), bash reads your ~/.profile at startup (among other files) and thus everything sourced from there.
When invoked with -i (--interactive), bash reads your ~/.bashrc at startup (among other files) and thus everything sourced from there.
$ man bash or :h shell and :h shellcmdflag for more info.
Taken from: https://stackoverflow.com/a/9092644/1071756
I set up a mapping to open a new terminal:
nnoremap <leader>z :new<CR>:terminal<CR>
To source my .bash_profile, I changed it to this:
nnoremap <leader>z :new<CR>:terminal<CR>source $HOME/.bash_profile<CR>c<CR>
The problem with this solution is that it breaks if you try to open a terminal buffer in any way other than with this mapping

mac how to save alias in computer

I created an alias on terminal, such as
alias hw="cd Desktop/2015hw"
but after I closed the terminal and do hw, there is an error message said there is no hw command.
I was wondering if we can save the alias on the computer.
Also, if we have stored the alias on the computer, how would we check it? By check it, I mean like list all alias we have stored.
Thanks.
Add the command to your .bashrc file
echo "alias hw='cd Desktop/2015hw'" >> ~/.bashrc
Keep in mind that your alias will only work when you are in your home (as you are using a relative path)
echo "alias hw='cd ~/Desktop/2015hw'" >> ~/.bashrc
Execute the saved alias by sourcing the file
source ~/.bashrc
When making an alias with alias, the alias is only valid until you close the terminal window. If you open another terminal window, the alias will no longer be present.
You can make an alias be valid for all terminal windows by placing it in your ~/.bash_profile or ~/.bashrc files (bash is the name of the terminal which Mac OS X ships with by default.)
Since Mac OS X does not load .bashrc by default, I would actually do:
echo "alias hw='cd ~/Desktop/2015hw'" >> ~/.bash_profile
When you want to delete the alias, you can open your ~/.bash_profile or ~/.bashrc files and delete it manually.
To open them, do open -a TextEdit ~/.bash_profile or open -a TextEdit ~/.bashrc
Finally, to list all your current aliases, simply type alias in Terminal.
your can start with type
ls -la
and then if you use bash typing
nano .bash_profile
or if you use zsh typing
nano .zshrc
then make alias save with ctrl + o
for activating
use
source ~/.bash_profile or
source ~/.zsh

terminal vim not loading .zshrc

My terminal vim configuration is not loading the ~/.zshrc. Zsh is the environment login shell. What is the proper configuration for this situation?
Here are some measures I've already taken and since removed:
set shell=zsh
(uses zsh as shell but doesn't source the rc)
set shellcmdflag=-ci
(all output suspended)
cat ~/.zshenv
$ source ~/.zshrc
(many errors when opening vim)
The accepted answer doesn't work as expected. The actual solution should be putting the aliases and other ~/.zshrc content into ~/.zshenv. The only thing needed in ~/.vimrc is set shell=zsh without any flags.
From the manual:
Commands are first read from /etc/zshenv; this cannot be overridden.
[...]
Commands are then read from $ZDOTDIR/.zshenv. If the shell is a
login shell, commands are read from /etc/zprofile and then
$ZDOTDIR/.zprofile. Then, if the shell is interactive,
commands are read from /etc/zshrc and then $ZDOTDIR/.zshrc. Finally,
if the shell is a login shell, /etc/zlogin and $ZDOTDIR/.zlogin are
read.
From what I understand,
set shell=zsh\ -i
should work.
I found an handy solution. As the only thing I really need is all my aliases, I added a function to my ~/.zshrc file:
function zshalias()
{
grep "^alias" ~/.zshrc > ~/.zshenv
}
Then execute source ~/.zshrc && zshalias.
In your vimrc you only need:
shell=zsh
Everything then works perfectly with no suspended tty output!

Resources