ZSH: Hide computer name in terminal - zsh

How would I hide the computer name in the prompt while using the terminal?
At the moment it shows both username and computer name like so:
It would save some space by removing anwarchoukah#anwars-mbp, seeing as I actually know who I am:)

Try to add export DEFAULT_USER=$USER to your .zshrc file

On MacOS 10.15 Catalina:
Open the file /private/etc/zshrc in a text editor
Locate the comment: # Default prompt
Modify the line that looks like this: PS1="%n#%m~ %& # "
Save the file. You will be prompted to enter a password to save it.
Load a new Terminal window.
For example, you can:
Remove "%n#%m" to get rid of both the username and computer name
Remove "%n" to get rid of the user name
Remove "%m" to get rid of the machine name

step 1. one your .zshrc file by vim .zshrc
step 2. go to end of your file.
Paste this code:
careful indent again your code
prompt_context() {
if [[ "$USER" != "$DEFAULT_USER" || -n "$SSH_CLIENT" ]]; then
prompt_segment black default "%(!.%{%F{yellow}%}.)$USER"
fi
}
EDIT - explaining what this does
This will remove the machine#user name from the prompt IF:
- you are not logged in as the default user
- you are not in an ssh client shell
For most people, not needed, but if you regularly ssh to other machines and have multiple terminals open (eg remote sys admin type work) then this is extremely useful so when you look at your terminal you know which machine and user you are logged in as inside that terminal.
If you don't need/manage that type of complexity then use one of the other answers to just modify your export PROMPT/PS1 value.
* WARNING *
If you are using a custom shell or theme, this might not work and although the prompt will no longer show your computer and username it will keep throwing the error:
prompt_context:2: command not found: prompt_segment
For example, you can see with this (very popular) powerlevel9k it does not work. This is because the Powerlevel9k theme uses it's own magic and you simply add commands to your ~/.zshrc file to get the same result, eg:
POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(context)
More info on that here.

Open up .zshrc, find the line reading export PS1 or maybe export PROMPT.
Remove the variable that is used for the hostname, could be %m or %M.
Zsh on Archlinux wiki
You can use colors and also have a prompt (or some information) on the right side.

I like this approach (on my mac)
put in .zshrc
PS1="%n$ "
The terminal will look like
username$

Just add prompt_context() {} to .zshrc

Unfortunately none of the .zshrc changes worked for me.
Machine : Mac M1, Big Sur 11.4
So this is what worked.
I Navigated to where the ZSH themes were installed, opened my theme, agnoster in TextEdit, and modified the configuration where it chooses what do display, which by default is $username#%m.
Note : %m here is the machine name.
Here is a screenshot of delta. Yellow is what I did, Green is the default setting from github version of agnoster theme.
Voila this worked. Now to me it just displays the machine name, as I intended.
Hope that helps. Many links and SOF posts only made me click that solution.

Set DEFAULT_USER in ~/.zshrc file to your regular username. You can get your exact username value by executing whoami in the terminal.
Something like this:
export DEFAULT_USER=username

If you are using PowerLevel9k theme, there is a variable POWERLEVEL9K_CONTEXT_TEMPLATE that can change the view of your hostname and computer name.
The default option is %n#%m,
%n -> username
%m -> machine name
to hide hostname:
Open the .zshrc file using sudo nano ~/.zshrc
Add the line
POWERLEVEL9K_CONTEXT_TEMPLATE="%n" at the end of .zshrc file
Save the file.

Maybe this will help you [ Open Profile => Shell ]
PS1="~ $: ";clear;

Just add this to your ~/.zshrc file:
export DEFAULT_USER=\`whoami`

Install Oh My Zsh is the easiest solution for me:
https://ohmyz.sh/
One liner install:
sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
Result:

If you're using Powerlevel10k, then you can run p10k configure and configure the output a bit.
My username and computer-name was gone after having gone through it. It feel less hacky, than the other solutions here.

I don't know why I can't find a simplified solution.
So here is the simplified one.
Just jump into the themes directory,
# ~/.oh-my-zsh/themes
Select the theme, as I have been using
# vim agnoster.zsh-theme
Just remove %m to remove and also you can remove the username too.
Once done,
Just run
#zsh
To reflect the changes, Enjoy :)

