Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to enable username and hostname in the default theme? #5686

Closed
ssbarnea opened this issue Dec 2, 2016 · 35 comments
Closed

how to enable username and hostname in the default theme? #5686

ssbarnea opened this issue Dec 2, 2016 · 35 comments
Labels
Area: theme Issue or PR related to a theme Feature New feature or request Support Request for support

Comments

@ssbarnea
Copy link

ssbarnea commented Dec 2, 2016

I do want to stick with the default theme but I want to just be able to see username and hostname in order to avoid running commands on the wrong host.

How can I do this?

@sounak98
Copy link

sounak98 commented Dec 4, 2016

You'll have to modify the theme settings. Go and open the theme(s) directory and select the theme you want to use. Open it in any of the text editors available. And make the necessary changes.
If you tell me which theme you use particularly I can help you @ssbarnea.

@ssbarnea
Copy link
Author

ssbarnea commented Dec 5, 2016

@sounak98 I would like to stick to the default theme because I expect is the most mature one and least likely to have bugs. The only thing that I don't like about it is the missing username and hostname.

I find quite overwhelming the number of themes and the fact that there is no matrix like comparison between them makes any decision even harder.

I guess that less-is-more concept does not apply to oh-my-zsh themes ;)

@sounak98
Copy link

Sorry for the late reply. Did you get it already? If not I'll tell you the way to do it. @ssbarnea

@sounak98
Copy link

PROMPT='%(!.%{%F{yellow}%}.)$USER @ %{$fg[white]%}%M %{$fg_bold[red]%}➜ %{$fg_bold[green]%}%p %{$fg[cyan]%}%c %{$fg_bold[blue]%}$(git_prompt_info)%{$fg_bold[blue]%} % %{$reset_color%}'

@sounak98
Copy link

Change the value of PROMPT to this in the file /themes/robbyrussell.zsh-theme

@mcornella
Copy link
Member

Copy the robbyrussell.zsh-theme to $ZSH_CUSTOM/themes/robbyrussell.zsh-theme. Then edit the $PROMPT as in #5686 (comment).

More info about customization.

@mcornella mcornella added Feature New feature or request Area: theme Issue or PR related to a theme question labels Dec 15, 2016
@ssbarnea
Copy link
Author

Am I wrong or all these solutions to break the ability to get updates of oh-my-zsh? If I clone it means that I am stuck on that specific version.

Maybe unrelated to this ticket but I found quite weird not to be able to compare themes by their features like:

  • display error code of last command
  • single line / multiple lines
  • display username
  • display group
  • distinguish between root and non root users
  • display current time
  • display git branch
  • included in core

My impression is that could be achieved by creating a google spreadsheet with main features as columns and themes as rows, allowing us to filter the themes with an auto-filter.

@ssbarnea
Copy link
Author

@sounak98 it seems that your example alternative prompt value contains at least one important bug: the prompt arrow is always red but on original default theme the colour is based on the result of the last command (green = success, red means != 0 return code).

Ideally the user/hostname should be just a parameter to configure on ~/.zshrc file.

@ssbarnea
Copy link
Author

ssbarnea commented Dec 15, 2016

In fact I finally found the solution for the problem, quite surprise how easy it was:

Edit ~/.zshrc file and add this to the end of the file:

PROMPT="$fg[cyan]%}$USER@%{$fg[blue]%}%m ${PROMPT}"

This will add a user@host prefix before the default prompt from the theme, so it will inherit the theme custom prompt without overriding it.

The only thing I was not able to do was to find out how to produce the dark-grey colour so I ended up using cyan and blue colours. The default colour is too bright and I don't want to be distracted by something too shiny.

@sounak98
Copy link

Yeah @ssbarnea it's true that you won't be able to update after this. Yours is a better method in that sense. Cheers. 😄

@ssbarnea
Copy link
Author

ssbarnea commented Jan 5, 2017

@sounak98 It seems that this bug should be reopened because I found a serious bug related to the workaround.

PROMPT="$fg[cyan]%}$USER@%{$fg[blue]%}%m ${PROMPT}"

If you add this to .zshrc, it will give you the impression that it works but it breaks the auto-completer in a very ugly way. You need to test it yourself to see it as it seems quite hard to explain.

  • Enable the PATH alternation
  • start a new shell
  • run a command like curl -V
  • now type cu<tab>
  • you will observed that the completion happens at the wrong position and you end-up with a messed/confusing command line prompt.

