Terminal – How to Center Prompt Vertically in iTerm2

itermiterm2terminalzsh

I just realized that I hunch over a lot when using the terminal since the prompt is always at the bottom of the terminal window, which is vertically maximized.

I would like the prompt to be at the vertical middle of the window (or in a point near where my eyes are pointed to).

You could argue that I can resize the window and achieve that, but sometimes I like the vertical space (eg. when running ls -la). So, ideally, I could toggle the position of the prompt between some point and the bottom of the window.

(I'm using iTerm under MacOS with zsh, but I'm interested in an agnostic/generic way of doing this)

Best Answer

The following code (uses zsh features, but the priciple can be used with other shells, too) defines two shell functions, prompt_middle and prompt_restore.

The first function keeps the prompt always above the middle of the terminal by forcing an appropriate number of empty lines below the prompt. The latter funtion restores the normal behavior.

You can assign these functions to some shortcuts or use some logic to toggle between these two modes.

# load terminfo modules to make the associative array $terminfo available
zmodload zsh/terminfo 

# save current prompt to parameter PS1o
PS1o="$PS1"

# calculate how many lines one half of the terminal's height has
halfpage=$((LINES/2))

# construct parameter to go down/up $halfpage lines via termcap
halfpage_down=""
for i in {1..$halfpage}; do
  halfpage_down="$halfpage_down$terminfo[cud1]"
done

halfpage_up=""
for i in {1..$halfpage}; do
  halfpage_up="$halfpage_up$terminfo[cuu1]"
done

# define functions
function prompt_middle() {
  # print $halfpage_down
  PS1="%{${halfpage_down}${halfpage_up}%}$PS1o"
}

function prompt_restore() {
  PS1="$PS1o"
}

Personally instead of toggling between two modes I would use a much simpler approach (you need the definitions of $halfpage_up/down from above):

magic-enter () {
    if [[ -z $BUFFER ]]
    then
            print ${halfpage_down}${halfpage_up}$terminfo[cuu1]
            zle reset-prompt
    else
            zle accept-line
    fi
}
zle -N magic-enter
bindkey "^M" magic-enter

This checks if the current command line is empty (see my other answer) and if so move the prompt up to the middle of the terminal. Now you can fast-forward your prompt with an additional press of the ENTER key (or maybe you want to call it double-press similar to a double-click).

Related Question