Editing ~/.bash_profile doesn't customize command-line prompt properly - bash

Related Issue: How to change the terminal prompt to just current directory?
I have added export PS1="\w\$ " to my ~/.bash_profile, but the prompt for the command line just displays this:
\w$
It recognizes the backslash escape for the $ character, but not for the filepath. It does the same thing when I use a capital 'W'.

The problem was that my terminal is running zsh. I created ~/.zshrc and wrote the following code:
PROMPT="%/\$ "
I referred to this resource to find the appropriate characters for displaying the current file path: ZSH Prompt Expansion.

Related

Open shell variable (assigned with path) using vim editor

I am assigning a shell variable say TEST1 in .bashrc like below:
TEST1='x/y'
In my Present directory (pwd), i have folders in following hierarchy:
pwd/x/y/ztext.c
I am at pwd. I want to open ztext.c file from bash shell by executing
vim $TEST1/ and Tab key
I expected shell will list ztext.c in screen. But it didnt. Since i know the file name, i entered like below:
vim $TEST1/ztext.c
vim was opening new file and not the existing file.
But cd $TEST1 is moving to x/y folder path. Problem is only when executed with vim like mentioned above
Please correct me what am i doing wrong here.
Note : I tried export and set keyword for the TEST1 variable. It didnt help. Observation is same.
I don't why it's not working - I just did it on my machine. Below I show
exactly what I'm seeing in my terminal:
mattb#flat:~/mytest
$ l -R
.:
x/
./x:
y/
./x/y:
ztest.c
mattb#flat:~/mytest
$ export TEST1='x/y'
mattb#flat:~/mytest
$ vim $TEST1/
Pressing Tab expands to:
vim x/y/ztest.c

Mac export PS1 doesn't handle escaped commands

I'm just trying to change the terminal prompt in macOS Sierra. Nothing fancy, I just need to know the current path.
In my ~/.bash_profile I'm adding
export PS1="[\w] > "
I know that \w should be replaced by current full path but none of the escape characters seems to work on my Mac. I just get the same string without evaluating, so my prompt looks like
[\w] >
I've tried many different PS1 strings without luck. I also tried using .bashrc instead bash_profile.
Any ideas?
I suspect you're running a shell that doesn't understand the \w escape in the prompt string. If echo $0 returns something other than -bash, you have to use whatever that shell understands:
Zsh prompt expansion
PS1 in the POSIX shell

ZSH aliases not found

When I open my ~/.zshrc file and add alias homestead=“cd ~/Homestead”, I expect to be able to type homestead and be taken to the Homestead folder.
Instead I get the following error:
zsh: command not found: “cd
Even when I use single quotes, i.e. alias homestead='cd ~/Homestead' and run source ~/.zshrc I get the same error.
UPDATE: Also, when I run which homestead I get homestead: aliased to "cd
How can I fix this?
The answer was to open ~/.zshrc in Sublime Text as opposed to TextEdit and to check that the " were coming up as 042 in an octal dump.
You don't need to define this alias at all in zsh. Add the following to your .zshrc:
setopt autocd
cdpath+=(~)
The first allows you to treat a directory name as a command, which implicitly sets the working directory of the current shell to the named directory. The second specifies that if the current directory doesn't have a directory whose name is used with cd (or by itself with autocd set), then try to find it in a directory named in the cdpath parameter.
With these two, simply typing Homestead will first try to run a command named Homestead; failing that, it tries to cd to ./Homestead, and failing that, will finally succeed in cding to ~/Homestead.
The double quotes must be ASCII, not Unicode outside the ASCII range. Load the file in your editor, disable any automatic mangling of single quotes and double quotes. Then replace the funny quotes with ASCII quotes " (code decimal 34, hex 22, octal 042). Or type the command at a prompt, then cut & paste it in your editor. If all else fails, add the alias at the end of your .zshrc with
printf 'alias homestead="cd ~/homestead"' >> ~/.zshrc
Verify the result with octal dump,
od -bc .zshrc
The number above the quotes should appear as 042.
Maybe your locale settings is auto correcting a double quote " into a localized double quote “ as you posted. Since this is not recognized as a valid quote in shell, a simple white-space would break the string. So the actual alias is “cd.
As to why alias homestead='cd ~/Homestead' does not work, it seems you changed the alias in ~/.zshrc. From the which homestead result, it can be seen that alias homestead='cd ~/Homestead' does not really work. Maybe there is another line of alias homestead=“cd ~/Homestead” hidden in .zshrc after it.
Just saying that for me what fixed it was an error in the first alias in my list that had a question mark in it.
Just switched to Mac OS Catalina and ~/.bashrc to ~/.zshrc and I guess zsh doesn't support question marks.
Maybe it'll help someone coming here from Google search like I did.

