Replacing a fixed part of PWD in a tcsh prompt

别等时光非礼了梦想. 提交于 2020-01-04 06:30:43

问题


My prompt is currently displayed like here:

[Aug-27 14:36] /x/y/z/w/u/v/dir1/dir2/dir3>

What I would like to do is replace the constant partial-path of the current working directory

/x/y/z/w/u/v

with

$WORK

so eventually what will be displayed is

[Aug-27 14:36] $WORK/dir1/dir2/dir3>

/x/y/z/w/t/u is always the same path from which I usually do my work and for which I have a local variable $WORK set (very similar to the home ~ idea).

A straight-forward solution will be most-welcomed as I really don't know much about setting a shell.


回答1:


Just put those lines into ~/.tcshrc:

set WORK='/x/y/z/w/u/v'
set dollar='$'
alias precmd 'printf "%b" "\e[36m"; date +"[%b-%d %H:%M] " | tr -d "\n";  [ `expr "$PWD" : "$WORK*"` -gt 0 ] && printf "%s" "$dollar$PWD" | sed "s|$WORK|WORK|" - || printf "%s" "$PWD"'
set prompt='%#%{\e[0;0m%} '

# The default tcsh ^L binding for screen clearing does not run precmd.
# This one does.
bindkey -s "^L" "clear\n"

precmd is a command, which is run before a prompt is shown to you. You can use it to customize your prompt using other commands available on your system.

When it comes to colors, you can add them using those special color sequences like \e[36m (more details here). In the my example I turned on non-bold cyan for the whole prompt by prepending printf "%b" "\e[36m"; to the definition of precmd. You add your own colors this way, just put that a similar printf command somewhere in there. I turned off colors (bringing back the default text color of the terminal) by appending %{\e[0;0m%} to the prompt, end of which happens to be set by the prompt variable. I'm using %{...%} because this is how you change colors inside when setting the prompt variable. So basically you should use printf "%b" "..."; for the precmd alias and %{...%} for the prompt variable.


I used those for reference:

  • Setting a part of a PWD as a prompt and keeping a variable updated (SO)
  • Customizing your shell prompt (www.nparikh.org)
  • Setting the current path in the command prompt in (t)csh (www.unix.com)
  • How to write If-else statement in one line in csh? (SO)
  • How to get a list of tcsh shortcuts? (Unix SE)

Tested on Ubuntu 17.04 with tcsh --version returining tcsh 6.20.00 (Astron) 2016-11-24 (x86_64-unknown-linux) options wide,nls,dl,al,kan,sm,rh,nd,color,filec.




回答2:


This is just a custom prompt that probably could give you an idea about how to create/improve yours:

set COLOR1="%{\e[0;32m%}"
set COLOR2="%{\e[0;33m%}"
set COLOR3="%{\e[0;36m%}"
set COLOR4="%{\e[0;0m%}"
set COLOR5="%{\e[0;33m%}"
set prompt="$COLOR2\[$COLOR3%n@%M$COLOR2\:$COLOR1%~$COLOR2\] [%p %d]\n$COLOR5>$COLOR4 "
set promptchars = "%#"

The prompt will be something like:

[user@host:/current/dir] [current date]
>

Like the COLOR variables you could set WORK.

Also, this answer could help: https://stackoverflow.com/a/20871994/1135424



来源:https://stackoverflow.com/questions/45914747/replacing-a-fixed-part-of-pwd-in-a-tcsh-prompt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!