Show only current directory name (not full path) on bash prompt
The way my bash prompt is currently configured, it shows the whole path to the current directory. This is annoying when I'm deep inside a directory tree, as the prompt becomes so long that every command wraps into the next line. How do I make it show only the last part of the path?
This is what I have in my .bashrc:
PS1='${debian_chroot:+($debian_chroot)}[33[01;32m]u@h[33[00m]:[33[01;34m]w[33[00m]$ '
# If this is an xterm set the title to user@host:dir
case "$TERM" in
xterm*|rxvt*)
PROMPT_COMMAND='echo -ne "33]0;${USER}@${HOSTNAME}: ${PWD/$HOME/~}07"'
;;
*)
;;
esac
linux command-line bash bashrc
add a comment |
The way my bash prompt is currently configured, it shows the whole path to the current directory. This is annoying when I'm deep inside a directory tree, as the prompt becomes so long that every command wraps into the next line. How do I make it show only the last part of the path?
This is what I have in my .bashrc:
PS1='${debian_chroot:+($debian_chroot)}[33[01;32m]u@h[33[00m]:[33[01;34m]w[33[00m]$ '
# If this is an xterm set the title to user@host:dir
case "$TERM" in
xterm*|rxvt*)
PROMPT_COMMAND='echo -ne "33]0;${USER}@${HOSTNAME}: ${PWD/$HOME/~}07"'
;;
*)
;;
esac
linux command-line bash bashrc
add a comment |
The way my bash prompt is currently configured, it shows the whole path to the current directory. This is annoying when I'm deep inside a directory tree, as the prompt becomes so long that every command wraps into the next line. How do I make it show only the last part of the path?
This is what I have in my .bashrc:
PS1='${debian_chroot:+($debian_chroot)}[33[01;32m]u@h[33[00m]:[33[01;34m]w[33[00m]$ '
# If this is an xterm set the title to user@host:dir
case "$TERM" in
xterm*|rxvt*)
PROMPT_COMMAND='echo -ne "33]0;${USER}@${HOSTNAME}: ${PWD/$HOME/~}07"'
;;
*)
;;
esac
linux command-line bash bashrc
The way my bash prompt is currently configured, it shows the whole path to the current directory. This is annoying when I'm deep inside a directory tree, as the prompt becomes so long that every command wraps into the next line. How do I make it show only the last part of the path?
This is what I have in my .bashrc:
PS1='${debian_chroot:+($debian_chroot)}[33[01;32m]u@h[33[00m]:[33[01;34m]w[33[00m]$ '
# If this is an xterm set the title to user@host:dir
case "$TERM" in
xterm*|rxvt*)
PROMPT_COMMAND='echo -ne "33]0;${USER}@${HOSTNAME}: ${PWD/$HOME/~}07"'
;;
*)
;;
esac
linux command-line bash bashrc
linux command-line bash bashrc
asked Oct 25 '09 at 21:38
hsribeihsribei
2,72293130
2,72293130
add a comment |
add a comment |
7 Answers
7
active
oldest
votes
Change the w (lowercase) to W (uppercase):
PS1='${debian_chroot:+($debian_chroot)}[33[01;32m]u@h[33[00m]:[33[01;34m]W[33[00m]$ '
^^
this one waaaaaay over here ------------------------------------------------+
Have a look at the Bash Prompt HOWTO for lots of fun details. example:
user@host:/usr/local/bin$ echo $PS1
${debian_chroot:+($debian_chroot)}[33[01;31m]u@h[33[00m]:[33[01;36m]w[33[00m]$
user@host:/usr/local/bin$ export PS1='${debian_chroot:+($debian_chroot)}[33[01;31m]u@h[33[00m]:[33[01;36m]W[33[00m]$ '
user@host:bin$
The PROMPT_COMMAND variable, if set, is a command that gets run before displaying the prompt specified in PS1. In your case, PROMPT_COMMAND runs an echo statement with certain ANSI escape sequences that manipulate the titlebar of an Xterm.
If you suspect your PROMPT_COMMAND is overriding your PS1 prompt, you can unset it and test things out:
$ unset PROMPT_COMMAND
Finally, be sure that you're changing the PS1 definition that actually gets used. Common locations are /etc/bash.bashrc, /etc/profile, ~/.bashrc, ~/.bash_profile, ~/.profile. The system files are generally (but not always) run before the user files.
Hm... I'm afraid it's already w, but it seems like thatcasestatement overrides it when i'm on an xterm, and the problem seems to be with thePWDin thePROMPT_COMMANDline. Do you know what I should put there?
– hsribei
Oct 25 '09 at 22:48
4
w(lower case) sets it to full path,W(uppercase) trims to the final bit. the PROMPT_COMMAND is setting the window title in an xterm. check your TERM variable; if it doesn't start with "xterm" or "rxvt" then PROMPT_COMMAND isn't even getting run.
– quack quixote
Oct 25 '09 at 22:59
Oh, yeah, duh. Sorry, I mixed upper and lowercase. That worked. Thanks! :)
– hsribei
Oct 25 '09 at 23:39
Great answer. Make sure you reboot in order this to take effect. Thanks!
– Combine
May 31 '18 at 9:00
add a comment |
Simple bash replace command is
${VAR/pattern_to_find/pattern_to_replace}
For showing the last directory you can just do ${PWD/*//}, i.e. find any thing before and including the last '/' and replace it with nothing.
On my ubuntu machine I use:
export PS1='$(whoami):${PWD/*//}#'.
5
Only six years late!
– Burgi
Jun 7 '16 at 8:29
I like this answer better than the accepted one because it's generic to any situation instead of just the special parsing logic of $PS1. Fewer more powerful tools are easier to remember and compose. :)
– David Ellis
Jun 7 '18 at 17:03
add a comment |
My solution is to show the top three and bottom 2 directories when there are more than 5
So my prompt (which has other info too) looks like:
08:38:42 durrantm U2017 /home/durrantm/Dropbox/_/rails/everquote
when my pwd is actually
/home/durrantm/Dropbox/93_2016/work/code/ruby__rails/rails/everquote
My PS1 prompt is setup as follows:
HOST='[33[02;36m]h'; HOST=' '$HOST
TIME='[33[01;31m]t [33[01;32m]'
LOCATION=' [33[01;34m]`pwd | sed "s#(/[^/]{1,}/[^/]{1,}/[^/]{1,}/).*(/[^/]{1,}/[^/]{1,})/{0,1}#1_2#g"`'
BRANCH=' [33[00;33m]$(git_branch)[33[00m]n$ '
PS1=$TIME$USER$HOST$LOCATION$BRANCH
git_branch is a function which shows current git branch, I keep it in my dotfiles, it is:
git_branch () {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* (.*)/1/'
}
add a comment |
My solution for this is slightly different in regards to how do I export it, so thought I'd share it here:
Create another prompt string variable; PS5 and export the following string in your .profile / .bash_profile file:
u: Display the current username .
W: Print the base of current working directory.
# Display username and current directory only.
export PS5='export PS1="u:W$";';
Now whenever you need to use the shorthand-ed PS, just run: eval $PS5
Or even better yet, create an alias in your .bash_aliases file: (thanks to @muru)
alias PS5='export PS1="u:W$";';
source again, and now you can just type PS5 to switch.
1
This looks like a frankenalias. Why not just use an alias or a function?
– muru
Aug 19 '18 at 1:31
@muru Thanks, this is much better, answer updated.
– U-ways
Aug 19 '18 at 11:17
add a comment |
Show only current directory name (not full path) on bash prompt
Most of the other solutions did not work for me across all of my OSes that share my dot files: AIX, Windoze, and Linux. The bash ports were old versions that didn't support certain constructs and I didn't want to fork another process (i.e. sed, awk, etc.) which is noticeably expensive under cygwin.
The following is long but performant:
# takes a number argument of the number of last dirs to show
function DIR_LAST {
# read -a didn't seem to work under bash 3
IFS='/' array=($PWD)
len=${#array[@]}
start=$((len - $1))
# leading / if fewer dir args: /usr/flastname not usr/flastname
if (( $start <= 1 )); then
start=1
echo -n /
fi
for (( i = $start; $i < $len; i++ )); do
if (( $i > $start )); then
echo -n /
fi
echo -n ${array[$i]}
done
}
export PS1="$(DIR_LAST 2) {$(hostname)} "
I want it to spit out:
/
/usr
/usr/foo
foo/bin
Notice the lack of a leading slash in the last line which is how I like it. Also, you can spit out the last 3 directories by changing the arg to DIR_LAST.
Also, I tried to do this with a regex and BASH_REMATCH but bash v3 didn't seem to like the parens and I couldn't figure out how to properly escape them. Sigh.
add a comment |
I believe this option is much easier, by simply doing:
echo $PWD | rev | cut -d '/' -f 1 | rev
So assign this to the PS1 variable in your .bashrc file:
PS1='$(PWD | rev | cut -d '/' -f 1 | rev)'
add a comment |
root:~/project# -> root:~/project(dev)#
add the following code to the end of your ~/.bashrc
force_color_prompt=yes
color_prompt=yes
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* (.*)/(1)/'
}
if [ "$color_prompt" = yes ]; then
PS1='${debian_chroot:+($debian_chroot)}[33[01;32m]u@h[33[00m]:[33[01;34m]w[33[01;31m]$(parse_git_branch)[33[00m]$ '
else
PS1='${debian_chroot:+($debian_chroot)}u@h:w$(parse_git_branch)$ '
fi
unset color_prompt force_color_prompt
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "3"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f60555%2fshow-only-current-directory-name-not-full-path-on-bash-prompt%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
7 Answers
7
active
oldest
votes
7 Answers
7
active
oldest
votes
active
oldest
votes
active
oldest
votes
Change the w (lowercase) to W (uppercase):
PS1='${debian_chroot:+($debian_chroot)}[33[01;32m]u@h[33[00m]:[33[01;34m]W[33[00m]$ '
^^
this one waaaaaay over here ------------------------------------------------+
Have a look at the Bash Prompt HOWTO for lots of fun details. example:
user@host:/usr/local/bin$ echo $PS1
${debian_chroot:+($debian_chroot)}[33[01;31m]u@h[33[00m]:[33[01;36m]w[33[00m]$
user@host:/usr/local/bin$ export PS1='${debian_chroot:+($debian_chroot)}[33[01;31m]u@h[33[00m]:[33[01;36m]W[33[00m]$ '
user@host:bin$
The PROMPT_COMMAND variable, if set, is a command that gets run before displaying the prompt specified in PS1. In your case, PROMPT_COMMAND runs an echo statement with certain ANSI escape sequences that manipulate the titlebar of an Xterm.
If you suspect your PROMPT_COMMAND is overriding your PS1 prompt, you can unset it and test things out:
$ unset PROMPT_COMMAND
Finally, be sure that you're changing the PS1 definition that actually gets used. Common locations are /etc/bash.bashrc, /etc/profile, ~/.bashrc, ~/.bash_profile, ~/.profile. The system files are generally (but not always) run before the user files.
Hm... I'm afraid it's already w, but it seems like thatcasestatement overrides it when i'm on an xterm, and the problem seems to be with thePWDin thePROMPT_COMMANDline. Do you know what I should put there?
– hsribei
Oct 25 '09 at 22:48
4
w(lower case) sets it to full path,W(uppercase) trims to the final bit. the PROMPT_COMMAND is setting the window title in an xterm. check your TERM variable; if it doesn't start with "xterm" or "rxvt" then PROMPT_COMMAND isn't even getting run.
– quack quixote
Oct 25 '09 at 22:59
Oh, yeah, duh. Sorry, I mixed upper and lowercase. That worked. Thanks! :)
– hsribei
Oct 25 '09 at 23:39
Great answer. Make sure you reboot in order this to take effect. Thanks!
– Combine
May 31 '18 at 9:00
add a comment |
Change the w (lowercase) to W (uppercase):
PS1='${debian_chroot:+($debian_chroot)}[33[01;32m]u@h[33[00m]:[33[01;34m]W[33[00m]$ '
^^
this one waaaaaay over here ------------------------------------------------+
Have a look at the Bash Prompt HOWTO for lots of fun details. example:
user@host:/usr/local/bin$ echo $PS1
${debian_chroot:+($debian_chroot)}[33[01;31m]u@h[33[00m]:[33[01;36m]w[33[00m]$
user@host:/usr/local/bin$ export PS1='${debian_chroot:+($debian_chroot)}[33[01;31m]u@h[33[00m]:[33[01;36m]W[33[00m]$ '
user@host:bin$
The PROMPT_COMMAND variable, if set, is a command that gets run before displaying the prompt specified in PS1. In your case, PROMPT_COMMAND runs an echo statement with certain ANSI escape sequences that manipulate the titlebar of an Xterm.
If you suspect your PROMPT_COMMAND is overriding your PS1 prompt, you can unset it and test things out:
$ unset PROMPT_COMMAND
Finally, be sure that you're changing the PS1 definition that actually gets used. Common locations are /etc/bash.bashrc, /etc/profile, ~/.bashrc, ~/.bash_profile, ~/.profile. The system files are generally (but not always) run before the user files.
Hm... I'm afraid it's already w, but it seems like thatcasestatement overrides it when i'm on an xterm, and the problem seems to be with thePWDin thePROMPT_COMMANDline. Do you know what I should put there?
– hsribei
Oct 25 '09 at 22:48
4
w(lower case) sets it to full path,W(uppercase) trims to the final bit. the PROMPT_COMMAND is setting the window title in an xterm. check your TERM variable; if it doesn't start with "xterm" or "rxvt" then PROMPT_COMMAND isn't even getting run.
– quack quixote
Oct 25 '09 at 22:59
Oh, yeah, duh. Sorry, I mixed upper and lowercase. That worked. Thanks! :)
– hsribei
Oct 25 '09 at 23:39
Great answer. Make sure you reboot in order this to take effect. Thanks!
– Combine
May 31 '18 at 9:00
add a comment |
Change the w (lowercase) to W (uppercase):
PS1='${debian_chroot:+($debian_chroot)}[33[01;32m]u@h[33[00m]:[33[01;34m]W[33[00m]$ '
^^
this one waaaaaay over here ------------------------------------------------+
Have a look at the Bash Prompt HOWTO for lots of fun details. example:
user@host:/usr/local/bin$ echo $PS1
${debian_chroot:+($debian_chroot)}[33[01;31m]u@h[33[00m]:[33[01;36m]w[33[00m]$
user@host:/usr/local/bin$ export PS1='${debian_chroot:+($debian_chroot)}[33[01;31m]u@h[33[00m]:[33[01;36m]W[33[00m]$ '
user@host:bin$
The PROMPT_COMMAND variable, if set, is a command that gets run before displaying the prompt specified in PS1. In your case, PROMPT_COMMAND runs an echo statement with certain ANSI escape sequences that manipulate the titlebar of an Xterm.
If you suspect your PROMPT_COMMAND is overriding your PS1 prompt, you can unset it and test things out:
$ unset PROMPT_COMMAND
Finally, be sure that you're changing the PS1 definition that actually gets used. Common locations are /etc/bash.bashrc, /etc/profile, ~/.bashrc, ~/.bash_profile, ~/.profile. The system files are generally (but not always) run before the user files.
Change the w (lowercase) to W (uppercase):
PS1='${debian_chroot:+($debian_chroot)}[33[01;32m]u@h[33[00m]:[33[01;34m]W[33[00m]$ '
^^
this one waaaaaay over here ------------------------------------------------+
Have a look at the Bash Prompt HOWTO for lots of fun details. example:
user@host:/usr/local/bin$ echo $PS1
${debian_chroot:+($debian_chroot)}[33[01;31m]u@h[33[00m]:[33[01;36m]w[33[00m]$
user@host:/usr/local/bin$ export PS1='${debian_chroot:+($debian_chroot)}[33[01;31m]u@h[33[00m]:[33[01;36m]W[33[00m]$ '
user@host:bin$
The PROMPT_COMMAND variable, if set, is a command that gets run before displaying the prompt specified in PS1. In your case, PROMPT_COMMAND runs an echo statement with certain ANSI escape sequences that manipulate the titlebar of an Xterm.
If you suspect your PROMPT_COMMAND is overriding your PS1 prompt, you can unset it and test things out:
$ unset PROMPT_COMMAND
Finally, be sure that you're changing the PS1 definition that actually gets used. Common locations are /etc/bash.bashrc, /etc/profile, ~/.bashrc, ~/.bash_profile, ~/.profile. The system files are generally (but not always) run before the user files.
edited Oct 25 '09 at 23:26
answered Oct 25 '09 at 21:54
quack quixotequack quixote
35.2k1087119
35.2k1087119
Hm... I'm afraid it's already w, but it seems like thatcasestatement overrides it when i'm on an xterm, and the problem seems to be with thePWDin thePROMPT_COMMANDline. Do you know what I should put there?
– hsribei
Oct 25 '09 at 22:48
4
w(lower case) sets it to full path,W(uppercase) trims to the final bit. the PROMPT_COMMAND is setting the window title in an xterm. check your TERM variable; if it doesn't start with "xterm" or "rxvt" then PROMPT_COMMAND isn't even getting run.
– quack quixote
Oct 25 '09 at 22:59
Oh, yeah, duh. Sorry, I mixed upper and lowercase. That worked. Thanks! :)
– hsribei
Oct 25 '09 at 23:39
Great answer. Make sure you reboot in order this to take effect. Thanks!
– Combine
May 31 '18 at 9:00
add a comment |
Hm... I'm afraid it's already w, but it seems like thatcasestatement overrides it when i'm on an xterm, and the problem seems to be with thePWDin thePROMPT_COMMANDline. Do you know what I should put there?
– hsribei
Oct 25 '09 at 22:48
4
w(lower case) sets it to full path,W(uppercase) trims to the final bit. the PROMPT_COMMAND is setting the window title in an xterm. check your TERM variable; if it doesn't start with "xterm" or "rxvt" then PROMPT_COMMAND isn't even getting run.
– quack quixote
Oct 25 '09 at 22:59
Oh, yeah, duh. Sorry, I mixed upper and lowercase. That worked. Thanks! :)
– hsribei
Oct 25 '09 at 23:39
Great answer. Make sure you reboot in order this to take effect. Thanks!
– Combine
May 31 '18 at 9:00
Hm... I'm afraid it's already w, but it seems like that
case statement overrides it when i'm on an xterm, and the problem seems to be with the PWD in the PROMPT_COMMAND line. Do you know what I should put there?– hsribei
Oct 25 '09 at 22:48
Hm... I'm afraid it's already w, but it seems like that
case statement overrides it when i'm on an xterm, and the problem seems to be with the PWD in the PROMPT_COMMAND line. Do you know what I should put there?– hsribei
Oct 25 '09 at 22:48
4
4
w (lower case) sets it to full path, W (uppercase) trims to the final bit. the PROMPT_COMMAND is setting the window title in an xterm. check your TERM variable; if it doesn't start with "xterm" or "rxvt" then PROMPT_COMMAND isn't even getting run.– quack quixote
Oct 25 '09 at 22:59
w (lower case) sets it to full path, W (uppercase) trims to the final bit. the PROMPT_COMMAND is setting the window title in an xterm. check your TERM variable; if it doesn't start with "xterm" or "rxvt" then PROMPT_COMMAND isn't even getting run.– quack quixote
Oct 25 '09 at 22:59
Oh, yeah, duh. Sorry, I mixed upper and lowercase. That worked. Thanks! :)
– hsribei
Oct 25 '09 at 23:39
Oh, yeah, duh. Sorry, I mixed upper and lowercase. That worked. Thanks! :)
– hsribei
Oct 25 '09 at 23:39
Great answer. Make sure you reboot in order this to take effect. Thanks!
– Combine
May 31 '18 at 9:00
Great answer. Make sure you reboot in order this to take effect. Thanks!
– Combine
May 31 '18 at 9:00
add a comment |
Simple bash replace command is
${VAR/pattern_to_find/pattern_to_replace}
For showing the last directory you can just do ${PWD/*//}, i.e. find any thing before and including the last '/' and replace it with nothing.
On my ubuntu machine I use:
export PS1='$(whoami):${PWD/*//}#'.
5
Only six years late!
– Burgi
Jun 7 '16 at 8:29
I like this answer better than the accepted one because it's generic to any situation instead of just the special parsing logic of $PS1. Fewer more powerful tools are easier to remember and compose. :)
– David Ellis
Jun 7 '18 at 17:03
add a comment |
Simple bash replace command is
${VAR/pattern_to_find/pattern_to_replace}
For showing the last directory you can just do ${PWD/*//}, i.e. find any thing before and including the last '/' and replace it with nothing.
On my ubuntu machine I use:
export PS1='$(whoami):${PWD/*//}#'.
5
Only six years late!
– Burgi
Jun 7 '16 at 8:29
I like this answer better than the accepted one because it's generic to any situation instead of just the special parsing logic of $PS1. Fewer more powerful tools are easier to remember and compose. :)
– David Ellis
Jun 7 '18 at 17:03
add a comment |
Simple bash replace command is
${VAR/pattern_to_find/pattern_to_replace}
For showing the last directory you can just do ${PWD/*//}, i.e. find any thing before and including the last '/' and replace it with nothing.
On my ubuntu machine I use:
export PS1='$(whoami):${PWD/*//}#'.
Simple bash replace command is
${VAR/pattern_to_find/pattern_to_replace}
For showing the last directory you can just do ${PWD/*//}, i.e. find any thing before and including the last '/' and replace it with nothing.
On my ubuntu machine I use:
export PS1='$(whoami):${PWD/*//}#'.
edited Jun 7 '16 at 8:29
Burgi
3,88792543
3,88792543
answered Jun 6 '16 at 22:11
stopBugsstopBugs
11112
11112
5
Only six years late!
– Burgi
Jun 7 '16 at 8:29
I like this answer better than the accepted one because it's generic to any situation instead of just the special parsing logic of $PS1. Fewer more powerful tools are easier to remember and compose. :)
– David Ellis
Jun 7 '18 at 17:03
add a comment |
5
Only six years late!
– Burgi
Jun 7 '16 at 8:29
I like this answer better than the accepted one because it's generic to any situation instead of just the special parsing logic of $PS1. Fewer more powerful tools are easier to remember and compose. :)
– David Ellis
Jun 7 '18 at 17:03
5
5
Only six years late!
– Burgi
Jun 7 '16 at 8:29
Only six years late!
– Burgi
Jun 7 '16 at 8:29
I like this answer better than the accepted one because it's generic to any situation instead of just the special parsing logic of $PS1. Fewer more powerful tools are easier to remember and compose. :)
– David Ellis
Jun 7 '18 at 17:03
I like this answer better than the accepted one because it's generic to any situation instead of just the special parsing logic of $PS1. Fewer more powerful tools are easier to remember and compose. :)
– David Ellis
Jun 7 '18 at 17:03
add a comment |
My solution is to show the top three and bottom 2 directories when there are more than 5
So my prompt (which has other info too) looks like:
08:38:42 durrantm U2017 /home/durrantm/Dropbox/_/rails/everquote
when my pwd is actually
/home/durrantm/Dropbox/93_2016/work/code/ruby__rails/rails/everquote
My PS1 prompt is setup as follows:
HOST='[33[02;36m]h'; HOST=' '$HOST
TIME='[33[01;31m]t [33[01;32m]'
LOCATION=' [33[01;34m]`pwd | sed "s#(/[^/]{1,}/[^/]{1,}/[^/]{1,}/).*(/[^/]{1,}/[^/]{1,})/{0,1}#1_2#g"`'
BRANCH=' [33[00;33m]$(git_branch)[33[00m]n$ '
PS1=$TIME$USER$HOST$LOCATION$BRANCH
git_branch is a function which shows current git branch, I keep it in my dotfiles, it is:
git_branch () {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* (.*)/1/'
}
add a comment |
My solution is to show the top three and bottom 2 directories when there are more than 5
So my prompt (which has other info too) looks like:
08:38:42 durrantm U2017 /home/durrantm/Dropbox/_/rails/everquote
when my pwd is actually
/home/durrantm/Dropbox/93_2016/work/code/ruby__rails/rails/everquote
My PS1 prompt is setup as follows:
HOST='[33[02;36m]h'; HOST=' '$HOST
TIME='[33[01;31m]t [33[01;32m]'
LOCATION=' [33[01;34m]`pwd | sed "s#(/[^/]{1,}/[^/]{1,}/[^/]{1,}/).*(/[^/]{1,}/[^/]{1,})/{0,1}#1_2#g"`'
BRANCH=' [33[00;33m]$(git_branch)[33[00m]n$ '
PS1=$TIME$USER$HOST$LOCATION$BRANCH
git_branch is a function which shows current git branch, I keep it in my dotfiles, it is:
git_branch () {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* (.*)/1/'
}
add a comment |
My solution is to show the top three and bottom 2 directories when there are more than 5
So my prompt (which has other info too) looks like:
08:38:42 durrantm U2017 /home/durrantm/Dropbox/_/rails/everquote
when my pwd is actually
/home/durrantm/Dropbox/93_2016/work/code/ruby__rails/rails/everquote
My PS1 prompt is setup as follows:
HOST='[33[02;36m]h'; HOST=' '$HOST
TIME='[33[01;31m]t [33[01;32m]'
LOCATION=' [33[01;34m]`pwd | sed "s#(/[^/]{1,}/[^/]{1,}/[^/]{1,}/).*(/[^/]{1,}/[^/]{1,})/{0,1}#1_2#g"`'
BRANCH=' [33[00;33m]$(git_branch)[33[00m]n$ '
PS1=$TIME$USER$HOST$LOCATION$BRANCH
git_branch is a function which shows current git branch, I keep it in my dotfiles, it is:
git_branch () {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* (.*)/1/'
}
My solution is to show the top three and bottom 2 directories when there are more than 5
So my prompt (which has other info too) looks like:
08:38:42 durrantm U2017 /home/durrantm/Dropbox/_/rails/everquote
when my pwd is actually
/home/durrantm/Dropbox/93_2016/work/code/ruby__rails/rails/everquote
My PS1 prompt is setup as follows:
HOST='[33[02;36m]h'; HOST=' '$HOST
TIME='[33[01;31m]t [33[01;32m]'
LOCATION=' [33[01;34m]`pwd | sed "s#(/[^/]{1,}/[^/]{1,}/[^/]{1,}/).*(/[^/]{1,}/[^/]{1,})/{0,1}#1_2#g"`'
BRANCH=' [33[00;33m]$(git_branch)[33[00m]n$ '
PS1=$TIME$USER$HOST$LOCATION$BRANCH
git_branch is a function which shows current git branch, I keep it in my dotfiles, it is:
git_branch () {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* (.*)/1/'
}
answered May 15 '17 at 12:42
Michael DurrantMichael Durrant
5781520
5781520
add a comment |
add a comment |
My solution for this is slightly different in regards to how do I export it, so thought I'd share it here:
Create another prompt string variable; PS5 and export the following string in your .profile / .bash_profile file:
u: Display the current username .
W: Print the base of current working directory.
# Display username and current directory only.
export PS5='export PS1="u:W$";';
Now whenever you need to use the shorthand-ed PS, just run: eval $PS5
Or even better yet, create an alias in your .bash_aliases file: (thanks to @muru)
alias PS5='export PS1="u:W$";';
source again, and now you can just type PS5 to switch.
1
This looks like a frankenalias. Why not just use an alias or a function?
– muru
Aug 19 '18 at 1:31
@muru Thanks, this is much better, answer updated.
– U-ways
Aug 19 '18 at 11:17
add a comment |
My solution for this is slightly different in regards to how do I export it, so thought I'd share it here:
Create another prompt string variable; PS5 and export the following string in your .profile / .bash_profile file:
u: Display the current username .
W: Print the base of current working directory.
# Display username and current directory only.
export PS5='export PS1="u:W$";';
Now whenever you need to use the shorthand-ed PS, just run: eval $PS5
Or even better yet, create an alias in your .bash_aliases file: (thanks to @muru)
alias PS5='export PS1="u:W$";';
source again, and now you can just type PS5 to switch.
1
This looks like a frankenalias. Why not just use an alias or a function?
– muru
Aug 19 '18 at 1:31
@muru Thanks, this is much better, answer updated.
– U-ways
Aug 19 '18 at 11:17
add a comment |
My solution for this is slightly different in regards to how do I export it, so thought I'd share it here:
Create another prompt string variable; PS5 and export the following string in your .profile / .bash_profile file:
u: Display the current username .
W: Print the base of current working directory.
# Display username and current directory only.
export PS5='export PS1="u:W$";';
Now whenever you need to use the shorthand-ed PS, just run: eval $PS5
Or even better yet, create an alias in your .bash_aliases file: (thanks to @muru)
alias PS5='export PS1="u:W$";';
source again, and now you can just type PS5 to switch.
My solution for this is slightly different in regards to how do I export it, so thought I'd share it here:
Create another prompt string variable; PS5 and export the following string in your .profile / .bash_profile file:
u: Display the current username .
W: Print the base of current working directory.
# Display username and current directory only.
export PS5='export PS1="u:W$";';
Now whenever you need to use the shorthand-ed PS, just run: eval $PS5
Or even better yet, create an alias in your .bash_aliases file: (thanks to @muru)
alias PS5='export PS1="u:W$";';
source again, and now you can just type PS5 to switch.
edited Aug 19 '18 at 11:17
answered Aug 18 '18 at 21:13
U-waysU-ways
1215
1215
1
This looks like a frankenalias. Why not just use an alias or a function?
– muru
Aug 19 '18 at 1:31
@muru Thanks, this is much better, answer updated.
– U-ways
Aug 19 '18 at 11:17
add a comment |
1
This looks like a frankenalias. Why not just use an alias or a function?
– muru
Aug 19 '18 at 1:31
@muru Thanks, this is much better, answer updated.
– U-ways
Aug 19 '18 at 11:17
1
1
This looks like a frankenalias. Why not just use an alias or a function?
– muru
Aug 19 '18 at 1:31
This looks like a frankenalias. Why not just use an alias or a function?
– muru
Aug 19 '18 at 1:31
@muru Thanks, this is much better, answer updated.
– U-ways
Aug 19 '18 at 11:17
@muru Thanks, this is much better, answer updated.
– U-ways
Aug 19 '18 at 11:17
add a comment |
Show only current directory name (not full path) on bash prompt
Most of the other solutions did not work for me across all of my OSes that share my dot files: AIX, Windoze, and Linux. The bash ports were old versions that didn't support certain constructs and I didn't want to fork another process (i.e. sed, awk, etc.) which is noticeably expensive under cygwin.
The following is long but performant:
# takes a number argument of the number of last dirs to show
function DIR_LAST {
# read -a didn't seem to work under bash 3
IFS='/' array=($PWD)
len=${#array[@]}
start=$((len - $1))
# leading / if fewer dir args: /usr/flastname not usr/flastname
if (( $start <= 1 )); then
start=1
echo -n /
fi
for (( i = $start; $i < $len; i++ )); do
if (( $i > $start )); then
echo -n /
fi
echo -n ${array[$i]}
done
}
export PS1="$(DIR_LAST 2) {$(hostname)} "
I want it to spit out:
/
/usr
/usr/foo
foo/bin
Notice the lack of a leading slash in the last line which is how I like it. Also, you can spit out the last 3 directories by changing the arg to DIR_LAST.
Also, I tried to do this with a regex and BASH_REMATCH but bash v3 didn't seem to like the parens and I couldn't figure out how to properly escape them. Sigh.
add a comment |
Show only current directory name (not full path) on bash prompt
Most of the other solutions did not work for me across all of my OSes that share my dot files: AIX, Windoze, and Linux. The bash ports were old versions that didn't support certain constructs and I didn't want to fork another process (i.e. sed, awk, etc.) which is noticeably expensive under cygwin.
The following is long but performant:
# takes a number argument of the number of last dirs to show
function DIR_LAST {
# read -a didn't seem to work under bash 3
IFS='/' array=($PWD)
len=${#array[@]}
start=$((len - $1))
# leading / if fewer dir args: /usr/flastname not usr/flastname
if (( $start <= 1 )); then
start=1
echo -n /
fi
for (( i = $start; $i < $len; i++ )); do
if (( $i > $start )); then
echo -n /
fi
echo -n ${array[$i]}
done
}
export PS1="$(DIR_LAST 2) {$(hostname)} "
I want it to spit out:
/
/usr
/usr/foo
foo/bin
Notice the lack of a leading slash in the last line which is how I like it. Also, you can spit out the last 3 directories by changing the arg to DIR_LAST.
Also, I tried to do this with a regex and BASH_REMATCH but bash v3 didn't seem to like the parens and I couldn't figure out how to properly escape them. Sigh.
add a comment |
Show only current directory name (not full path) on bash prompt
Most of the other solutions did not work for me across all of my OSes that share my dot files: AIX, Windoze, and Linux. The bash ports were old versions that didn't support certain constructs and I didn't want to fork another process (i.e. sed, awk, etc.) which is noticeably expensive under cygwin.
The following is long but performant:
# takes a number argument of the number of last dirs to show
function DIR_LAST {
# read -a didn't seem to work under bash 3
IFS='/' array=($PWD)
len=${#array[@]}
start=$((len - $1))
# leading / if fewer dir args: /usr/flastname not usr/flastname
if (( $start <= 1 )); then
start=1
echo -n /
fi
for (( i = $start; $i < $len; i++ )); do
if (( $i > $start )); then
echo -n /
fi
echo -n ${array[$i]}
done
}
export PS1="$(DIR_LAST 2) {$(hostname)} "
I want it to spit out:
/
/usr
/usr/foo
foo/bin
Notice the lack of a leading slash in the last line which is how I like it. Also, you can spit out the last 3 directories by changing the arg to DIR_LAST.
Also, I tried to do this with a regex and BASH_REMATCH but bash v3 didn't seem to like the parens and I couldn't figure out how to properly escape them. Sigh.
Show only current directory name (not full path) on bash prompt
Most of the other solutions did not work for me across all of my OSes that share my dot files: AIX, Windoze, and Linux. The bash ports were old versions that didn't support certain constructs and I didn't want to fork another process (i.e. sed, awk, etc.) which is noticeably expensive under cygwin.
The following is long but performant:
# takes a number argument of the number of last dirs to show
function DIR_LAST {
# read -a didn't seem to work under bash 3
IFS='/' array=($PWD)
len=${#array[@]}
start=$((len - $1))
# leading / if fewer dir args: /usr/flastname not usr/flastname
if (( $start <= 1 )); then
start=1
echo -n /
fi
for (( i = $start; $i < $len; i++ )); do
if (( $i > $start )); then
echo -n /
fi
echo -n ${array[$i]}
done
}
export PS1="$(DIR_LAST 2) {$(hostname)} "
I want it to spit out:
/
/usr
/usr/foo
foo/bin
Notice the lack of a leading slash in the last line which is how I like it. Also, you can spit out the last 3 directories by changing the arg to DIR_LAST.
Also, I tried to do this with a regex and BASH_REMATCH but bash v3 didn't seem to like the parens and I couldn't figure out how to properly escape them. Sigh.
edited Feb 13 '18 at 14:14
answered Feb 12 '18 at 22:32
GrayGray
25229
25229
add a comment |
add a comment |
I believe this option is much easier, by simply doing:
echo $PWD | rev | cut -d '/' -f 1 | rev
So assign this to the PS1 variable in your .bashrc file:
PS1='$(PWD | rev | cut -d '/' -f 1 | rev)'
add a comment |
I believe this option is much easier, by simply doing:
echo $PWD | rev | cut -d '/' -f 1 | rev
So assign this to the PS1 variable in your .bashrc file:
PS1='$(PWD | rev | cut -d '/' -f 1 | rev)'
add a comment |
I believe this option is much easier, by simply doing:
echo $PWD | rev | cut -d '/' -f 1 | rev
So assign this to the PS1 variable in your .bashrc file:
PS1='$(PWD | rev | cut -d '/' -f 1 | rev)'
I believe this option is much easier, by simply doing:
echo $PWD | rev | cut -d '/' -f 1 | rev
So assign this to the PS1 variable in your .bashrc file:
PS1='$(PWD | rev | cut -d '/' -f 1 | rev)'
answered May 26 '17 at 14:17
Dani MachDani Mach
11
11
add a comment |
add a comment |
root:~/project# -> root:~/project(dev)#
add the following code to the end of your ~/.bashrc
force_color_prompt=yes
color_prompt=yes
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* (.*)/(1)/'
}
if [ "$color_prompt" = yes ]; then
PS1='${debian_chroot:+($debian_chroot)}[33[01;32m]u@h[33[00m]:[33[01;34m]w[33[01;31m]$(parse_git_branch)[33[00m]$ '
else
PS1='${debian_chroot:+($debian_chroot)}u@h:w$(parse_git_branch)$ '
fi
unset color_prompt force_color_prompt
add a comment |
root:~/project# -> root:~/project(dev)#
add the following code to the end of your ~/.bashrc
force_color_prompt=yes
color_prompt=yes
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* (.*)/(1)/'
}
if [ "$color_prompt" = yes ]; then
PS1='${debian_chroot:+($debian_chroot)}[33[01;32m]u@h[33[00m]:[33[01;34m]w[33[01;31m]$(parse_git_branch)[33[00m]$ '
else
PS1='${debian_chroot:+($debian_chroot)}u@h:w$(parse_git_branch)$ '
fi
unset color_prompt force_color_prompt
add a comment |
root:~/project# -> root:~/project(dev)#
add the following code to the end of your ~/.bashrc
force_color_prompt=yes
color_prompt=yes
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* (.*)/(1)/'
}
if [ "$color_prompt" = yes ]; then
PS1='${debian_chroot:+($debian_chroot)}[33[01;32m]u@h[33[00m]:[33[01;34m]w[33[01;31m]$(parse_git_branch)[33[00m]$ '
else
PS1='${debian_chroot:+($debian_chroot)}u@h:w$(parse_git_branch)$ '
fi
unset color_prompt force_color_prompt
root:~/project# -> root:~/project(dev)#
add the following code to the end of your ~/.bashrc
force_color_prompt=yes
color_prompt=yes
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* (.*)/(1)/'
}
if [ "$color_prompt" = yes ]; then
PS1='${debian_chroot:+($debian_chroot)}[33[01;32m]u@h[33[00m]:[33[01;34m]w[33[01;31m]$(parse_git_branch)[33[00m]$ '
else
PS1='${debian_chroot:+($debian_chroot)}u@h:w$(parse_git_branch)$ '
fi
unset color_prompt force_color_prompt
answered Jan 17 at 7:06
Ali AlpAli Alp
11
11
add a comment |
add a comment |
Thanks for contributing an answer to Super User!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f60555%2fshow-only-current-directory-name-not-full-path-on-bash-prompt%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown