Andrew Connell
Andrew Connell

Reputation: 5287

Add newline to Oh My ZSH Theme

I'm trying to add a newline to my existing Oh My ZSH theme but can't figure out what to add or where it should be added / changed. Any ideas?

Upvotes: 41

Views: 25082

Answers (9)

Som Shekhar Mukherjee
Som Shekhar Mukherjee

Reputation: 8138

If you want spacing b/w the command and it's output, like:

enter image description here

instead of this:

enter image description here


Add the following to your theme:

preexec() {
  echo ""
}

precmd() {
  echo ""
}

Here's the entire file contents of ~/.oh-my-zsh/themes/robbyrussell.zsh-theme:

PROMPT="%(?:%{$fg_bold[green]%}%1{➜%} :%{$fg_bold[red]%}%1{➜%} ) %{$fg[cyan]%}%c%{$reset_color%}"
PROMPT+=' $(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]%}%1{✗%}"
ZSH_THEME_GIT_PROMPT_CLEAN="%{$fg[blue]%})"

preexec() {
  echo ""
}

precmd() {
  echo ""
}

Upvotes: 0

AverageHelper
AverageHelper

Reputation: 2221

I like gvsrepins answer, but didn't want to have to copy in the existing prompt_end definition. (I use a git submodule to load the Agnoster theme, and don't want to have to keep code updated in more than one spot).

What I did was add a new segment, and out it at the end of AGNOSTER_PROMPT_SEGMENTS, like so:

prompt_newline() {
    printf "\n ➜";
}

AGNOSTER_PROMPT_SEGMENTS+=prompt_newline

After that, I get a very similar result, with the arrow on a new line beneath the statusline, without having to update my copy of existing code when/if theme internals change!

Your theme might have something similar, a way to add a "segment" to the prompt.

Note: Agnoster seems to add a space between segments, so my arrow character isn't on the far left of the window. This is how I like it anyway, feels less cramped, but you may want to make more changes for your preferred setup.

Upvotes: 0

Rok T.
Rok T.

Reputation: 399

If you use the default theme (robbyrussell) a good approach is to overwrite the ZSH_THEME_GIT_PROMPT_SUFFIX or PROMPT in the ~/.zshrc file.

Somewhere after the line # User configuration you put something like this:

OMZ_NEW_LINE_FORMAT=$'\n ➜ '
ZSH_THEME_GIT_PROMPT_SUFFIX+=$OMZ_NEW_LINE_FORMAT
# Uncomment if you want to move the command line to a new line always 
#PROMPT+=$OMZ_NEW_LINE_FORMAT

You simply use the += so that the command is left as it is, yet you concatenate the new line \n char and I put an arrow () and some spacing, but you can use whatever you like.

The difference between changing the ZSH_THEME_GIT_PROMPT_SUFFIX and PROMPT variables is that in the former (ZSH_THEME_GIT_PROMPT_SUFFIX) it only creates a new line when you are in a git repo, while the later it shows a new line always.

If you want to observe the content of the theme you can check it like this:

cat ~/.oh-my-zsh/themes/robbyrussell.zsh-theme

Upvotes: 1

RicHincapie
RicHincapie

Reputation: 3973

If your using PROMPT and appending to it, like in senpai theme, just do:

        if [[ $SENPAI_SHOW_K8S == true ]]; then
                PROMPT+="\$(k8s_info)
 ${yellow} —————» %f"
        fi

And you'll get a multiline prompt:

    [19:28:02] ~ ⎈:default-demo2-gke
      —————»  ns:default ❯ ls /etc

Simplified:

PROMPT='multi
line
prompt'

Upvotes: 0

Bvoid
Bvoid

Reputation: 43

If you don't want to update your agnoster theme because it will be overwritten by an update, you can create a new theme and place it under ~/.oh-my-zsh/custom directory. The "custom" directory is excluded by git.

Upvotes: 0

Muhammad Reda
Muhammad Reda

Reputation: 27013

How about this

NEWLINE=$'\n'
PROMPT='%n ${NEWLINE} $ '

Upvotes: 12

gvsrepins
gvsrepins

Reputation: 1775

I was actually searching for the same answer. But my needs was a little more specific since I only wanted to add a newline in the agnoster theme, the one I'm using now.

In my research, I find a lot of forked themes that already do it, but I thought that this was an overkill solution for only add a new line.

So I read the agnoster code and come up with this simple solution of overwrite the prompt_end() function in my .zshrc file.

To do it, just add the code bellow in your .zshrc file:

prompt_end() {
  if [[ -n $CURRENT_BG ]]; then
      print -n "%{%k%F{$CURRENT_BG}%}$SEGMENT_SEPARATOR"
  else
      print -n "%{%k%}"
  fi

  print -n "%{%f%}"
  CURRENT_BG='' 

  #Adds the new line and ➜ as the start character.
  printf "\n ➜";
}

Hope it helps you to have a clue on how to customize your chosen theme.

Here is the result:

enter image description here

Upvotes: 113

Felix Dombek
Felix Dombek

Reputation: 14334

Here is my version which works just like the others, but repeats the last symbol of the previous line so that it imitates the exact prompt that agnoster gives you:

prompt_end() {
  if [[ -n $CURRENT_BG ]]; then
    echo -n " %{%k%F{$CURRENT_BG}%}$SEGMENT_SEPARATOR"
  else
    echo -n "%{%k%}"
  fi
  echo -n "\n%{%F{$CURRENT_BG}%}$SEGMENT_SEPARATOR%{%f%}"
  CURRENT_BG=''
}

Note: If you do not want to modify the library's source code, you can also just put this function into your ~/.zshrc file near the end. It will then be used over the library-provided function.

enter image description here

Upvotes: 14

Micheal Bee
Micheal Bee

Reputation: 628

I think the proper place to change one's prompt is in the theme itself. On my system, it's in ~/.oh=my-zsh/themes/agnoster.zsh-theme. I added a \n➜ there:
Find this section:

# End the prompt, closing any open segments

prompt_end() {
  if [[ -n $CURRENT_BG ]]; then
    echo -n " % 

{%k%F{$CURRENT_BG}%}$SEGMENT_SEPARATOR"
  else
    echo -n "%{%k%}"
  fi
  echo -n "\n➜%{%f%}"
  CURRENT_BG=''
}

Upvotes: 18

Related Questions