Tag Info

Hot answers tagged

43

You can use any printable character, bash doesn't mind. You'll probably want to configure your terminal to support Unicode (in the form of UTF-8). There are a lot of characters in Unicode, so here are a few tips to help you search through the Unicode charts: You can try to draw the character on Shapecatcher. It tries to recognize a Unicode character in ...


40

Here are a couple of things you can do: Editors + Code A lot of editors have syntax highlighting support. vim and emacs have it on by default. You can also enable it under nano. You can also syntax highlight code on the terminal by using Pygments as a command-line tool. grep grep --color=auto highlights all matches. You can also use export ...


40

The UNIX standard provides example code for this using the locale utility: if printf "%s\n" "$response" | grep -Eq "$(locale yesexpr)" then affirmative processing goes here else non-affirmative processing goes here fi The value for 'yesexpr' in the POSIX locale (and on English locales on real systems) is "^[yY]". It is to be interpreted as an ...


23

Tmux sets the TMUX environment variable in tmux sessions, and sets TERM to screen. This isn't a 100% reliable indicator (for example, you can't easily tell if you're running screen inside tmux or tmux inside screen), but it should be good enough in practice. if ! { [ "$TERM" = "screen" ] && [ -n "$TMUX" ]; } then PS1="@$HOSTNAME $PS1" fi If you ...


20

I also use: export TERM=xterm-color export GREP_OPTIONS='--color=auto' GREP_COLOR='1;32' export CLICOLOR=1 export LSCOLORS=ExFxCxDxBxegedabagacad And if you like colorizing your prompt, defined color vars can be useful: export COLOR_NC='\e[0m' # No Color export COLOR_WHITE='\e[1;37m' export COLOR_BLACK='\e[0;30m' export COLOR_BLUE='\e[0;34m' export ...


19

What's going on is that Bash is getting confused about the number of printing characters in your prompt. It sends cursor positioning sequences to the terminal to position the cursor properly for doing command history and such. It needs to have a good idea of where the cursor actually is after printing the prompt. Try setting your prompt to this: ...


17

Find where your PS1 is set in your .bashrc and insert '\[\e[1m\]' at the beginning and \[\e[0m\] at the end. \[ and \] are necessary so the shell knows the mess inside takes up 0 space on the screen, which prevents some screwed up behavior when doing line-editing. You don't need to worry too much about it. \e[ is known as the CSI (control sequence ...


16

The following settings seem to work. The text on the second line disappears if the command line overflows the first line. The preexec function erases the second line before running the command; if you want to keep it, change to preexec () { echo; }. terminfo_down_sc=$terminfo[cud1]$terminfo[cuu1]$terminfo[sc]$terminfo[cud1] PS1_2='[some status]' ...


16

Using a case is somewhat equivalent but not perfect since statements like YE are accepted. read -p 'Answer this question with yes: ' answer case "${answer}" in [yY]|[yY][eE][sS]) echo 'Surely this can be written better?' ;; esac


15

The text displayed before the login prompt is stored in /etc/issue (there's a related file, /etc/motd, that's displayed after the user logs in, before their shell is started). It's just a normal text file, but it accepts a bunch of escape sequences: \b -- Baudrate of the current line. \d -- Current date. \s -- System name, the name of the operating system. ...


13

Try putting single quotes around the variable value at assignment to delay evaluation: RPROMPT='${vcs_info_msg_0_}'


13

