在深层文件夹结构中使用终端时,有时提示符会占据大部分行。有没有办法缩短工作目录的显示?我知道可以这样做:
仅打印当前目录而不是完整路径,但是否有类似以下的方式:
PS1="\W >"
仅打印当前目录而不是完整路径,但是否有类似以下的方式:
/home/smauel/de...ther/folder >
PS1="\W >"
/home/smauel/de...ther/folder >
PROMPT_DIRTRIM=2
PS1='[\u@\h:$(p=${PWD/#"$HOME"/~};((${#p}>30))&&echo "${p::10}…${p:(-19)}"||echo "\w")]\$ '
import os
from socket import gethostname
hostname = gethostname()
username = os.environ['USER']
pwd = os.getcwd()
homedir = os.path.expanduser('~')
pwd = pwd.replace(homedir, '~', 1)
if len(pwd) > 33:
pwd = pwd[:10]+'...'+pwd[-20:] # first 10 chars+last 20 chars
print '[%s@%s:%s] ' % (username, hostname, pwd)
export PROMPT_COMMAND='PS1="$(python ~/.short.pwd.py)"'
~/.bashrc
文件中。~/.bashrc
呢?只需将最后一行粘贴到文件底部吗? - FloatingRockPS1="\w\n>"
~/.bashrc
文件的末尾。split_pwd() {
# Only show ellipses for directory trees -gt 3
# Otherwise use the default pwd as the current \w replacement
if [ $(pwd | grep -o '/' | wc -l) -gt 3 ]; then
pwd | cut -d'/' -f1-3 | xargs -I{} echo {}"/../${PWD##*/}"
else
pwd
fi
}
export PS1="\$(split_pwd) > "
/home/chris/../Node Projects >
/home/chris/Desktop >
/home/chris >
/home
get_bash_w() {
# Returns the same working directory that the \W bash prompt command
echo $(pwd | sed 's@'"$HOME"'@~@')
}
split_pwd() {
# Split pwd into the first element, elipsis (...) and the last subfolder
# /usr/local/share/doc --> /usr/.../doc
# ~/project/folder/subfolder --> ~/project/../subfolder
split=2
W=$(get_bash_w)
if [ $(echo $W | grep -o '/' | wc -l) -gt $split ]; then
echo $W | cut -d'/' -f1-$split | xargs -I{} echo {}"/../${W##*/}"
else
echo $W
fi
}
export PS1="\$(split_pwd) > "
#! /usr/bin/python3
import os, getpass
from socket import gethostname
username = getpass.getuser()
hostname = gethostname()
pwd = os.getcwd()
homedir = os.path.expanduser('~')
pwd = pwd.replace(homedir, '~', 1)
if len(pwd) > 40:
# first 10 chars+last 30 chars
pwd = pwd[:10] + '...' + pwd[-30:]
# Virtual environment being used? Essential not to omit!
ve = os.getenv('VIRTUAL_ENV')
venv = '(`basename \"$VIRTUAL_ENV\"`)' if ve else ''
# colours as per my current BASH Terminal:
# username + hostname: bold green
# path and $: bold blue
print( '\[\e[;1;32m\]%s%s@%s \[\e[;1;34m\]%s $\[\e[0m\] ' % (venv, username, hostname, pwd) )
export
行添加到 .bashrc 中变成了:export PROMPT_COMMAND='PS1="$(~/.local/bin/manage_prompt.py)"' # adjust path to .py file
NB1 这些 "\e" 转义码必须始终用 "\[ ... \]" 括起来,否则换行会完全混乱。
NB2 随时获取您的完整路径,只需执行
... $ pwd
workon
命令时会显示出来。import os
from platform import node
hostname = node().split('.')[0]
username = os.environ['USER']
pwd = os.getcwd()
homedir = os.path.expanduser('~')
pwd = pwd.replace(homedir, '~', 1)
# check for the virtualenv
ve = os.getenv('VIRTUAL_ENV')
if ve:
venv = '(`basename \"$VIRTUAL_ENV\"`)'
else:
venv = ''
if len(pwd) > 33:
pwd = pwd[:10]+'...'+pwd[-20:] # first 10 chars+last 20 chars
print '%s[%s@%s:%s] ' % (venv, username, hostname, pwd)
PS1="[\W]\\$ "
。user:/h/t/D/C/current$
sps() {
echo `dirname $PWD` | sed -r 's|/(.)[^/]*|/\1|g'
}
PS1='\u:$$(eval "sps")/\W\$ '
export MAXPROMPTLEN=<desired length>
#! /usr/bin/python3
import os, getpass
from socket import gethostname
username = getpass.getuser()
hostname = gethostname()
pwd = os.getcwd()
homedir = os.path.expanduser('~')
pwd = pwd.replace(homedir, '~', 1)
# Default value for maxlen of pwd
# Use environment varibale MAXPROMPTLEN to change dynamically
MAXLEN=30
if "MAXPROMPTLEN" in os.environ:
MAXLEN = int(os.environ["MAXPROMPTLEN"])
if len(pwd) > MAXLEN:
# keep first and last folder
parts = pwd.split(os.path.sep)
leaf = "..." + parts[-1]
stem = os.path.sep.join(parts[:2])[:(MAXLEN-len(leaf)+1)]
pwd = stem + leaf
# Virtual environment being used? Essential not to omit!
ve = os.getenv('VIRTUAL_ENV')
venv = '(`basename \"$VIRTUAL_ENV\"`)' if ve else ''
# colours as per my current BASH Terminal:
# username + hostname: bold green
# path and $: bold blue
print(f"\[\e[;1;32m\]{venv}{username}@{hostname} \[\e[;1;34m\]{pwd}$\[\e[0m\]")
.bashrc
中:# Limit the lenght of the prompt dynamically
# comment to turn off
max_prompt_len=yes
if [ "$max_prompt_len" ]; then
MAXPROMPTLEN=30 # maxium length of prompt
PS1="$(python3 ~/.short_pwd.py)"
fi
~/.short_pwd.py
中。