I had the same problem and had also to manually execute
source ~/.zshrc
And long story short I solved it with a symlink by executing
ln -s ../.zshrc $ZDOTDIR/.zshrc
Explanation how I came up with this solution and why I prefer this solution over two other possible solutions that are working and have been proposed above:
Following the documentation in
https://code.visualstudio.com/docs/terminal/shell-integration
I've added the following line at the end of my ~/.zshrc
[[ "$TERM_PROGRAM" == "vscode" ]] && . "$(code --locate-shell-integration-path zsh)"
which sources a script whenever VSCode gets started.
But that alone didn't fix that particular problem.
When looking at that script by executing
less "$(code --locate-shell-integration-path zsh)"
I found following line that caused this problem
. $USER_ZDOTDIR/.zshrc
with $USER_ZDOTDIR
pointing to ~/.zsh
The variable $USER_ZDOTDIR
even sets the variable $ZDOTDIR
So setting
USER_ZDOTDIR=${HOME}
would have solved it, but since $ZDOTDIR
is very crucial and pointing it to ${HOME}
could break things by missing the ${HOME}/.zsh/.zshenv
with $ZDOTDIR/.zshenv
pointing to it.
For example, in my $ZDOTDIR/.zshenv
scripts are sourced which paths are added to the $PATH
variable in (e.g. $HOME/.cargo/bin
)
To have a fast look on that script I recommend executing
cat "$(code --locate-shell-integration-path zsh)" | grep -E '\bUSER_ZDOTDIR\b'
results in (Attention: these are not consecutive lines)
ZDOTDIR=$USER_ZDOTDIR
HISTFILE=$USER_ZDOTDIR/.zsh_history
if [[ $options[norcs] = off && -f $USER_ZDOTDIR/.zshrc ]]; then
ZDOTDIR=$USER_ZDOTDIR
. $USER_ZDOTDIR/.zshrc
if [[ $options[login] = off && $USER_ZDOTDIR != $VSCODE_ZDOTDIR ]]; then
ZDOTDIR=$USER_ZDOTDIR
(Attention: these are not consecutive lines) with
cat "$(code --locate-shell-integration-path zsh)" | grep -E '\bUSER_ZDOTDIR\b' -A 2 -B 2
you get these lines with some context! And by increasing the numbers at the end you can display more context
But you can see the line causing the problem
. $USER_ZDOTDIR/.zshrc
and
cat "$(code --locate-shell-integration-path zsh)" | grep -E '\bZDOTDIR\b' # -A 1 -B 1
you can have a look concerning the effects of $ZDOTDIR
.
Remove the #
and change the number after -A
and -B
to include more or less context.
You also don't have to be concerned about your $HISTFILE
in spite of the line
HISTFILE=$USER_ZDOTDIR/.zsh_history
which stays the same with this solution as long it's set in your ~/.zshrc
file and overwrites this assignment.
Off-Topic but potentially useful
While searching for the solution which fits the most I found out that you could have a different history files for VSCode only by adding also
[[ "$TERM_PROGRAM" == "vscode" ]] && HISTFILE=${ZDOTDIR}/.vscode_history
which can be very useful for shell-intensive projects like npm, yarn etc. are.
But the commands have to be executed inside the VSCode-Shell to end up in said history file.
Since I have created⁽¹⁾ two aliases which are useful for myself and possible for others, too
You can also have history files which are only assigned to a single project.
All the following extra history files are contained in the $ZDOTDIR
folder
This alias temporarily sets $HISTFILE
according to the folder you're in and should only executed in the project's root folder
alias set-project-histfile='eval "[[ \"\$TERM_PROGRAM\" == \"vscode\" ]] && HISTFILE=\${ZDOTDIR}/\$(echo \$PWD | rev | cut -d '/' -f1 | rev)_history"'
while the following alias writes the $HISTFILE
assignment which is like above also only in effect when using the VSCode terminal at the end of $HOME/.zshrc making it permanent until this line is deleted manually
alias set-project-histfile-permanent='echo "[[ \"\$TERM_PROGRAM\" == \"vscode\" ]] && HISTFILE=\${ZDOTDIR}/\$(echo \$PWD | rev | cut -d '/' -f1 | rev)_history" >> ${HOME}/.zshrc'
Keep in mind that this $HISTFILE
assignment has the effect that each project you're working with VSCode has its own history file in the $ZDOTDIR
folder.
Examples:
$ZDOTDI/projectFoo_history
$ZDOTDIR/projectBar_history
$ZDOTDIR/projectFoobar_history
$ZDOTDIR/Fugazi_history
…
$HOME/.zsh/Fugazi_history
and the command you execute in VSCode's terminal end up in these history file instead of the usual one.
zsh -x
, to better see what zsh is actually doing on startup. What happens if you try this? Another point to consider: Do you accidentally haveZDOTDIR
set (i.e. do you get something when you do aecho $ZDOTDIR
?). – GaeaZDOTDIR
was the point, Thanks @Gaea – Mcgaha