在获得结果之前猛击新行

时间:2015-11-18 15:53:46

标签: macos bash

我正在修改我的控制台颜色和macos bash上的PS1 var。到目前为止,我已经按照我想要的方式做到了。但是,我想要的最后一件事是我输入的新行AFTER命令和结果之前。目前的剧本如下:

function parse_git_dirty {
  [[ $(git diff --shortstat 2> /dev/null | tail -n1) != "" ]] && echo " *"
}
function parse_git_branch {
  git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e "s/* \(.*\)/ (\1$(parse_git_dirty)\)/"
}

export CLICOLOR=1
export LSCOLORS=exFxBxDxCxegedabagacex
export PS1="\n\[$(tput setaf 4)\]\u\[$(tput setaf 2)\] \h: \[$(tput setaf 0)\]\w\[$(tput setaf 4)\]\$(parse_git_branch)\[$(tput sgr0)\] \$ "

以下是它现在的样子

Here is how it looks now

我希望它看起来像

user host pwd (branch) $ ls
--- newline which is missing and I don't know how to get it here ---
folder folder file whatever.something etc...
--- newline that is here because of the \n in PS1 ---
user host pwd (branch) $ 

行为的其他描述:

  • 按Enter键执行命令后,Newline应该在那里。
  • 如果命令的结果有多行,则换行只应显示一次(在上面的示例中,新行应该只在ls命令后出现一次)。

2 个答案:

答案 0 :(得分:1)

您可以(ab)使用DEBUG陷阱在每个命令执行之前打印空行。这可能不是您想要的,但它是bash提供的最接近的东西。

$ trap 'echo' DEBUG
$ echo foo

foo
$ echo foo; echo bar

foo

bar
$

请注意,每个逻辑命令都会执行DEBUG陷阱,而不是每个命令行执行一次。

答案 1 :(得分:0)

我认为您所要做的就是在导出PS1声明的末尾加上\ n。

像:

export PS1="\n\[$(tput setaf 4)\]\u\[$(tput setaf 2)\] \h: \[$(tput setaf 0)\]\w\[$(tput setaf 4)\]\$(parse_git_branch)\[$(tput sgr0)\] \$\n"

但在这种情况下,你可能想要一个表明你身份的角色。所以你可以这样做:

export PS1="\n\[$(tput setaf 4)\]\u\[$(tput setaf 2)\] \h: \[$(tput setaf 0)\]\w\[$(tput setaf 4)\]\$(parse_git_branch)\[$(tput sgr0)\]\n\$ "

在你的$字符后加一个空格,让它看起来很漂亮。

相关问题