Changing the VSCode integrated shell's prompt on MacOS X - bash

Having just installed VScode I have noticed as it uses Bash by default on OSX, with the shell's default prompt of bash-3.2$; consequently, I cannot see the current working directory. It means having to type 'pwd' and 'ls' quite frequently which is obvious quite tedious.
I have tried changing the default shell in the settings to
"terminal.integrated.shell.osx": "/Applications/Utilities/Terminal.app"
or
"terminal.integrated.shell.osx": "/Applications/iTerm.app"
This doesn't seem to work, have I made a mistake here?
I would also like to know if I am limited to bash, can I configure it to display the working directory instead of simply bash-3.2$ ?
See this screenshot of how the VSCode integrated terminal looks by default
Thanks in advance!

I use Ubuntu, and only add the following lines to the end of ~/.bashrc:
if [ "$TERM_PROGRAM" = "vscode" ]; then
PS1='\$ '
fi
Try it and let me know if it works on your OS.

You can set your prompt to contain the current working directory by defining PS1 as follows:
PS1="\w $"
The $ is just some visual sugar. There all manner of things you can have your prompt display. Put the definition in your ~/.bashrc or ~/.profile for it to be set when you login.
Check out the Controlling the Prompt section of the GNU Bash manual for details.
If you are not accustomed to editing your bash init files you can do it with Visual Studio Code by going to View->Command Palette and execute the following command (one-time only):
Install 'Code' command in path
Then open the integrated terminal and type the following:
code ~/.bashrc
Then add the PS1 definition to the bottom of that file.

Related

Why VS Code on macOS suggests add $PATH to zprofile instead of zshrc?

I am studying zsh on macOS and I find when adding software to $PATH you are usually supposed to write them in .zshrc (zsh). However, Visual Studio Code documentation suggests
To add VS Code to your path, to do so run the following commands:
cat << EOF >> ~/.zprofile
# Add Visual Studio Code (code)
export PATH="\$PATH:/Applications/Visual Studio Code.app/Contents/Resources/app/bin"
EOF
I find zprofile set the environment for login shells while zshrc sets the environment for interactive shells. However, I cannot really see differences between two methods on macOS terminal since each terminal tab is a login session on macOS by default.
Is there any reason why Visual Studio Code on macOS suggests add $PATH to zprofile instead of zshrc and what will be the good practice?
If you read this and this answer as well as the man page (man zsh under STARTUP/SHUTDOWN FILES) you might come to the following conclusion:
You are using MacOS, therefore every zsh you open is a login shell, that way you made sure, that .zprofile will always be read. So if you append your $PATH in your .zprofile and and use zsh as a non interactive-shell you will be able to access your appended $PATH. This might happen in a script of if other programs try to use a program that you added to your $PATH.
Now on the other hand if you added VSCode to your .zshrc it will always be available in an interactive shell. Which mean if you use VSCode 'manually' you will always have it at your disposal in zsh. But other programs/scripts might not find it.
If you set environment variables in .zshrc or .zshenv it can be awkward if you want to use a different value. Say you install a different version of vscode in a different directory. Any new shell will reset the environment variable instead of inheriting it. As environment variables are inherited, there is no need to reset them for every new shell. So .zprofile is often a better choice - the variable is set on first login and inherited but can be changed for a shell and its children. Another option is to set them from .zshenv but use a condition around them such as if [[ ! -o privileged && -o rcs && $SHLVL = 1 ]]; then

Backspace Not Working on VS Code's Bash Terminal

So, I'm new to VS code just installed it and I'm having an issue. The backspace doesn't work in the bash terminal of VS code. The backspace works on the cmd terminal of VS code though.
Not working means when I tap the backspace it prints a white space in the terminal.
Also the backspace key works perfectly fine on Git Bash
I tried googling but didn't find what I was looking for!
Please help.
You can check your TERM by running echo $TERM, but before you do this I suggest you restart your vscode as you have already set the TERM by using the command. I think your TERM was set to something incompatible.
export TERM=xterm will set the emulator to xterm.
Most application will expect that you already have the emulator set to something compatible such as xterm or linux.
For more information regarding what export TERM=xterm does you can check the following answer
Now, if you restart your vscode or terminal inside the vscode, TERM will reset to default and the problem will occur again. Therefore I suggest you add the export TERM=xterm in bash_profile or bashrc.
To add it to bashrc, type following in your terminal:
echo export TERM=xterm >> ~/.bashrc
source ~/.bashrc
go to command palette and select open shortcut key words go to this file and delete all data in this file

How to customize the shell prompt in the VS Code terminal on macOS

