Wednesday, January 28, 2009

使用Vim的自动补全功能

前段时间SHE同学说想在Vim中有一个自动补全功能, 因为我以前都是不用这个的, 所以推荐他了一个在IRC上看到的命令: CTRL-N, 可这个命令使用之后列出的结果太多了, 花在找匹配上的时间还不如直接打呢.

昨天下午花了点时间研究了下, 开始还以为需要使用什么插件才能办到呢, 最后才发现其实Vim已经拥有了这些功能, 只是需要你去发掘.

Vim自带了很多种自动补全的方式, 最终选定了omni completion (全能补全), 如果想要查看其它的补全方式的话, 可以在Vim中输入 :help ins-completion

由于omni是借助tags文件来达到补全的, 所以需要安装有Ctags才行, Ubuntu可以用下面的命令安装:
$ sudo apt-get install exuberant-ctags

2009.12.26更新:
这里有一个针对C语言补全的小技巧, 在安装好Ctags之后, 可以先对系统中已有的头文件进行一次tag, 然后让Vim识别生成的tag文件 (下面的Vim配置文件中已经有相应的配置), 这样就可以实现对于C标准库函数以及system call的补全, Ubuntu下执行如下命令即可:
$ ctags -R -f ~/.vim/systags /usr/include

然后就是修改.vimrc文件, 下面是我已经写好了的, 添加进去就行了. 需要注意的是, 默认情况下omni completion的快捷键是CTRL-X CTRL-O, 但每次都这样有点麻烦, 所以我把这个快捷键映射到了<Tab>键上. 不过如果我只是想用<Tab>来增加缩进的话, 直接映射就会导致缩进功能消失了. 于是抄了一段Vim帮助中的脚本, 再根据实际情况修改了下, 这样就可以很智能地根据具体情况映射<Tab>键了. 如果这一行只有空白, 那么按<Tab>键就是增加缩进, 否则就是使用omni completion.
" ----------------------------------------------------------------------
" OMNI COMPLETION
" ----------------------------------------------------------------------

" Enable file type detection.
" Use the default filetype settings, so that mail gets 'tw' set to 72,
" 'cindent' is on in C files, etc.
" Also load indent files, to automatically do language-dependent indenting.
filetype plugin indent on

" Don't show the preview window.
set completeopt=menu

set tags+=~/.vim/systags,/tmp/tags

" Map <Tab> to either actually insert a <Tab> if
" the current line is currently only whitespace, or start a omni
" completion operation.
function! CleverTab()
    if strpart(getline('.'), 0, col('.')-1) =~ '^\s*$'
        return "\<Tab>"
    else
        let current_path = bufname("%")  " get name of current buffer
        let current_path = system("dirname " . current_path)
        execute "cd " . current_path
        execute "silent !ctags -R -f /tmp/tags ."
        return "\<C-X>\<C-O>"
    endif
endfunction
inoremap <Tab> <C-R>=CleverTab()<CR>

这是效果图:

omni completion

这时你可以使用CTRL-N或者CTRL-P来上下选择, 使用CTRL-Y来确定, CTRL-E来取消.

不过虽说这被叫做是全能补全, 但实际使用中还是有不是很顺利的地方的, 比如对于某些局部变量的补全. 这时可以试试CTRL-X CTRL-N, 将这两种补全结合着用, 应该就能满足日常需求了.

Monday, January 26, 2009

献给太阳

四周被震耳的鞭炮围绕, 心里默默诉说的话语也不用担心被人偷听到了, 无论怎样, 我收到了最美好的新年祝福, 这是属于我们的小秘密, 对吗?

原谅我飞, 曾经眷念太阳.

Sunday, January 25, 2009

Firefox的快速访问插件: SiteLauncher

SiteLauncher
这是一个看起来和GNOME Do差不多的东西, 用快捷键呼出SiteLauncher以后, 就可以按照自定义的key来访问网站了. 把你经常光顾的网站加进去, 借助SiteLauncher快速地访问, 最好是先打开想要添加的网站, 然后通过菜单或者快捷键 (Ctrl+Shift+S) 来添加, 这样添加以后就会有网站的图标 (fashion) 了.