Bash has a precommand hook. Sort of. preexec () { clear } preexec_invoke_exec () { [ -n "$COMP_LINE" ] && return # do nothing if completing [ "$BASH_COMMAND" = "$PROMPT_COMMAND" ] && return # don't cause a preexec for $PROMPT_COMMAND local this_command=`history 1 | sed -e "s/^[ ]*[0-9]*[ ]*//g"`; # obtain ...


12

To get the same output you note in your question, all that is needed is this: PS1='${PS2c##*[$((PS2c=0))-9]}- > ' PS2='$((PS2c=PS2c+1)) > ' You need not contort. Those two lines will do it all in any shell that pretends to anything close to POSIX compatibility. - > cat <<HD 1 > line 1 2 > line $((PS2c-1)) 3 > HD line ...


11

You've already found zle-keymap-select which is executed whenever the mode changes. You could use it to set some other visual indicator than the prompt, depending on what your terminal supports it (and your taste in mode indicator display, of course). There is a standard terminfo capability to change the shape of the cursor. However some terminals display ...


11

.vimrc save file with root permissions by typing w!!: cmap w!! w !sudo tee % > /dev/null .bashrc Don't bother with devices or binary files when greping: alias grep='grep --color=auto --binary-files=without-match --devices=skip' Share code on the web (like pastebin, but simpler) by cat 1337.sh | webshare alias webshare='curl -F "sprunge=<-" ...


11

Put \j in your prompt. From the bash manual: \j The number of jobs currently managed by the shell Just remember that prompts do go stale and jobs can finish at any time, so if you have left the terminal idle, you'll want to redisplay the prompt. At the cost of requiring an extra process just to print your prompt, you can make the \j only ...


10

The environment Variables "$PS1" and "$PS2" set the prompt's look. You can check this howto in order to see all the different variables you can put in there. `


10

You should read about .bash_profile and .bashrc. The bracketed information is just a preference in those files which typically live in your home directory and/or /etc/profile (look for the line that starts PS1=). I'm assuming that when you're chrooted, that user does not have access to its home folder to load these files, so your prompt is, essentially, ...


10

The solution is to get the shell to substitute the color variables when defining the prompt, but not the functions. To do this, use the double quotes as you had originally tried, but escape the commands so they aren't evaluated until the prompt is drawn. PS1="\u@\h:\w${YELLOW}\$(virtual_env)${GREEN}\$(git_branch)${RESET}$ " Notice the \ before the $() on ...


9

\033 is the octal code for the Esc (Escape) character, which is a good hint that the echoed strings in your PROMPT_COMMAND are terminal control sequences. Both sequences in your examples look like they set the terminal title to user@host:pwd. The first case, xterm* sets the window name and icon title. For a detailed explanation, look at the list of xterm ...


9

Instead of putting it in PROMPT_COMMAND, put it in PS1: PS1='$($HOME/bin/myprompt) \[\033[34m\]\$\[\033[0m\] ' When it's in PS1, bash counts the number of characters that get printed so that it can correctly redraw when you scroll through your history. That's why bash has the \[ and \] special characters—they tell bash that the enclosed characters ...


9

I have a fancy prompt with colors, and now bash doesn't seem to know how wide my terminal is. Lines wrap around incorrectly. I have another proper way to do this, put this code in your ~/.bashrc or create a new file and source file : PROMPT_COMMAND=$( cat<<-'EOF' retval=$? RED=$(tput setaf 1) GREEN=$(tput setaf 2) STOP=$(tput ...


9

Those are ANSI escape sequences; that link is to a chart of color codes but there are other interesting things on that wikipedia page as well. Not all of them work on (e.g.) a normal linux console. This is incorrect: \033]00m\] # white 0 resets the terminal to its default (which is probably white). The actual code for white foreground is 37. ...


8

You could do something like: PS1='%F{063}%1' # format blue PS1=$PS1'~' # show current directory PS1=$PS1'%f' PS1=$PS1'%(1v.%F{099}%1v %f.)' # show git branch if git repo in purple etc.?


8

grep and ls have already been mentioned, if you want a lot more colors check out Generic Coloriser, its initial purpose was to colorize logfiles, but right out of the box it also colorizes ping, traceroute, gcc, make, netstat, diff, last, ldap, and cvs. It's easily extended if you know regexes. I've added ps and nmap to the list (if you get into grc I'll be ...


8

basically, you need: trap 'echo -ne "\033]0;$BASH_COMMAND\007"' DEBUG at the end of your .bashrc or similar. Took me a while to work this out -- see my answer here for more information :)


8

My background is RHEL-derived distributions (mainly Fedora, today), Arch is foreign to me. Back when /etc/inittab and mgetty where used by RHEL, you could edit /etc/inittab and pass the -p option to mgetty. This would set the prompt. Now with the adoption of systemd /etc/inittab is empty. The tty configuration file is now ...


8

Bash outputs the prompt only in interactive mode. I.e. it is normally output to the terminal (/dev/tty on linux). That is neither /dev/stdout or /dev/stdin :) Now, I'm not sure but I can imagine that bash will allow limited interactive mode when there isn't a fully functional tty. In that case I'd expect the prompt to be written to stdout. I haven't tested ...


8

As I suspected in my comment, you are missing a \] in your PS1. Try this one: PS1='\[\e]2;\u@\h\a\]\u@\h \W \$ ' For comparison, the original is PS1='\[\e]2;\u@\h\a\u@\h \W \$ ' # ^ missing \]


8

This is controlled by the PS1 environment variable. You can see what this is by running, as root, echo $PS1. You can then set the variable in your own bash profile with echo 'PS1=<value of PS1>' >> ~/.bashrc. You can google for lots of possible values of PS1, but the part you are currently missing is \w for working directory.



Only top voted, non community-wiki answers of a minimum length are eligible