2009年5月12日星期二

在Linux下统计你的Erlang程序的行数(SLOC)

统计代码行数(SLOC, soure line of code)的几种选择:
  1. 使用命令wc -l filename可以统计文件filename的行数,但是不能除去空行和注释。
  2. Linux下有个程序叫sloccount,可以统计各种语言的代码行数,但是不支持Erlang -_-!
  3. 我的最终解决方案是使用grep。
使用下面的命令即可统计当前目录下Erlang源代码的行数:
grep -cv '\(^%\)\|\(^\s*$\)' *.erl *.hrl
或者
egrep -cv '(^%)|(^\s*$)' *.erl *.hrl
返回的结果类似于:
module1.erl: 120
module2.erl: 38
module2.hrl: 45

参数c表示让grep统计行数
参数v, 也就是--invert-match,表示选取不符合正则表达式的行

正则表达式(^%)|(^\s*$)由两个正则表达式的并组成。正则表达式^%表示注释(Erlang的注释都以%开始),而正则表达式^\s*$表示开头(^)与结尾($)之间只有任意多个空白符(\s)的行,也就是空行。这样整个正则表达式(^%)|(^\s*$)即表示注释行或者空白行。

GNU的grep和egrep功能相当,区别在于前者使用GNU Basic Regular Expression(BRE),这是目前还在使用的最古老的正则表达式语法,而后者使用的是GNU Extended Regular Expression(ERE)。

两种正则表达式语法的主要区别是前者要求特殊符号前必须有转移字符\, 而后者不要求有\,但是当想表示特殊字符的原意的时候必须加\作为前缀。当正则表达式中有很多特殊符号的时候,ERE的表达就比BRE简洁不少。

关于BRE和ERE可以查看http://www.regular-expressions.info/gnu.html
关于grep的正则表达式的语法可以查看http://opengroup.org/onlinepubs/007908775/xsh/regexp.html

2009年5月7日星期四

Summary on Eleutian's vim tutorial

Eleutian gave a series of vim screencast tutorials , which I think it's good for beginners.

Here's summaries on the key points in his tutorials:

Tutorial 1

1. Vim is a modal editor. There are a lot of modes:
  • Normal mode: Press ESC or CRTL+[
  • Insert mode: Press i in normal mode
  • Command mode: Press : in normal mode
  • Visual mode: Press v in normal mode
  • Append mode: Press a in normal mode
2. In command mode, "e" means opening a file and "w" means saving a file.
  • :e filename : open a file whose name is "filename"
  • :e! : reopen the current file; all changes are lost
  • :w : save the current file
  • :w filename : save the current file as filename; primarily used when a file is to be saved for the first time
3. Stay in normal mode, unless you're actively typing text.
4. To move around in a most efficient way use "h", "j", "k", "l" as left, down, up, right respectively.
5. Move more quickly:
  • ^: Home; move to the head of the current line
  • $: End; move to the end of the current line
  • CRTL + D: Pagedown
  • CRTL + U: Pageup
  • w: Move to the next word
  • b: Move to the previous word
Tutorial 2
1. Move the beginning or the end of a document:
  • gg: Move to the beginning of the document
  • GG: Move to the end of the document
  • XGG: Move the Xth line of the document, like 10G(move to the 10th line)
2. Copy(yank), Cut(delete) and Paste
  • yy: Copy the whole line
  • yw: Copy the next word
  • y$: Copy till the end of line
  • x: cut the current letter
  • dd: Delete the whole line
  • dw: Delete the next word
  • p: Paste the text that just deleted or yanked
3. Change
  • cw: Change the next word
  • cb: Change the previous word
  • c$: Change till the end of line
  • ct: Change till , like ctD(change till the first occurence of letter D)
4. Swap two consecutive letter: xp

Tutorial 3
1. Search
  • :/<Pattern> : Search forward according to the <Pattern>
  • :?<Pattern> : Search backward
  • n: Go to the next occurence <Pattern>
  • p or N: Go to the previous occurence <Pattern>
2. Find
  • f<Symbol> : Find the first occurence of <Symbol>
  • ; : Go to the next occurence of the <Symbol>
  • , : Go to the previous occurence of the <Symbol>
3. Replace
  • %s/<TargetString>/<SubstituteBy>/g : % means the range is the whole text, % means substitution, is what we are looking for, is the substitution for . Don't forget the /g. For example, %s/Good/Bad/g will substitute all ocurrences of "Good" by "Bad" in the whole document.
4. Find occurences of the word under the cursor
  • # : Go to the previous occurence of the word under cursor
  • * : Go to the next occurence of the word under cursor