你可以在这里下载.

Saturday, January 17, 2009

发现gdb的一个新功能

都怪以前没好好看看gdb的man page, 今天帮同学整gdb的时候, 偶然发现gdb有一个选项-tui, 就是这个神奇的选项, 让我来到了一个崭新的gdb的世界.

gdb
看下这张图片, 是不是和普通的gdb调试有点不一样, 那个占了大半个屏幕的东西就是改变gdb体验的一个重要工具. 有了它, 可以把代码都显示在里面, 比以前一个劲地list方便多了. 有了它, 你可以显式地看到打断点的位置. 有了它, 你可以看到当前执行到哪一行代码了. 这已经和在Emacs里面用gdb调试差不多了, 除了打断点没有那么方便, 对代码中的中文支持很差和没有代码高亮以外, 其它我都觉得用着挺爽的, 以后在没有Emacs的机器上调试也可以很舒适了, OY!

Sunday, January 11, 2009

马...克...思

Marxism
外带打印版纸张无数, 小抄无数, ORZ...

P.S. 同样是终端, 差别咋就那么大呢...

经典书收集 (持续更新)

  1. Computer Foundamental

    • The Art of Computer Programming  作者: Donald E. Knuth
      这个不用多说什么了.

    • Introduction to Algorithms  作者: Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein
      传说中的MIT教材. 传说中的CLRS.

    • Computer Systems: A Programmer's Perspective  作者: Randal E. Bryant and David R. O'Hallaron
      想从一个初级程序员晋升为高级程序员吗? 请看这本书.

    • Programming Pearls  作者: Jon Bentley
      编程猪与鸡, :)

    • Hacker's Delight  作者: Henry S. Warren
      我看到的第一本Amazon五星书籍, 涉及很多位运算. Amazon上有人戏称这本书是"The Art of Computer Programming, Volume 0: Bit Manipulation".

    • Introduction to the Theory of Computation  作者: Michael Sipser

  2. Mathematics

    • What Is Mathematics? An Elementary Approach to Ideas and Methods  作者: the late Richard Courant, Herbert Robbins, and Ian Stewart

    • Discrete Mathematics and Its Applications  作者: Kenneth Rosen

    • Probability and Statistics  作者: Morris H. DeGroot and Mark J. Schervish

    • Concrete Mathematics: A Foundation for Computer Science  作者: Ronald L. Graham, Donald E. Knuth, and Oren Patashnik

  3. Operating System
  4. 在操作系统原理领域有两本书被公认为经典, 一本是Tanenbaum的《Modern Operating Systems》, 另一本是Abraham Silberschatz, Peter B. Galvin以及Greg Gagne的《Operating System Concepts》. 这里之所以提出来, 是因为我最初学习操作系统时在选书上走了一点"弯路". 后一本我没看过, 不便评论, 但Tanenbaum的那本我想应该提醒一下各位. 首先, 如果你是一个操作系统的初学者, 那我极不推荐你看第一本. 纵观操作系统的书籍, 那些最典型, 同时也是最基础的知识, 比如process, memory, I/O, file system, 是肯定会包含的. Tanenbaum的那本也不例外, 但在某些概念上的阐述就不是很清晰了, 会让初学者感到没有解释清楚, 也许是大牛觉得那些都是很简单的东西, 不用说的太详细. 此外书中的一些术语也不同于国内以及其它国外教材, 比如process control block, 在这本书中却叫做process table. 恐怕《Modern Operating Systems》书中的最大亮点是占了全书近1/3篇幅的关于Linux, Windows Vista以及Symbian (这里是以第三版为例, 其它版本可能不同) 的分析与介绍, 但在我看来这有点鸡肋, 毕竟关于上述三种操作系统的分析都有很不错的对应的书籍, 区区100来页怎能很好地理解一个完整的操作系统呢? 不过, 最后一章中作者讲述的作为一个操作系统设计者应该注意的地方还是值得一读的. 所以没有必要拘泥于权威, 我推荐初学者看下面这本书, 至少在我看来也是很不错的, Amazon上虽然褒贬不一, 但只有真正看了才能知晓其中的好坏.

    • Operating Systems: Internals and Design Principles  作者: William Stallings

  5. UNIX/Linux

    • Advanced Programming in the UNIX Environment  作者: W. Richard Stevens and Stephen A. Rago
      UNIX的圣经.

    • The Art of Unix Programming  作者: Eric Steven Raymond
      讲述UNIX哲学.

    • UNIX Network Programming, Volume 1: The Sockets Networking API  作者: W. Richard Stevens, Bill Fenner, and Andrew M. Rudoff

    • Understanding the Linux Kernel  作者: Daniel P. Bovet and Marco Cesati Ph.D.

  6. Network

    • 计算机网络  编著者: 谢希仁
      这是我推荐的为数不多的国内出版的书籍, 推荐的理由很简单, 适合初学者, 文字清晰易懂, 能让你在最短的时间内了解计算机网络的全貌, 为以后更深入的学习做好铺垫.

    • Computer Networks  作者: Andrew S. Tanenbaum
      这是一本巨细无遗的书籍, 可以让你从里到外, 非常透彻地了解计算机网络, 很适合于那些已经具有计算机网络基础的人用来深入学习.

    • TCP/IP Illustrated, Volume 1: The Protocols  作者: W. Richard Stevens

    • TCP/IP Illustrated, Volume 2: The Implementation  作者: Gary R. Wright and W. Richard Stevens

  7. Database

    • Fundamentals of Database Systems  作者: Ramez Elmasri and Shamkant B. Navathe

  8. Compiler

    • Compilers: Principles, Techniques, and Tools  作者: Alfred V. Aho, Monica S. Lam, Ravi Sethi, and Jeffrey D. Ullman
      传说中的龙书.

  9. Software Engineering

    • The Mythical Man-Month: Essays on Software Engineering  作者: Frederick P. Brooks

    • Code Complete: A Practical Handbook of Software Construction  作者: Steve McConnell

    • The Pragmatic Programmer: From Journeyman to Master  作者: Andrew Hunt

    • Design Patterns: Elements of Reusable Object-Oriented Software  作者: Erich Gamma, Richard Helm, Ralph Johnson, and John M. Vlissides

  10. Assembly

    • 80X86 IBM PC and Compatible Computers: Assembly Language, Design, and Interfacing  作者: Muhammad Ali Mazidi and Janice Gillispie-Mazidi

  11. C

    • The C Programming Language  作者: Brian W. Kernighan and Dennis M. Ritchie
      传说中的K&R.

    • C Traps and Pitfalls  作者: Andrew Koenig

  12. C++

    • The C++ Programming Language  作者: Bjarne Stroustrup
      C++之父大作. 这本书不是很适合初学者, 如果你不了解C++, 那么你在看这本书的前几节时会感到异常难懂, 甚至会因为缺乏耐心而放弃. 不过这绝对是C++的圣经.

    • C++ Primer  作者: Stanley B. Lippman, Josée Lajoie, and Barbara E. Moo
      适合初学者, 浅显, 讲解透彻, 入门好教材.

    • C++ Templates: The Complete Guide  作者: David Vandevoorde

    • C++ Coding Standards: 101 Rules, Guidelines, and Best Practices  作者: Herb Sutter

    • Effective C++: 55 Specific Ways to Improve Your Programs and Designs  作者: Scott Meyers

    • Exceptional C++: 47 Engineering Puzzles, Programming Problems, and Solutions  作者: Herb Sutter

    • Effective STL: 50 Specific Ways to Improve Your Use of the Standard Template Library  作者: Scott Meyers

    • Programming: Principles and Practice Using C++  作者: Bjarne Stroustrup

  13. Java

    • Core Java  作者: Cay S. Horstmann and Gary Cornell

    • Thinking in Java  作者: Bruce Eckel

  14. JavaScript

    • JavaScript: The Definitive Guide  作者: David Flanagan
      一本全面介绍JavaScript的书, 你可以从中学到很多基础, 同时这本书也可以作为一本reference.

  15. Shell Script


  16. Version Control

    • Version Control with Subversion  作者: C Pilato, Ben Collins-Sussman, and Brian Fitzpatrick
      作者在网上放了免费的版本, 参见我的另一篇日志: 学习了SVN

    • Pro Git  作者: Scott Chacon

df的增强版工具——pydf

pydf
在LinuxTOY上看到的文章: 使用 pydf 查看磁盘空间占用情况, 比df多了彩色显示和可视化的百分比, 另外排版也稍微整齐了一点, 以后就用它了, Ubuntu源里有.
$ sudo apt-get install pydf

Friday, January 9, 2009

欢迎来到我的豆瓣

今天注册了豆瓣, 然后花了将近一个下午添加了很多东西进去, 欢迎大家参观.

小高子的豆瓣

Thursday, January 8, 2009

彩色的less 彩色的世界

曾经写过一篇日志《彩色man的less替代方案》, 这里面就是借助了less来让man page变成彩色的. 日志的最后留下了如何用less查看普通文件也可以高亮的疑惑, 今天看LinuxTOY的一篇文章时才有了解答.

首先安装source-highlight:
$ sudo apt-get install source-highlight

然后配置.lessfilter文件:
$ cat ~/.lessfilter
#!/bin/bash

case $1 in
  *ChangeLog | *changelog ) 
    source-highlight --failsafe -f esc --lang-def=changelog.lang --style-file=esc.style -t 2 -i $1
    ;;

  *Makefile | *makefile ) 
    source-highlight --failsafe -f esc --lang-def=makefile.lang --style-file=esc.style -t 2 -i $1
    ;;

  *)
    source-highlight --infer-lang -f esc --style-file=esc.style -t 2 -i $1
    ;;
