38

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?

Andrew Connell
  • 4,939
  • 5
  • 30
  • 42

6 Answers6

105

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

gvsrepins
  • 1,687
  • 2
  • 17
  • 20
16

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=''
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
Micheal Bee
  • 550
  • 5
  • 11
  • The code formatter in stackoverflow seems difficult. I'm sorry for the badly formatted code. Is there a limit to how much code can be formatted as code? – Micheal Bee Nov 28 '19 at 20:36
  • 3
    When you change the theme directly, you will loose your config when update it. – gvsrepins Feb 17 '20 at 12:48
  • 2
    Although I'll stick with the "override in `.zshrc`" approach. It's still very helpful to remind people to look into the original theme file, learn how the `prompt_end` is implemented and know what to override and where to append. Thanks! – Escape0707 Apr 27 '20 at 02:13
12

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

Felix Dombek
  • 13,664
  • 17
  • 79
  • 131
11

How about this

NEWLINE=$'\n'
PROMPT='%n ${NEWLINE} $ '
Muhammad Reda
  • 26,379
  • 14
  • 93
  • 105
0

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.

Bvoid
  • 23
  • 4
0

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'
RicHincapie
  • 3,275
  • 1
  • 18
  • 30