Monday, October 29, 2012

Customize Shell and Eshell Prompt

My shell prompt was "localhost:~ Arreat$ blahblah" before. I've been bored with it for a long time. I decided to change it to simple style I like.

Shell

 I use bash. The variable which controls the prompt string is PS1(prompt string 1). So in my .bash_profile, I can add this:

export PS1="[\u@\h:\W, \@, \d]> "   #custom prompt options
whoami@hostname\current working directory\time\date


Here is a table of the options:

Description csh* ksh bash tcsh* zsh
Current working directory $CWD $PWD \w %/ %/
Current working directory, with one's home directory by `~' $CWD:t $PWD##*/ \W %~ %~
Full hostname 'uname -n' 'uname -n' N/A %M %M
Hostname up to the first '.' `hostname -s` `hostname -s` \h %m %m
Start (stop) boldfacing mode %B (or %b) N/A N/A %B (or %b) %B (or %b)
Start (stop) standout mode %S (or %s) N/A N/A %S (or %s) %S (or %s)
Start (stop) underline mode %U (or %u) N/A N/A %U (or %u) %U (or %u)
User name `whoami` `logname` \u %n %n
The shell's tty that the user is logged in on %l N/A N/A %| %|
The current history number %h N/A \! %h (or %!) %h (or %!)
Name of the shell N/A N/A \s N/A N/A
Time of day in 12-hour hh:mm AM/PM %t N/A \@ %t (or %@) %t (or %@)
Time of day in 24-hour hh:mm %T N/A \A %T %T
Time of day in 12-hour with seconds hh:mm:ss AM/PM %p N/A \T %p N/A
Time of day in 24-hour with seconds hh:mm:nn %P N/A \t %P %*
The day in 'dd' format %D N/A N/A %D N/A
The month in 'Mon' format %w N/A N/A %w N/A
The month in 'mm' format %W N/A N/A %W N/A
The year in 'yy' format %y N/A N/A %y N/A
The year in 'yyyy' format %Y N/A N/A %Y N/A
The date in "Weekday Month Date" format N/A N/A \d N/A N/A
The date in day-dd format N/A N/A N/A N/A %w
The date in Mon/dd/yy format N/A N/A N/A N/A %W
The date in yy-mm-dd format N/A N/A N/A N/A %D
The weekday in 'Day' format %d N/A N/A %d N/A
Description csh* ksh bash tcsh* zsh


I mainly find stuffs about shell from the link below:
How to change your shell prompt You can find a lot of things about shell from this site.

Eshell 

As to eshll, I got information from EmacsWiki: Eshell Prompt

The Eshell prompt is generated by the function stored in ‘eshell-prompt-function’. When moving through the buffer, eshell also needs to know which lines start with a prompt. Therefore, whatever ‘eshell-prompt-function’ prints must be matched by ‘eshell-prompt-regexp’.

In my emacs init file, I add this:

(setq eshell-prompt-function
      (lambda ()
        (concat " $ "))) 

Though I don't know much about elisp, but I think it's quite clear what it means. And I also learned two command of emacs:

  1.  C-h m: describe mode 
  2.  C-h v: describe variable 

 Now my shell prompt is simple and clear, just my style :)

Sunday, October 28, 2012

Incremental Development

This is the first time I know the concept of Incremental Development. I know it from the exercise of Dict and File section of Google Python Class. I think this concept is great for people that not experienced to programming like me.

Here is what author of the python class said about the concept:
Building a Python program, don't write the whole thing in one step. Instead identify just a first milestone, e.g. "well the first step is to extract the list of words." Write the code to get to that milestone, and just print your data structures at that point, and then you can do a sys.exit(0) so the program does not run ahead into its not-done parts. Once the milestone code is working, you can work on code for the next milestone. Being able to look at the printout of your variables at one state can help you think about how you need to transform those variables to get to the next state. Python is very quick with this pattern, allowing you to make a little change and run the program to see how it works. Take advantage of that quick turnaround to build your program in little steps.
This can make programming much easier.  Sorry for my bad English. I'll work much harder to improving my English writing skills. : )

PS: Is there anyone want to learn Chinese or Korean? If there is, I can help you with Chinese or Korean, then you can help me with my poor English. Thanks!


How to remove item when looping through a list in Python

The best way is to make a copy of the list first. Like this:
words = ['xaa', 'sss', 'aaa', 'bbb']
for word in words[:]:
    if word[0] == 'x':
      words.remove(word)
At first, I use code like this to achieve my goal:
words = ['xaa', 'aaa', 'bbb', 'ccc']
for word in words:
  if word[0] == 'x':
    words.remove(word)
the behavior of this code is not ensured. Thera are some other ways to modify a list while looping through the list. But I stick to the copy method.