Modifying bash prompt

When I open my terminal in Mac OS X, the command prompt current reads:
James-MacBook:project1 sam$
project1 is the name of the current directory.
What I want is to display the full path instead of James-MacBook.
How do I achieve this?
Your current prompt appears to show the hostname and the basename of your current directory. That means that the bash prompt, PS1, is likely set to:
PS1='\h:\W\$ '
To get the full directory name, use \w in place of \W:
PS1='\h:\w\$ '
You can set this at the command prompt. To make it permanent, this command can go into ~/.bashrc or ~/.bash_profile or other depending on how your system is configured.
You can read more about the options for command prompts, for which there are many options, in the PROMPTING section of man bash. Regarding the \w and \W options mentioned above, man bash explains how they are used:
\wthe current working directory, with $HOME abbreviated with a tilde (uses the value of the PROMPT_DIRTRIM
variable)
\W the basename of the current working directory, with $HOME abbreviated with a tilde
Easy. This is not an OS X thing, but a bash thing. Try this:
export PS1='$(pwd): '
Then if you want to make it permanent, just edit your .bash_profile:
nano ~/.bash_profile
And place that first command in there.

How to suppress (or customize) Mac Terminal shell prompt

Currently in my Terminal, every shell prompt looks like ComputerName: FooDir UserName$. The UserName part simply wastes too much space out of my precious 80 columns. Is there a way to suppress it?
The prompt is defined by the environment variable PS1 which you can define in .bash_profile.
To edit it, open or create the (hidden) file .bash_profile:
nano .bash_profile
and add a line that says
export PS1=""
Between the quotation marks, you can insert what you would like as your terminal prompt. You can also use variables there:
\d – date
\t – time
\h – hostname
\# – command number
\u – username
\W – current directory (e.g.: Desktop)
\w – current directory path (e.g.: /Users/Admin/Desktop)
The default prompt for common Linux distributions would be \w $, which evaluates to ~ $ in your home directory or e.g. /Users $ somewhere else. There are also website (like this one) that can help you with building your prompt.
If you want to remove the UserName part, your choice would be \h: \w$.
Once you made your changes, save the file with Control+o, Return, Control+x.
Here's an excellent article with a full list of Variables and Colors:
Customize your Shell Command Prompt
For a simple, minimalistic prompt, you can try this. Add the following line to your .bash_profile or simply test it first by running it in your terminal:
export PS1="\[\033[0m\]\w\$ "
It'll look something like this:
Here's my Prompt (source), also very simple:
export PS1="\[\033[1;97m\]\u: \[\033[1;94m\]\w \[\033[1;97m\]\$\[\033[0m\] "
2019 onwards, MacOS default shell is Z Shell. To customize command prompt, add a file named .zshrc in user home and put following line that sets a PS1 environment variable with desired prompt format:
export PS1="[%n]%~> "
Open new terminal
This is result of following format expansion:
%n User name
%~ Current directory
See full list of available expansions here.
Your answer can be found right here:http://www.hypexr.org/bash_tutorial.php#vi at about the middle of the page. :)

Resources