@ssbarnea
Copy link
Author

ssbarnea commented Mar 19, 2017

I still don't consider this issue as resolved as long we do not have an option to deploy/install oh-my-zsh using a one-liner and end-up with the hostname in prompt. No further changes.

Not having the hostname as part of the prompt is recipe for disasters.

Did anyone create a way to compare themes based on features?

@cduque89
Copy link

cduque89 commented Apr 3, 2017

Hi.

You can create under the ~/.oh-my-zsh/custom a new folder named themes.

Copy the base theme you want to add the user@host from the ~/.oh-my-zsh/themes to: ~/.oh-my-zsh/custom/themes/mycustomtheme.zsh-theme (you can change mycustomtheme to whatever you want).

Then add the PROMPT="$fg[cyan]%}$USER@%{$fg[blue]%}%m ${PROMPT}" line into your theme file.

Finally edit the ~/.zshrc and change ZSH_THEME="mycustomtheme".

It works fine in this way and you can update without problems since the custom folder is in the .gitignore file.

@jontro
Copy link

jontro commented Apr 19, 2017

It would be nice if this could be a configurable in the default theme. Copying the theme and modifying it seems like an awkward work around

@cduque89
Copy link

When you copy the theme to your custom folder you are creating your own theme, but instead of making it from scratch you started with a base. For me is a good solution. What I do is put all my custom settings in the custom folder, doesn't matter if it is a change to the theme, to the PATH variable, an alias, etc..

In this way I sync my custom folder to my own git repository and when I change machines I only need to pull my custom settings and everything is with the same configuration. For example my personal laptop with Xubuntu and my work Mac Book have the same custom settings and when I need to add any new settings it will be synced on both machines.

Of course you can also have some settings only in one of the machines it is all a question of your personal custom configuration.

@koeniglorenz
Copy link

koeniglorenz commented Apr 30, 2017

Hello,

i came across the same auto-complete problem when i tried the solution proposed by @ssbarnea. But it appears to me that this behavior is due to a missing opening-bracket in the line that was added to .zshrc.
I tried to fix this by altering that line:

PROMPT="%{$fg[white]%}%n@%{$fg[green]%}%m%{$reset_color%} ${PROMPT}"

This gives me the user@host in front of the prompt and the auto-complete feature seems to be working fine.

@Shea690901
Copy link

Would´nt it be possible to use zstyle to switch on/off what´s displayed within the prompt?
That way one wouödn´t need to change a theme but just set the corresponding styles...

@ssbarnea
Copy link
Author

Can we please reopen this? I want to deploy ohmyzsh to all hosts for root account too and I am pretty people will scream only because of this: where is my hostname/username, nothing else.

If we need to customize things to enable these it means that we degrade the user experience (UX) because the best user experience is that that does just-works and that does not require "investigation". Especially for system where management is shared this matters.

@Smithx10
Copy link

I just took a look at this and added this in my .zshrc

PROMPT='${${PROMPT}:+"%F{9}%n%f%F{7}@%f%F{3}%m%f "}%F{4}${_prompt_sorin_pwd}%(!. %B%F{1}#%f%b.)${editor_info[keymap]}'

It's been working for the passed hour or so.

@dketterer
Copy link

%n is the username
%m is the hostname

twpayne added a commit to twpayne/dotfiles that referenced this issue Jan 22, 2019
@mshahat
Copy link

mshahat commented Feb 8, 2019

Hi Guys,

cp robbyrussell.zsh-theme' to $ZSH_CUSTOM/themes/`
and then use this

local ret_status="%(?:%{$fg_bold[green]%}➜ :%{$fg_bold[red]%}➜ )"
PROMPT='${ret_status} %{$fg_bold[magenta]%}%n %{$fg[cyan]%}%~%{$reset_color%} $(git_prompt_info)'

ZSH_THEME_GIT_PROMPT_PREFIX="%{$fg_bold[blue]%}git:(%{$fg[red]%}"
ZSH_THEME_GIT_PROMPT_SUFFIX="%{$reset_color%} "
ZSH_THEME_GIT_PROMPT_DIRTY="%{$fg[blue]%}) %{$fg[yellow]%}✗"
ZSH_THEME_GIT_PROMPT_CLEAN="%{$fg[blue]%})"

this adds the username@servername , and also shows full path or relative path to ~

