Terminal shows "? - terminal

I was editing .bash_profile and after I saved it ,terminal shows nothing.How can I reset it to previous mode.This is how it looks now.
I changed the value of PS1 variable.I don't have any knowledge about terminal.Please help.

The PS1 environment variable defines what the bash prompt looks like. The default varies among distros, but is generally something like this:
PS1='\h:\W \u\$ '
The bash manpage has an explanation of PS1 values under the heading "Prompting".
You can apply PS1 values to your current terminal session by pressing Control+C several times, then pasting in the line of code above and pressing return or enter. That should get your environment behaving normally long enough to edit your bash profile unless something else is wrong.
If something else is wrong with your profile, and bash is completely broken, you can temporarily use a different shell (one that doesn't care about your bash_profile) with the "New Command..." option in Terminal.app's file menu. When prompted for a command, enter /bin/zsh. You should then get a usable terminal window which you can use to edit or move your .bash_profile.

Related

Automatic meaningful titles for GNOME Terminal tabs

(related: Identify gnome-terminal window by tab title)
When I open several tabs in GNOME Terminal (Version 3.44.0 for GNOME 42) on my Ubuntu 22.04, all tabs have the uninformative title "Terminal". I want them to be automatically titled:
the current working directory if it's in shell prompt (e.g. /home/user123/Downloads)
the name of the open process (e.g. vim ~/.bashrc) in case there is an open process.
How can I achieve that? I use bash, but I can switch to zsh if necessary.
From another thread somewhere on the internet:
function termtitle()
{
printf "\033]0;$*\007";
}
I put this in my .bashrc right above my PS1Update(). Then I call it in PS1Update, right after I update the prompt. I call it with a string constructed from some env flags, username, hostname, pwd. You can put almost anything. I throw in $0. I did try using $PS1, but I use color and I think the printf in termtitle() trips over the escape sequences. So I build another string with the same info but w/o color.
Every time the prompt is updated, so it the terminal window/tab title, like if I change dir, ssh to another machine.
Hope this helps!

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.

Remove "Last login" message for new tabs in terminal

After some search about it I created a ~/.hushlogin file and it worked, but only for new windows. Is there a way to make it work for new tabs too?
On Mavericks, creating the empty file ~/.hushlogin removes the line “Last login”, for new windows and new tabs.
On my Mac it works.
Solution
This is running OS X 10.8.3. I haven't tested it on other versions, but so long as Terminal has the above option, then it should work.
In Terminal.app, go to Preferences->Settings and select the profile you're using. Go to the 'Shell' tab and under the 'Startup' heading, check 'Run command:' and enter into the box:
login -fpql your-username /bin/bash
Substitute your-username with your actual Unix username. If you use a shell other than the default bash shell, replace /bin/bash with the full path to that shell (yes, even if you've already set it in Preferences->Startup.)
Make sure 'Run inside shell' is unchecked.
If you have the "Prompt before closing: Only if there are processes other than login shell and:" option selected, add "login" and "bash" to the list of processes it will not prompt for.
Make sure you have a ~/.bashrc file, since this will be the file bash uses on startup from now on rather than ~/.bash_profile. I just have one file reference the other using this method. You also need to be sure it sources /etc/profile.
Explanation
We want to run login with the -q option to tell it to supress the "Last login" message, even in the absence of a .hushlogin file. (As noted above, login will only look in cwd for that file, not your home directory, so you'd need a .hushlogin file in every directory you'd open a shell to, for that method to work.)
The problem is Terminal runs something like login -pfl your-username /bin/bash -c exec -la bash /usr/local/bin/bash when you create a new shell (I'm using homebrew's version of bash, hence the weird bash path at the end,) which lacks the -q flag.
Unfortunately, there's no way to directly change the arguments Terminal uses, so we just trampoline a new login session with login -pfql from Terminal's default login -pfl session. Inelegant, but it works.
We need to have the -q option and the path to bash to keep the "New windows/tabs open with: Same Working Directory" option working. If you don't care about that option, you can remove that flag and argument, and probably avoid the .bashrc stuff above.
you could just add a clear to your .bash_profile
Adding ~/.hushlogin is fine unless you want to open a new tab in the same folder, or open Terminal from Finder on the exact folder, in that case it won't work.
Changing a running command to another login is something I would like to avoid because of the strange unnecessary scheme login -> login -> zsh. You can see it in Activity Monitor, but also it will show up when you are quitting interactive programs (like, python repl) in the message that python, login and zsh are running.
Putting clear in ~/.zshrc is not ideal since on mac it just prints a lot of newlines (and if you scroll back, you'll see them).
The best way that I found up to this point is adding printf '\33c\e[3J' to ~/.zshrc (or in Terminal/Preferences/Profiles/Shell/Startup/Run command with Run inside shell checked). I chose beginning of ~/.zshrc file since startup command is running after it and if the ~/.zshrc file is heavy you can briefly see Last Login message before printf is executed.
This might be OS version dependent. On Terminal 2.3 (on 10.8), touching the file ~/.hushlogin suppresses the 'last login' message for new tabs as well as new windows. That is, it Works For Me.
Just in case it helps to work out what's going on (and in case you don't know), note that the 'last login' message is in principle coming from login(1), and not the shell. Or, more precisely, if a shell is invoked in a particular way (including starting it with the -l option), then bash will "act as if it had been invoked as a login shell" (zsh may have a similar feature, though I can't find it right now). Now, it could be that when Terminanl opens up a new tab in your OS X version, the shell is effectively simulating opening a login shell, and maybe getting this detail wrong. But if you have the 10.8 version of bash/zsh (namely 3.2.48 / 4.3.11), then I don't know what might be amiss.
A simple solution without changing anything related to login would be just to add the clear command in the end of your ~/.bashrc or ~/.zshrc file. It will clear the terminal in initialization from any initialization prints. It works for me very well.
On my MacOS Big Sur 11.1 it works.

Change in the hostname/user in Terminal in Leopard OSX

My terminal previously showed subalcharla$ at the command line.
The terminial is now showing subalcharla#subal-charlas-macbook ~ $.
How do I go back to the original setting?
What is the difference between the two?
How did this get changed without my doing so?
At the end of ~/.profile add the line
export PS1='\u$ '
to get your old prompt back.
To do this you can type
nano ~/.profile
which will bring up a text editor. Press down until you get to the bottom of the file. Hit Enter to create a new line, and paste in
export PS1='\u$ '
Press Control+X to exit the editor and say "yes" when asked if you want to save. Now restart your terminal and your prompt should be restored.
The first prompt you gave shows your username, the second shows your username and hostname. There is no error and the functionality of your bash shell is not changed by changing the prompt.
Something must have changed your PS1 environment variable, maybe a system update or the installation of software. It's probably benign though.
I don't know how it got changed, but it's controlled by some symbol definitions. Use "man bash" in the terminal and search for the section called "PROMPTING". There are symbols named PS1-to-4 that it uses to construct the prompt.

iTerm 2 name length

I am new to OSX and have downloaded iTerm 2 and my name for the command line is superfluously long.
It looks like my-name-Macbook-pro:~ myname$
and I want it to look like ~myname$
Is there an easy way to shorten this name, I have googled as much as I can but can't find an easy solution.
This has nothing to do with OSX or with iterm2. OSX by default uses the bash shell and you just need to set the prompt correctly.
The prompt is set by the variable PS1. See this article on how to set your bash prompt. To achieve what you want, you need the \w and the \u fields. So something like
PS1=\\w:\\u\$
will give you ~:myname$. Play around with the other options and see what you like best. Once you find a setting that you like, enter it in your .bashrc file so that it is loaded every time you login. Also see this question for cool stuff to put in your .bashrc.

Resources