oh-my-posh/website/docs/installation/prompt.mdx
sheldonhull 3e23bd30dd docs: improve zsh example with correct initialization
For zsh, eval isn't the correct way to reload.
Changed to `exec zsh`, which could also be `exec zsh -l` for login shell.

This is helpful when reloading to avoid scope and other issues. See for example a brief write-up [reload-zsh-configuration](https://batsov.com/articles/2022/09/15/reload-zsh-configuration/)
2022-10-18 19:52:01 +02:00

204 lines
4.9 KiB
Plaintext

---
id: prompt
title: Change your prompt
sidebar_label: ✨ Prompt
---
import Tabs from "@theme/Tabs";
import TabItem from "@theme/TabItem";
:::tip
If you have no idea which shell you're currently using, Oh My Posh has a utility switch that can tell that to you.
:::
```bash
oh-my-posh get shell
```
<Tabs
defaultValue="powershell"
groupId="shell"
values={[
{ label: 'powershell', value: 'powershell', },
{ label: 'cmd', value: 'cmd', },
{ label: 'zsh', value: 'zsh', },
{ label: 'bash', value: 'bash', },
{ label: 'fish', value: 'fish', },
{ label: 'nu', value: 'nu', },
]
}>
<TabItem value="powershell">
Edit your PowerShell profile script, you can find its location under the `$PROFILE` variable in your preferred PowerShell version. For example, using notepad:
```powershell
notepad $PROFILE
```
:::info
When the above command gives an error, make sure to create the profile first.
```powershell
New-Item -Path $PROFILE -Type File -Force
```
:::
Then add the following line.
```powershell
oh-my-posh init pwsh | Invoke-Expression
```
:::tip Antivirus software
If adding an exception to your Antivirus software still blocks Oh My Posh for Windows, you can try using the below
alternate line in your PowerShell profile. This is a drop-in replacement to the above call with Invoke-Expression.
Tested with PowerShell 7.2.4 with Bitdefender Antivirus installed and requires NO exceptions for oh-my-posh.exe.
Bitdefender blocks the calls to Invoke-Expression as malicious code and this alternate line skips the double calls:
```powershell
& ([ScriptBlock]::Create((oh-my-posh init pwsh --config "$env:POSH_THEMES_PATH\jandedobbeleer.omp.json" --print) -join "`n"))
```
:::
Once added, reload your profile for the changes to take effect.
```powershell
. $PROFILE
```
</TabItem>
<TabItem value="cmd">
There's no out-of-the-box support for Windows CMD when it comes to custom prompts.
There is however a way to do it using [Clink][clink], which at the same time supercharges
your cmd experience. Follow the installation instructions and make sure you select autostart.
Integrating Oh My Posh with Clink is easy: create a new file called oh-my-posh.lua in your Clink
scripts directory (run `clink info` inside cmd to find that file's location).
```lua title="oh-my-posh.lua"
load(io.popen('oh-my-posh init cmd'):read("*a"))()
```
Once added, restart cmd for the changes to take effect.
</TabItem>
<TabItem value="zsh">
Add the following to `~/.zshrc`:
```bash
eval "$(oh-my-posh init zsh)"
```
:::tip
As the standard terminal has issues displaying the ANSI characters correctly, you might want to skip loading just for that terminal and instead of the line above, place this in your `~/.zshrc`:
```bash
if [ $TERM_PROGRAM != "Apple_Terminal" ]; then
eval "$(oh-my-posh init zsh)"
fi
```
Note this will still load Oh My Posh for [iTerm2][iterm2] or any other modern day macOS terminal that supports ANSI characters.
:::
Once added, reload your profile for the changes to take effect.
```bash
exec zsh
```
</TabItem>
<TabItem value="bash">
Add the following to `~/.bashrc` (could be `~/.profile` or `~/.bash_profile` depending on your environment):
```bash
eval "$(oh-my-posh init bash)"
```
Once added, reload your profile for the changes to take effect.
```bash
exec bash
```
Or, when using `~/.profile`.
```bash
. ~/.profile
```
</TabItem>
<TabItem value="fish">
:::caution
Oh My Posh requires fish v3.4.0 or higher.
:::
Initialize Oh My Posh in `~/.config/fish/config.fish`:
```bash
oh-my-posh init fish | source
```
Once added, reload your config for the changes to take effect.
```bash
exec fish
```
</TabItem>
<TabItem value="nu">
:::caution
Oh My Posh requires Nushell v0.68.1 or higher.
:::
Run the following command:
```bash
oh-my-posh init nu
```
it saves the initialization script to `~/.oh-my-posh.nu` by default.
Then edit the Nushell config file (`$nu.config-path`) and add the following line at the bottom:
```bash
source ~/.oh-my-posh.nu
```
If you want to save the initialization script elsewhere, you should run a command like:
```bash
oh-my-posh init nu --print | save /mylocation/myscript.nu
```
and add the `source` line like:
```bash
source /mylocation/myscript.nu
```
Once added, restart Nushell for the changes to take effect.
:::tip
For the time being, there is a [problem][homebrew-problem] with the initialization using Oh My Posh installed via Homebrew.
To resolve this, you can use the `--strict` flag which tells Oh My Posh to use the executable name and not the
full path in the initialization. For example:
```bash
oh-my-posh init nu --print --strict | save /mylocation/myscript.nu
```
This way for a new Oh My Posh version that does not update the initialization script, you don't have to recreate a new one.
:::
</TabItem>
</Tabs>
[clink]: https://chrisant996.github.io/clink/
[iterm2]: https://www.iterm2.com/
[homebrew-problem]: https://github.com/JanDeDobbeleer/oh-my-posh/discussions/2644