Search code examples
bashterminalfish

fish shell: add newline before prompt only when previous output exists


(Disclaimer: I am using fish, but this should apply equally to bash)
My current shell prints a newline before the prompt so I can easily find it between command outputs.

# [...]
echo # newline before prompt
echo -s $arrow ' ' $cwd $git_info
echo -n -s '❯ '

However, the newline is also printed when there is no previous output, e.g. after clearing the terminal with printf "\033c" (or when the terminal is first opened):

                           <--- bad newline: no previous output
➜ /some/dir            
❯ command1            
output...             
                           <--- good newline
➜ /some/dir          
❯ command2             

Question: is there any way I can get rid of this small aesthetic annoyance?



Edit #1:

For clarification: By "no previous output" I meant the contents of my console are empty, i.e. after (re-)initializing the terminal (because that's all printf "\033c" does).


Solution

  • exec events were merged into fish-shell a few years ago. I think you can use these here. They work like event lifecycle hooks in other languages. https://github.com/fish-shell/fish-shell/pull/1666

    As a test I made a file called postexec-newline.fish with the following:

    function postexec_test --on-event fish_postexec
       echo
    end
    

    After issuing source postexec-newline.fish, the behavior you are describing is observed. The newline is also not visible when the screen is cleared with C-l, which I think is how such a feature should behave.

    This function can live in config.fish if you want the change permanently.