I'm trying to customize my integrated terminal shell prompt in vscode, and was successfully able to change the theme (so that I can see my current working directory and branch I'm on), however now I want to remove the first portion 'anhlucci#Anhs-MacBook-Pro'. How do I do that?
I use Ubuntu with bash, and I only add the following lines to the end of ~/.bashrc:
if [ "$TERM_PROGRAM" = "vscode" ]; then
PS1='\[\033[01;34m\]\w\[\033[00m\]\$ '
fi
I found that vscode sets TERM_PROGRAM environment variable, and then use it to modify PS1 only to vscode.
The command line prompt is not dictated by Visual Studio Code, but by bash. The prompt is dictated by the PS1 variable in bash. You can view it as follows:
echo "$PS1"
To give you an idea of how that works, this is how my prompt looks like:
[hongli#Leticia Projects]$
My $PS1 looks like this:
[\u#\h \W]\$
Things like \u and \h are formatters that are substituted with a specific value. \u is for the current username, \h is for the hostname.
I'm guessing your $PS1 contains something like \u#\h in the beginning. Remove that and reset the PS1 variable, for example like this:
PS1='[\W]\$ '
Finally, you need to persist this in your bash configuration file so that the next time you start your shell it will show that same prompt. The bash config file is typically ~/.bashrc or ~/.profile depending on the exact Linux distribution you use. Make sure you set $PS1 in there.

Ubuntu terminal: strange symbols instead of my username

I have a strange problem with my Ubuntu terminal: when I open it instead of seeing my username I see this:
32m]u#h[033[00m]:[033[01: command not found
31m]w[033[00m]$: command not found
’[033[01
Strangely enough bash commands work normally, the terminal just does not show my username or the current path. I googled, but was unable to find any answers. The most recent changes I made on my computer involved installing RVM (Ruby Version Manager) and manually editing the PATH to add RVM in files: .bash_profile, .profile and .bashrc, but after that it all worked normally, so I am not really sure that could be the reason.
It looks like you've edited the PS1 variable by mistake when modifying the ~/.bashrc, which controls the prompt layout. You'll need to edit your ~/.bashrc and replace it with the following default.
if [ "$color_prompt" = yes ]; then
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u#\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
else
PS1='${debian_chroot:+($debian_chroot)}\u#\h:\w\$ '
fi
For more information on other changes you can make to your prompt have a look at Customising Bash Prompt. The change wouldn't appear immediately after modifying the file because bash doesn't reload it's configuration once you've changed the file automatically. You'll either need to exit the shell and start a new one or reload the configuration using
. ~/.bashrc
The . at the begining is needed, it's shorthand for the source command.

How to run ~/.bash_profile in mac terminal

So I'm installing some things for coding and personal usage, and I need to run this in the terminal (I'm on Mac if you didn't read the title).
~/.bash_profile
It just says permission denied, Im running OSX 10.8.4 Mountain Lion. How do I bypass this?
On MacOS: add source ~/.bash_profile to the end of ~/.zshrc.
Then this profile will be in effect when you open zsh.
You would never want to run that, but you may want to source it.
. ~/.bash_profile
source ~/.bash_profile
both should work. But this is an odd request, because that file should be sourced automatically when you start bash, unless you're explicitly starting it non-interactively. From the man page:
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.
If you change .bash_profile, it only applies to new Terminal sessions.
To apply it to an existing session, run source ~/.bash_profile. You can run any Bash script this way - think of executing source as the same as typing commands in the Terminal window (from the specified script).
More info: How to reload .bash_profile from the command line?
Bonus: You can make environment variables available to OSX applications - not just the current Bash session but apps like Visual Studio Code or IntelliJ - using launchctl setenv GOPATH "${GOPATH:-}"
As #kojiro said, you don't want to "run" this file. Source it as he says. It should get "sourced" at startup. Sourcing just means running every line in the file, including the one you want to get run. If you want to make sure a folder is in a certain path environment variable (as it seems you want from one of your comments on another solution), execute
$ echo $PATH
At the command line. If you want to check that your ~/.bash_profile is being sourced, either at startup as it should be, or when you source it manually, enter the following line into your ~/.bash_profile file:
$ echo "Hello I'm running stuff in the ~/.bash_profile!"
No need to start, it would automatically executed while you startup your mac terminal / bash. Whenever you do a change, you may need to restart the terminal.
~ is the default path for .bash_profile
I was getting this error on zsh(mac os Big Sur 11.3), This is how i solved this :-
Go to Terminal.
cd /users/<yourusername>
Once you reach here issue a command :
ls -al
You will see a lot of files and one specific file .zprofile. This is your user profile. We need to edit this.
After this we need to edit the file. Issue the below command :
nano .zprofile
Once you issue this command file will be opened for edit. Add the path details for maven.
M2_PATH="/Users//code/apache-maven-3.8.1/bin" //add your path of maven diretory
PATH="${PATH}:${M2_PATH}"
export PATH
press ctrl + X and save the file.
Issue command after saving the file :
source .zprofile
Once done, you will be able to run the mvn command.
If the problem is that you are not seeing your changes to the file take effect, just open a new terminal window, and it will be "sourced". You will be able to use the proper PATH etc with each subsequent terminal window.

Resources