Thanks to Wes Bos' amazing video series Command Line Power User , I managed to find the answer.. It's pretty simple. Stuff like that is set in the ZSH theme.
Open up .zshrc, change the theme from ZSH_THEME="agnoster" (which is what I was using) to something else.
Wes Bos has made a lovely theme called Cobalt2 which does exactly what I was looking for :)
I've now changed it to ZSH_THEME="cobalt2"

Related

How get zsh prompt of form: [working-directory] # under macOS

In macOS Catalina (10.15.6), I want to use zsh for Terminal sessions. Formerly I had been using the default bash. For bash, I had a .profile containing the line
export PS1="[\u#\h:\w]$ "
which gave a prompt of the form:
[me#myhost:current-dir]$
I want something similar for zsh, but without the user-name#host-name prefix and with # instead of $ for the actual prompt.
In a zsh Terminal session, the command
PROMPT='[%/]%% '
gives the expected prompt, with the current directory enclosed in square brackets.
Of course I don't want to enter that manually each time. Instead, I want to set this in .zprofile. So in .zprofile I included the line
export PROMPT='[%/]%% '
However, that does not work as expected -- the prompt now has the form:
me#myhost current-dir %
Question: How can I get the zsh prompt to have the desired form as follows?
[current-dir] %
Just add the following export to ~/.zshrc, otherwise it won't work.
export PROMPT='[%1~] %%'
That will give you the following, my directory name is test-workflow-branch-only
[test-workflow-branch-only] %
NOTE: This will give you [~] % when in ~/ directory so don't be alarmed when you see that
UPDATE - per comment questions
We add it to ~/.zshrc as this file gets sourced in all interactive shell configurations. The file ~/.zprofile are for commands that we want to execute when we log in, therefore a non-login shell won't source this file.
Thanks for info from Edward Romero. My critique of answer is that it contains four wasted characters, '[',']',' ','%'. Using instead PROMPT='%d>' yields the nice clear absolute path, something like this:
/Users/myuser/test-workflow-branch-only>
In any case, nice to get this headache behind me, and begin reaping the wonderful benefits of using zsh, whatever they may be.

What does terminal / hyperterm display in the tab title? Is it possible to customize this?

I'm confused what data is being shown in my terminal tabs. The first part, epzio is obviously my username. But the second part, #C02S60BBG8WP, I have no idea.
Note: I use zsh combined with oh-my-zsh for my shell.
Is it possible to customize what is shown in the tab titles? I'd like to remove the epzio#C02S60BBG8WP part altogether and just display the current working directory.
Also, why do my tab titles turn blue for no apparent reason? I thought this was only supposed to happen when you had a specific process such as node running. As you can see in the screenshot below, the tic-tac-toe tab has turned blue even though nothing is happening in that tab.
Update: It seems that the solution might involve making changes to ~/.oh-my-zsh/lib/termsupport.zsh, but I'm not sure if these would get overridden when oh-my-zsh updates itself.
C02S60BBG8WP is probably your hostname; check by typing hostname.
You can change the terminal title by printing an escape sequence like this:
echo -en "\033]0;New terminal title\a"
So this should change the title to your current working directory, $PWD, substituted by a single ~ if you're in $HOME:
echo -en "\033]0;${PWD/#$HOME/~}\007"
If that doesn't work, it's probably being overridden immediately afterwards by a command that is automatically invoked by your shell. In bash, this would be PROMPT_COMMAND which on my system looks like this:
$ echo $PROMPT_COMMAND
__vte_prompt_command; printf "\033]0;%s#%s:%s\007" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/\~}"
The zsh equivalent seems to be to define a precmd hook:
precmd() { echo -en "\033]0;${PWD/#$HOME/~}\007" }
To make that permanent, you can just put it your .zshrc.

terminator - programmatic way to set window titles? [duplicate]