esac

并在.bashrc文件中添加一行:
export LESS='-R'

最后执行:
$ . ~/.bashrc

Monday, January 5, 2009

课程设计终于搞定 :)

这几天为了调试课程设计的程序已经达到可以不去吃饭了, 好在终于弄完, 今天老师检查的时候还因为没来得及生成Window$版本的程序, 而不得不直接在Linux上演示. 晚上把Window$的也弄出来了, 所有的代码也都svn到了Google Code上了. 长出一口气, 呼~

期间在两个系统之间有一个不兼容的地方, 就是Window$下<conio.h>文件中的getch()这个函数. 由于Linux下没有那个头文件, 自然也就没有这个函数了. 但这个函数又是比较重要的一个, 不好轻易改动, 于是在CSDN的某博上找到了很好的解决办法, 用着挺不错的~

他自己写了个getch()函数, 功能上和Window$下的一样:
#include <stdio.h>
#include <termios.h>
#include <unistd.h>
#include <assert.h>
#include <string.h>

int getch()
{
  int c = 0;
  struct termios org_opts, new_opts;
  int res = 0;

  /*
   * Store old settings.
   */
  res = tcgetattr(STDIN_FILENO, &org_opts);
  assert(res == 0);

  /*
   * Set new terminal parms.
   */
  memcpy(&new_opts, &org_opts, sizeof(new_opts));
  new_opts.c_lflag &= ~(ICANON | ECHO | ECHOE | ECHOK | ECHONL | ECHOPRT | ECHOKE | ICRNL);
  tcsetattr(STDIN_FILENO, TCSANOW, &new_opts);
  c = getchar();

  /*
   * Restore old settings.
   */
  res = tcsetattr(STDIN_FILENO, TCSANOW, &org_opts);
  assert(res == 0);

  return c;
}

Window$版的本来想用VC的编译器来弄的, 结果果然VC的编译器比较高级, 在Linux下gcc没有一个错误和警告, 到了VC下就出现了几十个错误和警告(- -). 最后没办法, 就直接用MinGW编译了, 不过运行起来没有Linux下那么稳定.