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:~$
mchid
  • 44,904
  • 8
  • 102
  • 162
liam
  • 43

2 Answers2

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:~$
mchid
  • 44,904
  • 8
  • 102
  • 162
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.

waltinator
  • 37,856