monkeyUser
monkeyUser

Reputation: 4671

Add Dollar Sign after path in prompt .zsh

I want to add the dollar sign after the path in my prompt.

Current prompt is:

.oh-my-zsh git:(master) 

Wanted prompt is:

.oh-my-zsh git:(master) $

I tried to modify PROMPT in ~/.zshrc without success.

Upvotes: 4

Views: 3139

Answers (3)

Michael Ekoka
Michael Ekoka

Reputation: 20088

One way to set the user|superuser character in the prompt, according to the manual, is to add this pattern %(!.<superuser_char>.<ordinary_user_char>). For instance to have this joe@mypc:~ $ when joe is running the shell as an ordinary user and this root@mypc:~# when running as a superuser, while keeping the user's environment (i.e. not the root environment, for instance when starting a superuser tmux session with sudo -E tmux new -s admintasks), this is what should be in .zshrc.

`PS1='%n@%m:%~%(!.#.$) `

See man zshmisc, under SIMPLE PROMPT ESCAPES / Shell state section.

Upvotes: 2

Osinaga
Osinaga

Reputation: 71

editing your PROMPT variable is fine, but maybe you just want to change zsh's behaviour for all prompt themes without going in and modifying every single %# in your themes. % is hardcoded in the source code. but it's pretty simple to edit. You can find the line in question in the prompt.c file in the source code (github) specifically on line 734 currently. The code in question is:

        case '#': 
        addbufspc(1);
        *bv->bp++ = privasserted() ? '#' : '%';
        break;

All you have to do is change % for whatever character you want, probably $, build the program and replace your zsh install. That might be a pain tho. You can also patch your current binary to replace whatever character you want. You just have to change a single byte, from 0x25 (%) to 0x24 ($). The problem is, it's hard to find where that is in your zsh binary! in my case (Ubuntu 20.04, zsh 5.8) I found this:

   $ objdump -D zsh  | less

   8af87:       e8 84 e4 01 00          callq  a9410 <privasserted@@Base>
   8af8c:       4c 8b 2d fd 49 05 00    mov    0x549fd(%rip),%r13        # df990 <keyboardhackchar@@Base+0x210>
   8af93:       0f b6 74 24 10          movzbl 0x10(%rsp),%esi
   8af98:       85 c0                   test   %eax,%eax
   8af9a:       b8 25 00 00 00          mov    $0x25,%eax
   8af9f:       0f 44 f0                cmove  %eax,%esi
   8afa2:       49 8b 45 18             mov    0x18(%r13),%rax
   8afa6:       48 8d 50 01             lea    0x1(%rax),%rdx

To be frank im not even sure what most of the instructions here do, but it's easy to see that instruction at 0x8af9a is the culprit. Using a Hex editor (ghex) I replaced the 0x25 there with a 0x24, and now zsh shows a $ prompt! This is easier than downloading the zsh source and recompiling it, but it took me a while to find the right instruction to patch. There doesn't seem to be many instances of privasserted calls in the binary, so you can probably check all of those one by one and see which one fits.

Hope that was helpful! I am a bash user and that % was annoying me too much, so much I refused to try zsh. Now I might give it a try.

Upvotes: 1

Barno
Barno

Reputation: 3331

https://github.com/ohmyzsh/ohmyzsh/blob/c78277fd8bda5fec87504469afdf121355876006/themes/gozilla.zsh-theme#L1

PROMPT='%{$fg_bold[red]%}➜ %{$fg_bold[green]%}%p %{$fg[cyan]%}%c %{$fg_bold[blue]%}$(git_prompt_info)%{$fg_bold[blue]%}$ % %{$reset_color%}'

Upvotes: 3

Related Questions