I have the following in my .bash_profile:
PS1='\h:\w$ '
But my prompt looks like this:
laptop:~$
What setting do I need to modify so that the "~" instead prints out the correct path... in this instance it should be /Users/jay/
The jay account is the default user, so is there a way to change this?
This is expected. Tilde is a very well known shorthand for active user's home folder.
If you look at the help for Bash
man bash
and then type ( you may need to hit 'n' key a couple of times to get to the section about PROMPTING)
/PROMPTING
You will notice that it says
\w the current working directory, with $HOME abbreviated with a tilde
\W the basename of the current working directory, with $HOME abbreviated with a tilde
That being said, if you really want it to print the full path instead, you can use another variable: $PWD (Peek Working Directory) to replace your \w or \W
So, type:
echo $PS1
If for example that returns
\h:\W \u\$
Type
PS1='\h:$PWD \u\$ '
That should change it (it works on my 10.11)
Related
I'd like the include a dynamic variable in my PS1 prompt, let's say the 5th folder in the path. I'd also like to include some other PS1 codes (maybe color, username or current directory).
I have a script to get the 5th folder and echo it with one of the escape PS1 codes.
demo_prompt.sh
folder5=$(cut -d / -f 6 <<< $PWD)<br>
echo "$folder5 \W $ "
This .bashrc sets PS1 to the output of the script.
.bashrc
PS1='$(~/demo_prompt.sh)'
If I keep the PS1 definition in .bashrc in single quotes:
Pro: The 5th folder dynamically updates while I change directories as desired,
Con: \W appears in the prompt rather than resolving to the current folder name.
If I change the PS1 definition in .bashrc from single quotes to double quotes:
Pro: \W resolves properly to be the current directory
Con: The 5th folder is fixed to the value when I source .bashrc
How can I achieve both the \W resolving and the 5th folder dynamically updating?
I've more or less followed the idea here and am essentially asking the followup question that went unanswered. Bash: How to set changing variable in PS1, updating every prompt)
Quote: "I.e. it won't read in the escape codes nor color options. Any suggestions on this one?"
Thanks!
Try this :
demo_prompt.sh
folder5='$(cut -d / -f 6 <<< $PWD)\n'
echo "$folder5 \W $ "
and .bashrc
PS1="$(~/demo_prompt.sh)"
PROMPT_COMMAND='dir5=$(cut -d / -f 3 <<< "$PWD")'
PS1='$dir5 \W $ '
(Note the quotes)
PROMPT_COMMAND runs before the prompt is displayed and allows you to set global variables. You can also set it to a function call that would set global variables, call scripts, or call your script directly from PROMPT_COMMAND.
I have environment variable export MY_WORK_DIR="~/project".
While I'm using below command, it give me an error:
realpath $MY_WORK_DIR
realpath: '~/project': No such file or directory
In my guess, the ~ is not processed while using this env variable.
BTW, export MY_WORK_DIR=~/project is not an option for me. ~ should be in the string.
Could you please guide me how to get real path from envrionment variable ~/project ?
EDIT
Sorry. The variable is from other app so I cannot modify the environment variable which contains tilde. (Storing variable with tilde expanded form is not an option).
EDIT2
Is it safe to use eval command like this? eval "echo ${MY_WORK_DIR}". It works for my use.
I wouldn't use eval if I can avoid it. Especially in the way you are doing it, this is an invitation to do havoc by embedding dangerous code into MY_WORK_DIR.
A cheap solution for your concrete example would be to do a
if [[ ${MY_WORK_DIR:0:1} == '~' ]]
then
MY_WORK_DIR="$HOME/${MY_WORK_DIR:1}"
fi
which chops off the annoying ~ and prepends your home directory. But this would fail if MY_WORK_DIR is set to, say, ~einstein/project.
In this case, you would have to extract the user name (einstein) and search the home directory for this user.
Following steps can provide a resolution:
You need to replace "~" with the full path of the project directory.
Use pwd command to identify the full path of the project directory; e.g. /root/Documents/project is the full path you get.
Execute this command export MY_WORK_PROJECT=/root/Documents/project
Execute this command echo $MY_WORK_PROJECT so you should get this result
/root/Documents/project
I was wondering if there was any way to adjust how the terminal on Mac looks. Mainly, is there any way I could change the '$' after my name to be something like '>' after my name. Thanks!
To get a non-permanent preview you can just do
export PS1=">"
To make this permanent add this command to your .bash_profile.
To add the date, directory or other information, you can add any of the following to the text inside the quotes after PS1= :
\d – Current date
\t – Current time
\h – Host name
# – Command number
\u – User name
\W – Current working directory
\w – Current working directory with full path
Suddendly my bash prompt changed to
[80-254-70-241]myusername # ~ $
when in my home directory.
I have the following command in my .bash_profile export PS1="\[\033[32m\][\h]\u # \W \$\[\033[0m\]
I think the expression in square brackets supposed to be the name of the current directory.
What does this number 80-254-70-241 mean and how do I change it back?
The string "80-254-70-241" comes from the parameter "\h", which denotes your hostname. I think you got an IP and a hostname from DHCP so the prompt changed accordingly.
If you think this is annoying, you can replace "\h" with your preferred hostname (hardcoded).
I have a Vim script that calls an external shell script and reads the output into the current buffer. It works fine in Unix systems. I'm trying to make the script OS-agnostic, so I want it to work for Windows users too. Here's the relevant line from the script:
:exe ":0r !$HOME/bin/shell_script"
According to Vim's docs, $HOME should translate fine for Windows users too. Using gvim on Win XP in command mode, doing
:echo $HOME
does indeed produce "C:\Documents and Settings\my_user".
However, my Vim script (adjusted for backslashes) fails on the Windows machine with a message in the DOS cmd.exe window saying
$HOME\bin\shell_script" not found.
In other words, Vim appears not to be expanding the value of $HOME before passing it to cmd.exe.
I can't use %APPDATA% either because Vim interprets % as the current file and pre/appends the file name to APPDATA. Weird that Vim does expand % but doesn't expand $HOME.
How do I get $HOME expanded correctly? Is it because I'm using exe in the vim script?
You don't need ! to read a file.
:exe ":0r $HOME/bin/shell_script"
Or read type command in windows(like cat in linux):
:exe '0r !type "'. $HOME . '\bin\shell_script"'
Note:
the type is executed in windows shell, so you need \(backslash) in path
if $HOME contains spaces, you need "(double-quote) to preserves the literal value of spaces
To clarify the answer given by kev:
On windows the $HOME variable do not expand properly when you escape to the console. For example, consider this code:
:e $HOME/myscript
This works because vim expands $HOME as normal. On the other hand this won't work:
:%! $HOME/myscript
Why? Because vim passes everything after ! to the underlying shell, which on Windows is cmd.exe which does environment variables %LIKE_THIS%. If you try to use that notation, vim will jump in and expand % to the name of the current file - go figure.
How to get around it? Use exe keyword:
:exe "%! ".$HOME."\myscript"
Let's analyze:
:exe is a command that takes a string and evaluates it (think eval in most languages)
"!% " the escape to shell command. Note that it is quoted so that exe can evaluate it. Also note how there is an extra space there so that when we append the home var it does not but right against it
.$HOME the dot is a string concatenation symbol. The $HOME is outside of the quotes but concatenated so that vim can expand it properly
."/myscript" path to script and possible arguments (also quoted)
The important thing here is keeping $HOME outside of the quote marks, otherwise it may not be properly expanded on Windows.
You probably need something like the expand function. For example:
:echo expand("$HOME/hello")
/home/amir/hello
You can find out more info about expand() with :help expand.
Here you have some information about slashes and backslashes in vim:
http://vimdoc.sourceforge.net/htmldoc/os_dos.html
When you prefer to use forward slashes, set the 'shellslash' option.
Vim will then replace backslashes with forward slashes when expanding
file names. This is especially useful when using a Unix-like 'shell'.