Is there way to set terminator (Version: 0.95ppa1) title of tabs to a different string via bash command-line (CLI)?
I plan to use this feature with AutoKey and I can open multiple machines at same time and set title to Name of the machine its connected to.
ORIG=$PS1
TITLE="\e]2;\"This is just crazy enough to work\"\a"
PS1=${ORIG}${TITLE}
Resets title to
"This is just crazy enough to work"
This should apply to all xterm-style terminal emulators.
From the Terminator man pages,
Ctrl+Alt+W
Rename window title.
Ctrl+Alt+A
Rename tab title.
Ctrl+Alt+X
Rename terminal title.
You can also launch a new instance with
$ terminator --title [title]
Add follwing in your .bashrc file by editing it using vim ~/.bashrc and use set_title to rename your tab:
set_title()
{
ORIG=$PS1
TITLE="\e]2;$*\a"
PS1=${ORIG}${TITLE}
}
run source ~/.bashrc command after editing your .bashrc file
Ex.: set_title newtab will rename your current tab to newtab
working Properly in Gnome3.14 terminal and terminator 0.97
On Terminator 1.91-6 double click terminal title enables edition
PS1 does not need to be set. Credit for this function goes to geirha on freenode #bash
set_title() { printf '\e]2;%s\a' "$*"; }
This seems to work for me. I'm using BASH on Crunchbang (Debian derived)
$ echo -en '\e]0;echo works too\a'
With Terminator 0.96 and GNU bash 4.2.25 the printf suggestion above worked for me, but I had to tweak it slightly to make it into a function that would just work for me in a new Terminator tab. I added this to the end of my ~/.bashrc file:
set_title() { printf "\e]2;$*\a"; }
The key was placing the \a at the end of the quoted string. Then when opening a new tab in Terminator I can use this like so:
set_title "My new tab title"
Apologies to those who already stated the essentials of this answer, but since I found that I had to make slight changes to get it to work for me, this my also benefit someone else.
Try add PROMPT_COMMAND='echo -en "\033]0; $("pwd") \a"' in your .bashrc
For terminator 0.98 (Ubuntu 16.04 MATE), right clicking the title enables renaming it.
Right click on terminator and choose preferences from the drop-down menu. Choose profiles tab and enable "show title bar" option. It works!!
Tip: You can actually rename each terminator window!!

Is it possible to keep the output of less on the screen after quitting?

I'm using oh-my-zsh which pipes the output of some functions like git diff and git log into less, whilst this is great for reading the output in the terminal. If I need to refer back to it it isn't possible after quitting with :q
Is there an option to preserve the current view on the file in my terminal after quitting?
Secondly, If there is an option where would I need to edit my oh-my-zsh config to ensure anything piped to less passes this option?
To prevent less from clearing the screen on exit you can start it with the option -X:
less -X FILE
If you want to pass this option automatically to every instance of less, you can set the LESS environment variable accordingly in your ~/.zshrc:
export LESS="-X"
Note:
If your shell has syntax coloring enabled, the -X option will cause your less output to display those color change escape sequences as inline ESC text.
This can be fixed by also passing the raw-control-chars display option, -r. For example:
export LESS="-Xr"
This also includes instances where less is started by another program, for example man. If you want to disable this option for a single command, you can just prepend LESS=. For example
LESS= man less
For Git specifically, this can be handled with the following
git config --global color.ui true
git config --global core.pager 'less -Xr'

Set the terminal tab title as prompt name in unix

Lets say, the prompt is as below
run_scripts >
How to set that terminal tab title same as prompt
i.e Terminal tab tile also should be
run_scripts>
So that terminal title should dynamically update when the prompt changes.
Many terminals emulators are able to understand the special escaping : "\033]0;foo\007".
I know its a old post but i saw it today :
Here is the answer:
title `pwd`
if title command does not works in your shell then:
write a shell script with follwing contents (filename = title)
#!/usr/bin/tcsh -f
echo "^[]2;$1^G^[]1;$1^G"
then:
chmod +x title (give this script executable permission)
type:
title `pwd` <enter>
This single line command will change the title of the tab.
Simply run the command from terminal tab which title need to change-
PS1=$PS1"\[\e]0;My_Tab_Name\a\]"
Some Info:
The PS1 is a primary prompt variable which holds the characters displayed at the terminal prompt. You can set it whatever you want. However the above command will make it only work for current terminal session. Once you close the terminal and opena new one, it'll be the default one.
To make it permenant edit the PS1 variable in ~/.bashrc or ~/.zshrc file.
Benefits-
It help us to easily navigate over the tabs.

Resources