@mcornella mcornella added Support Request for support and removed Type: question labels Mar 25, 2019
@yanzou
Copy link

yanzou commented Apr 24, 2019

Hi Guys,

cp robbyrussell.zsh-theme' to $ZSH_CUSTOM/themes/`
and then use this

local ret_status="%(?:%{$fg_bold[green]%}➜ :%{$fg_bold[red]%}➜ )"
PROMPT='${ret_status} %{$fg_bold[magenta]%}%n %{$fg[cyan]%}%~%{$reset_color%} $(git_prompt_info)'

ZSH_THEME_GIT_PROMPT_PREFIX="%{$fg_bold[blue]%}git:(%{$fg[red]%}"
ZSH_THEME_GIT_PROMPT_SUFFIX="%{$reset_color%} "
ZSH_THEME_GIT_PROMPT_DIRTY="%{$fg[blue]%}) %{$fg[yellow]%}✗"
ZSH_THEME_GIT_PROMPT_CLEAN="%{$fg[blue]%})"

this adds the username@servername , and also shows full path or relative path to ~

this works for me

@blinky-z
Copy link

blinky-z commented Jul 18, 2019

Hi Guys,

cp robbyrussell.zsh-theme' to $ZSH_CUSTOM/themes/`
and then use this

local ret_status="%(?:%{$fg_bold[green]%}➜ :%{$fg_bold[red]%}➜ )"
PROMPT='${ret_status} %{$fg_bold[magenta]%}%n %{$fg[cyan]%}%~%{$reset_color%} $(git_prompt_info)'

ZSH_THEME_GIT_PROMPT_PREFIX="%{$fg_bold[blue]%}git:(%{$fg[red]%}"
ZSH_THEME_GIT_PROMPT_SUFFIX="%{$reset_color%} "
ZSH_THEME_GIT_PROMPT_DIRTY="%{$fg[blue]%}) %{$fg[yellow]%}✗"
ZSH_THEME_GIT_PROMPT_CLEAN="%{$fg[blue]%})"

this adds the username@servername , and also shows full path or relative path to ~

It missing '@' and hostname. To anyone who need also hostname please use this:

local ret_status="%(?:%{$fg_bold[green]%}➜ :%{$fg_bold[red]%}➜ )"
PROMPT='${ret_status} %{$fg_bold[magenta]%}%n%{$reset_color%}%{$FG[146]%}@%{$reset_color%}%{$fg_bold[blue]%}%m%{$reset_color%}  %{$fg_bold[cyan]%}%~%{$reset_color%} $(git_prompt_info)'

ZSH_THEME_GIT_PROMPT_PREFIX="%{$fg_bold[blue]%}git:(%{$fg[red]%}"
ZSH_THEME_GIT_PROMPT_SUFFIX="%{$reset_color%} "
ZSH_THEME_GIT_PROMPT_DIRTY="%{$fg[blue]%}) %{$fg[yellow]%}✗"
ZSH_THEME_GIT_PROMPT_CLEAN="%{$fg[blue]%})"

@seestonebat
Copy link

I also wanted the username and machine in my prompt.
I found I had to wrap the first section in %{..%}>
If I did not, I got spaces inserted into my prompt as described here - https://stackoverflow.com/questions/28799198/zsh-inserts-extra-spaces-when-performing-searches-and-completion

So my prompt looks like this -
PROMPT="%{$fg[cyan]%}$USER@%{$fg[blue]%}%m%} ${PROMPT}"

@Avamander
Copy link

I'd rather also like auto-updates, yet the ability to display user & machine based on config.

@grissom1
Copy link

There's another useful condition
IF [[ -n $SSH_CONNECTION ]]; then
PROMPT="%{$fg[white]%}%n@%{$fg[green]%}%m%{$reset_color%} ${PROMPT}"
fi
If you only want the prompt for SSH session but not local environment.

@mdmmn378
Copy link

mdmmn378 commented May 2, 2020

Just comment out the prompt_context section the .oh-my-zsh/themes/<your_theme>.zsh-theme file

## Main prompt
build_prompt() {
  RETVAL=$?
  prompt_status
  prompt_virtualenv
  prompt_aws
  # prompt_context
  prompt_dir
  prompt_git
  prompt_bzr
  prompt_hg
  prompt_end
}

PROMPT='%{%f%b%k%}$(build_prompt) '

@hassam-saeed
Copy link

Hi Guys,

