4

Is there any way to change the name which appears before every command line in the terminal window? If so, then how? As shown below, my current one is way too long and takes up a lot of space in each line.

$: command not found
liam@liam-Lenovo-Legion-Y7000:~$
5
  • 1
    Welcome to Ask Ubuntu. Please don't put screenshots of the terminal. Always copy the text from the terminal and paste it directly in your question. Then format the pasted text as code by using the {_} icon above the edit question window. Please edit your question and indicate if you just want to change what is displayed on the terminal or you want to change your username? Changing your username may affect other things such as your "Home" folder /home/liam to /home/harry.
    – user68186
    Jun 5, 2023 at 20:00
  • 2
    The long part after the @ symbol is your computer's hostname not your username. See for example Change computer name Ubuntu Jun 5, 2023 at 20:08
  • Open a terminal and copy and paste this command: PS1='${debian_chroot:+($debian_chroot)}Harry@MyPC:\w\$ ' This will change your name to Harry and your computer's name to MyPC, but only temporarily and only as shown in the command prompt. Alternately you may change your computer's name to something short and simple like (Mercedes) "Benz" rather than "liam-Lenovo-Legion-Y7000".
    – user68186
    Jun 5, 2023 at 20:18
  • Because I have multiple installs and a couple of systems, I like to have short name for system & version. "fred@Z170-jammy:~$ "
    – oldfred
    Jun 5, 2023 at 21:19
  • It may not be clear, especially to people new to Unix, but that prompt is extremely useful, not just for knowing which userid and system you are currently using, but for commands like scp and rsync to quickly move files and directories around. For example: ` $ scp ~/.bash_aliases joe@romulus: ` or $ rsync -avz ~/.bash* joe@romulus: These will copy the files matched by the glob pattern to the userid "joe" HOME on romulus.
    – JohnP
    Jun 13, 2023 at 11:44

2 Answers 2

5

The long part is the computer name, also known as your host name: liam-Lenovo-Legion-Y7000

The easiest way would be to change your computer name to something like liam so that the prompt would appear like this:

liam@liam:~$

See this duplicate answer for how to change your computer name using the hostnamectl command.

In your case and to change your host name to liam, you could use the following command:

sudo hostnamectl set-hostname liam

Here is another duplicate question for how to change your device-name.


Alternatively, the following is a more complicated method that removes the computer name from your prompt.

Without changing the actual computer name, you can remove the computer name from the prompt by editing your ~/.bashrc file.

Before we begin, make a backup of this file:

cp ~/.bashrc ~/.bashrcbackup

Run the following command to use nano to edit the file:

nano ~/.bashrc

1. Press CTRL+W to search and then type PS1 and then press ENTER.

The line should look like this:

PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '

Edit the line to remove @\h\[\033[00m\] up to the : but do not remove the : so the edited line should look like this:

PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u:\[\033[01;34m\]\w\[\033[00m\]\$ '

2. Press CTRL+W and then press ENTER.

This line should look like this:

PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '

Edit the line to remove @\h so the edited line should look like this:

PS1='${debian_chroot:+($debian_chroot)}\u:\w\$ '

3. Press CTRL+W and then press ENTER.

This line should look like this:

PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"

Edit the line to remove @\h so the edited line should look like this:

PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u: \w\a\]$PS1"

When you are done editing the file, press CTRL+o to save the changes and then press CTRL+x to exit nano.

Finally, run the following command to apply the changes:

source ~/.bashrc

Your prompt should now appear as the following:

liam:~$
2
  • 2
    The only other thing I’d suggest to this great answer is that you can experiment freely in a terminal - just type PS1=<whatever you want to try out > and it will have immediate effect - like it and you can edit into .bashrc as above, and if you don’t like it, the changes disappear as soon as you close that terminal.
    – Will
    Jun 5, 2023 at 20:41
  • This just solved my proplem , thank you very much !
    – liam
    Jun 7, 2023 at 8:54
1

Most of the length of your prompt is your hostname (too long for me to retype - one of the disadvantages of posting pictures of text, rather than posting formatted text. Please read https://askubuntu.com/help/how-to-ask and https://askubuntu.com/help/formatting .). Can you shorten the hostname?

The prompt is produced through shell magic, using the PS1 through PS4 environment variables, This magic is explained in man $SHELL, the "PROMPTING" section.

Not the answer you're looking for? Browse other questions tagged .