cp robbyrussell.zsh-theme' to $ZSH_CUSTOM/themes/`
and then use this

local ret_status="%(?:%{$fg_bold[green]%}➜ :%{$fg_bold[red]%}➜ )"
PROMPT='${ret_status} %{$fg_bold[magenta]%}%n %{$fg[cyan]%}%~%{$reset_color%} $(git_prompt_info)'

ZSH_THEME_GIT_PROMPT_PREFIX="%{$fg_bold[blue]%}git:(%{$fg[red]%}"
ZSH_THEME_GIT_PROMPT_SUFFIX="%{$reset_color%} "
ZSH_THEME_GIT_PROMPT_DIRTY="%{$fg[blue]%}) %{$fg[yellow]%}✗"
ZSH_THEME_GIT_PROMPT_CLEAN="%{$fg[blue]%})"

this adds the username@servername , and also shows full path or relative path to ~

Thank you it works for me

@GithubUser5462
Copy link

GithubUser5462 commented Jan 9, 2021

A quick copy & paste solution if anyone needs it. Adds "user@hostname" at the beginning.

cp "$ZSH/themes/robbyrussell.zsh-theme" "$ZSH_CUSTOM/themes/" && \
echo "\n# My config" >> $ZSH_CUSTOM/themes/robbyrussell.zsh-theme && \
echo 'PROMPT="%{$fg[white]%}%n@%{$fg[green]%}%m%{$reset_color%} ${PROMPT}"' >> $ZSH_CUSTOM/themes/robbyrussell.zsh-theme && \
echo "\nDone\n";

@Jeinzi
Copy link

Jeinzi commented Jan 20, 2021

Unfortunately, none of the suggestions above did what I wanted without bugs. This is my current solution:
PROMPT='%{$fg_bold[white]%}$USER@%{$fg[yellow]%}%m%}%{$fg_bold[cyan]%} %c $(git_prompt_info)%{$reset_color%}'

This looks as follows and, for me, doesn't have any autocompletion bugs:
image

@jaimey
Copy link

jaimey commented Feb 17, 2021

This works for me
https://github.com/romkatv/powerlevel10k/blob/master/README.md#how-do-i-add-username-andor-hostname-to-prompt

@mo-cmyk
Copy link

mo-cmyk commented Nov 30, 2021

can someone help me to also display the device name and not only my name in this custom theme as well?
PROMPT='[%{$fg_bold[white]%}%n%{$reset_color%}@%{$fg_bold[red]%}%{$reset_color%} %{$fg[cyan]%}%c%{$reset_color%} $(git_prompt_info)%{$reset_color%}]$ '

@philipperemy
Copy link

Add those lines at the bottom of your .zshrc:

autoload -U colors && colors
PS1="%{$fg[green]%}%n%{$reset_color%}@%{$fg[cyan]%}${${(%):-%m}} %{$fg[yellow]%}%~ %{$reset_color%}%% "

image

@mikeburgh
Copy link

Fixing the tab completion offset issue as mentioned here: https://stackoverflow.com/questions/11916064/zsh-tab-completion-duplicating-command-name

local ret_status="%(?:%{$fg_bold[green]%}%{%G➜%} :%{$fg_bold[red]%}%{%G➜%} )"
PROMPT='${ret_status} %{$fg_bold[magenta]%}%n%{$reset_color%}%{$FG[146]%}@%{$reset_color%}%{$fg_bold[green]%}%m%{$reset_color%}  %{$fg_bold[cyan]%}%~%{$reset_color%} $(git_prompt_info)'

ZSH_THEME_GIT_PROMPT_PREFIX="%{$fg_bold[blue]%}git:(%{$fg[red]%}"
ZSH_THEME_GIT_PROMPT_SUFFIX="%{$reset_color%} "
ZSH_THEME_GIT_PROMPT_DIRTY="%{$fg[blue]%}) %{$fg[yellow]%}%{%G✗%}"
ZSH_THEME_GIT_PROMPT_CLEAN="%{$fg[blue]%})"

@hnyls2002
Copy link

Yeah, I just found when using SSH to connect the remote machine, the username@hostname will always show. To enable this in your shell's prompt, see where your ZSH theme file defines the $PROMPT env variable and delete the condition SSH_TTY; it works!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: theme Issue or PR related to a theme Feature New feature or request Support Request for support
Projects
None yet
Development

No branches or pull requests