10月31日
很不幸,Ctags的auto-cursor-preview脚本并不能和Taglist很好的在一起工作。它们都借助于updatetime来更新信息,有时它们会出现窗口的错乱。所以我常会在两种模式下工作。
我会在$HOME目录下建立一个文件夹来管理我的工作空间(估且这么叫吧),不妨叫viworkspace。我通常会这样组织我的工程,假定我的源码在.../src/目录下,我会在~/viworkspace/下建立一个符号链接src指向实际的源码目录.../src/。然后我在~/viworkspace/目录下运行ctags -R *,会在当前目录生成一个tags文件。然后我会在以下两种模式下工作:
第一种就是auto-cursor-preview模式
我在~/viworkspace/创建一个叫workspace1的文件,其内容为如下的vi脚本:
"********************************************************
set updatetime=50 "预览窗更新间隔
set tags=~/viworkspace/tags "设置tags文件
cd ~/viworkspace/src "进入到源码目录
edit . "用netrw打开当前目录
"设置自动预览
au! CursorHold *.[ch] nested call PreviewWord()
func PreviewWord()
if &previewwindow " don't do this in the preview window
return
endif
let w = expand("<cword>") " get the word under cursor
if w =~ '\a' " if the word contains a letter
" Delete any existing highlight before showing another tag
silent! wincmd P " jump to preview window
if &previewwindow " if we really get there...
match none " delete existing highlight
wincmd p " back to old window
endif
" Try displaying a matching tag for the word under the cursor
try
exe "ptag " . w
catch
return
endtry
silent! wincmd P " jump to preview window
if &previewwindow " if we really get there...
if has("folding")
silent! .foldopen " don't want a closed fold
endif
call search("$", "b") " to end of previous line
let w = substitute(w, '\\', '\\\\', "")
call search('\<\V' . w . '\>') " position cursor on match
" Add a match highlight to the word at this position
hi previewWord term=bold ctermbg=green guibg=green
exe 'match previewWord "\%' . line(".") . 'l\%' . col(".") . 'c\k*"'
wincmd p " back to old window
endif
endif
endfun
"********************************************************
第二种就是Taglist模式
"********************************************************
set updatetime=50 "设置标签窗口更新间隔
let Tlist_Compact_Format=1 "设置标签列表为紧凑模式,去掉多余空行
"let Tlist_Display_Prototype=1 "设置标签列表为原型模式
let Tlist_File_Fold_Auto_Close=1 "设置不活动文件,自动折叠标签列表,仅显示当前文件列表
TlistToggle "开启Taglist列表
set tags=~/viworkspace/tags "设置tags文件
cd ~/viworkspace/src "进入到源码目录
edit . "用netrw打开当前目录
"********************************************************
最后,在.vimrc中添加键盘映射。
"********************************************************
map <F2> :source ~/viworkspace/workspace1<CR>
imap <F2> <ESC>:source ~/viworkspace/workspace1<CR>
map <F3> :source ~/viworkspace/workspace2<CR>
imap <F3> <ESC>:source ~/viworkspace/workspace2<CR>
"********************************************************
这样,只要以后启动了vim,按下<F2>就会自动进入源码目录,用netrw打开文件列表,并进入auto-cursor-preview模式。同理,按下<F3>就会自动进入源码目录,用netrw打开文件列表,并进入Taglist模式。在这两种模式下,只要在文件列表中选择要打开的文件,并按回车,就可以开始代码之旅了。
10月25日
想不想一侧显示变量或函数列表,另一侧显示源代码窗口,Taglist可以帮你实现这点。
1.下载taglist.zip并解压到 $HOME/.vim,会产生以下两个目录:
plugin/taglist.vim - main taglist plugin file
doc/taglist.txt - documentation (help) file
2.确保.vimrc文件中开启了: filetype on
2.切换到 $HOME/.vim/doc,启动Vim并执行":helptags ."命令来产生taglist的帮助文件,没有这一步,你在VIM中就无法查看taglist的帮助。
3.使用":Tlist[Toggle]" 命令开启与关闭taglist窗口,使用TlistOpen和TlistClose来打开和关闭taglist窗口。
4.使用回车键或鼠标双击来浏览相应tag。用o在新窗口中打开标签,用p在预览窗口打开标签使鼠标仍留在taglist窗口。
5.为方便使用,在.vimrc中作如下设置:
let Tlist_Compact_Format=1 “使用紧凑模式显示标签,不会添加空行
let Tlist_Display_Prototype=1 “显示标签原型,而不仅是名字
let Tlist_File_Fold_Auto_Close=1 ”自动折叠不活动的文件或buffer的标签
下面这张效果图来自Taglist官方网站的Screenshot。

10月24日
在使用Ctags生成的标签信息阅读代码的时候,可以在你的.vimrc中加入如下脚本来实现将当前光标下的标的签定义自动显示在预览窗口中。
set updatetime=50
"setup for ctags auto-cursor-preview
au! CursorHold *.[ch] nested call PreviewWord()
func PreviewWord()
if &previewwindow " don't do this in the preview window
return
endif
let w = expand("<cword>") " get the word under cursor
if w =~ '\a' " if the word contains a letter
" Delete any existing highlight before showing another tag
silent! wincmd P " jump to preview window
if &previewwindow " if we really get there...
match none " delete existing highlight
wincmd p " back to old window
endif
" Try displaying a matching tag for the word under the cursor
try
exe "ptag " . w
catch
return
endtry
silent! wincmd P " jump to preview window
if &previewwindow " if we really get there...
if has("folding")
silent! .foldopen " don't want a closed fold
endif
call search("$", "b") " to end of previous line
let w = substitute(w, '\\', '\\\\', "")
call search('\<\V' . w . '\>') " position cursor on match
" Add a match highlight to the word at this position
hi previewWord term=bold ctermbg=green guibg=green
exe 'match previewWord "\%' . line(".") . 'l\%' . col(".") . 'c\k*"'
wincmd p " back to old window
endif
endif
endfun
10月23日
很多Linux/Unix程序员都希望能拥有Source Insight那样的杀手级的工具,能在代码之间无拘无束、来去自如。但是Source Insight只能用于Windows平台,而且价格也不菲。当然,我们会去找一些替代案,虽然功能无法超越Source Insight,但是够用就好,不是吗?Ctags就是一个很好的借助标签信息来实现代码跳转和补全的一个很好的选择。
1.在源码根目录下执行ctags --sort=yes -R
会在当前目录下生成一个名为tags的tagfile。
如果不加--sort=yes,生成的文件头中也显示是sorted的(不知道是不是个bug,可能只在某些版本有吧),但是有时vim会提示tagsfile unsorted,查找会变慢。因为对于sorted tagfile,vim会使用二分查找法;对于unsorted tagfile,vim会使用线性查找法。而且有时错误会影响你的使用,通过set notagbsearch可以关闭错误提示,但vim将总是用线性查找法。
2.vi -t $tag启动vim并自动定位到tag的定义处。如果不是在tagfile所在目录下打开vim,需在vim启动后设置set tags=path/tagfile。
这样你就可以用CTRL+]定位tag的定义,用CTRL+T来返回,用tag $tag定位到特定tag。
在编写代码的时调用CTRL+P、CTRL+N也会自动搜索tag信息来进行补齐。
3.如果有鼠标的话,设置
set mouse=a
set scroll=5
会较好用些。
可用CRTL+left click来代替CTRL+]
可用g+right click来代替CTRL+T
4.如时标签有多个匹配,会显示tag i of N,可用:tfirst跳转到第一个匹配,:tlast跳转到最后一个匹配,:tprevious跳转到前一个匹配,:tnext跳转到下一个匹配,:tselect选择要跳转的匹配。
5.其它
用tag $tag+n次TAB进行tag的自动补齐来(别忘了在.vimrc中设置set wildmenu)定位到特定tag。set ignorecase会可能会影响到tag定位的结果。tags会显示tag堆栈的历史记录。
6.常用命令助记
ta[g] $tag+TAB
tf[irst]
tr[ewind]与tfirst等价
tl[ast]
number+tp[revious]
number+tn[ext]
number+tN[ext]与tprevious等价
ts[elect] $tag
tj[ump] $tag与tselect相似,但当只有一个匹配时,直接跳转到定义。
sts[elect] $tag与tselect相似,但是会在新建水平分割窗口是显示所选的tag的定义。
stj[ump] $tag是tj与sts相结合的产物
g CTRL+] 与tjump等价
下面这些命令与上面类似,但是都是在预览(preview)窗口中显示tag定义。
pt[ag] $tag+TAB
ptf[irst]
ptr[ewind]与ptfirst等价
ptl[ast]
number+ptp[revious]
number+ptn[ext]
number+ptN[ext]
10月20日
netrw插件是vim的标准插件,在.vimrc中只要设置了
set nocp "不与vi兼容
filetype plugin on "允许插件
就可以使用netrw插件了。
netrw插件的项是网络读写,但我们经常用到的却是它的目录浏览功能(取代了先前的explore.vim)。
打开目录命令
Ex[plore] 打开当前目录或指定目录
Se[xplore] 分割并打开当前目录或指定目录He[xplore]
Ve[xplore] 垂直分割并打开当前目录或指定目录
Te[xplore] 新建标签并打开当前目录或指定目录
浏览文件
移动光标到感兴趣的文件或目录,按<cr>即可在当前窗口浏览该文件或目录;按p键在预览窗口中浏览该文件;按P键在上次使用的窗口中浏览该文件。
命令列表
<cr> netrw 进入目录或读入文件 |netrw-cr|
<del> netrw 试图删除文件/目录 |netrw-del|
- netrw 往上走一层目录 |netrw--|
a 在以下三种方式间切换: 正常显示,|netrw-a|
隐藏 (不显示匹配 g:netrw_list_hide 的文件) 和
显示 (只显示匹配 g:netrw_list_hide 的文件)
c 使浏览中的目录成为当前目录 |netrw-c|
d 建立目录 |netrw-d|
D netrw 试图删除文件/目录 |netrw-D|
i 在瘦、长、宽和树形的各种列表方式间切换 |netrw-i|
<c-l> netrw 刷新目录列表 |netrw-ctrl-l|
o 打开新浏览窗口,进入光标所在的目录。使用水平分割。|netrw-o|
p 预览文件 |netrw-p|
P 在前次使用的窗口里浏览 |netrw-P|
r 反转排序顺序 |netrw-r|
R 给指定的文件/目录换名 |netrw-R|
s 选择排序方式: 按名字、时间或文件大小排序 |netrw-s|
S 指定按名字排序的后缀优先级 |netrw-S|
t 在新标签页里打开光标所在的文件/目录 |netrw-t|
v 打开新浏览窗口,进入光标所在的目录。使用垂直分割。|netrw-v|
10月16日
经常要在VMware虚拟机下与windows主机共享文件,一个方案就是在Guest OS中装上VMware-tools,然后采用文件夹共享。但是并不是所有Guest OS都能找到对应的VMware-tools。而Samba则是一个很好的方案:
我的Host OS是Vista,Guest OS是Ubuntu 8.04。
1. 先在Vista中认置一个共享文件夹叫vmshare。
2. 再在Ubuntu中安装Sambfs,执行如下:
apt-get install smbfs
3.手动挂载:
sudo mount -t smbfs -o username=xxx,password=xxx,rw //192.168.1.10/vmshare /mnt/vmshare
其中192.168.1.10是Vista的IP,username和password是有效的Vista帐号。
4.开机自动挂载:
vi /etc/fsab
加入如下一行:
//192.168.1.10/vmshare /mnt/vmshare smbfs auto,rw,username=xxx,password=xxx 0 0
mount -a
OK,可以自由共享了。
10月9日
今天在 Red Hat Enterprise Linux 5上安装eclipse的C/C++版本,但是提示JVM版本为1.4,必须1.5以上才能运行。于是下载了一个jdk-6u7-linux-i586-rpm.bin文件进行安装,但是安装完以后,运行java -version,仍然显示为版本1.4。
要经过什么样的配置呢?从网上搜索了一些方法,凭我的直觉觉得这些方法都不是我想要的。经过摸索,我找到的方法如下。
/usr/bin/目录中有4个链接文件:java、javac、javadoc、javaws。它们分别指向系统默认的java安装,即先前的1.4版本。
从Sun网站下载的jre或jdk默认安装在/usr/java目录下。这个目录中还有两个链接文件:链接default指向latest,链接latest则指向java的最新版本目录。
于是,我在/usr/bin/ 目录下执行以下4条命令,重建java链接:
ln -sf /usr/java/default/bin/java
ln -sf /usr/java/default/bin/javac
ln -sf /usr/java/default/bin/javadoc
ln -sf /usr/java/default/bin/javaws
这时运行java -version,正确显示java version "1.6.0_07"。
因为我不是java程序员,所以对java的配置不是很了解,但是上面的方法的确让我的eclipse工作了。如果您有更好的方法,请赐教,不胜感激!
10月8日
TFTP、FTP、SFTP、SAMBA和NFS,这些都是常用的文件服务。
10月5日
XP有Standby(待机)和Hibernate(休眠)两种模式,而VISTA有Sleep(睡眠)、Hibernate(休眠)和Hybrid Sleep(混合睡眠)三种模式。经常使用笔记本的朋友,如果能把这几个概念都弄清楚的话,在日常使用中会有很大帮助。下面我们看看微软专家们怎么说。
XP的Standby和Hibernate
在Sustainable Computing Sleep or Hibernate?这篇文章中,对XP的Standby和Hibernate的解释是这样的:
The power management features in Windows XP include Hibernate and Standby.
Hibernate saves an image of your desktop with all open files and documents, and then it powers down your computer. When you turn on power, your files and documents are open on your desktop exactly as you left them.
Standby reduces the power consumption of your computer by cutting power to hardware components you are not using. Standby can cut power to peripheral devices, your monitor, even your hard drive, but maintains power to your computer's memory so you don't lose your work.
可见,如果想快速恢复工作,就要用Standby模式,前提是计算机不能断电;如果长时间不使用计算机,推荐使用Hibernate模式,这样更省电,但这样做恢复工作空间的时间稍长一点。台式机电源比较稳定情况下,不会有什么问题。而对于笔记本,如果短时不用可以Standby,但期间不要把电池拿出,另外还要保证电量充分,否则就要用Hibernate模式,这样比较保险一些。
VISTA的Sleep、Hibernate和Hybrid Sleep
在这篇文章中,对VISTA的Sleep、Hibernate和Hybrid Sleep的解释是这样的:
Sleep, which is the default power-saving state in Windows Vista, is a low-power state that preserves whatever you were doing in memory. A small amount of power is used to maintain that memory (typically less than 1 watt on a modern laptop).
It causes your computer to go to sleep automatically if you haven't used the system for a set amount of time. Of course, if you know you are stepping away from your computer, you can also choose to put it to sleep manually.
Hibernate, on the other hand, writes your settings and the content of memory to the hard disk and then completely powers down the system.
On a laptop, this means that the computer is not using any power (unless the laptop is charging, of course). On a desktop that remains plugged in, power usage can vary. In fact, hibernate may not use significantly less power than sleep.
Hybrid sleep is a power-saving feature designed primarily for desktop computers. Because laptops have batteries, there is limited risk that power will be completely lost unexpectedly. If a laptop is in a sleep state and the battery starts to run low, the system will wake up and immediately go into hibernate, which requires no power to preserve memory. But a desktop computer does not typically have a battery backup, so if the computer loses power while in sleep, all the contents stored in memory will be lost (including any unsaved work).
Hybrid sleep addresses this issue by combining the fast resume capabilities of sleep with the reliability of hibernate: specifically, it saves the contents of memory to hard disk. This means that you can quickly resume from sleep in most cases, but if a power failure occurs, Windows Vista can restore your work and settings from the hard disk.
On desktops, hybrid sleep is usually turned on by default. When this is enabled, clicking Sleep will put the computer into hybrid sleep. (When hybrid sleep is turned off, or if your computer doesn't support hybrid sleep, clicking Sleep puts the computer into the standard sleep mode.) To wake up most computers from hybrid sleep, simply press the power button and the computer will resume exactly as you left it.
可见:
对于笔记本,默认情况下,VISTA的Sleep对应于XP的Standby(Standby过程中,如果电池快耗尽,则转入Hibernate模式),VISTA的Hibernate对应于XP的Hibernate。
对于台式机,默认情况下,VISTA的Sleep是Hybrid sleep,也就是说同时在内存和硬盘中保留数据,如果没断过电,则从内存恢复(快),如果断了电,则从硬盘恢复(稍慢)。
当然,如果你使用笔记本,你想来个双保险的,你可以从高级电源设置里启用Hybrid sleep,这样你点Sleep按钮的时候就会使用Hybrid sleep模式。如果你使用台式机,但却不喜欢Hybrid sleep,也可以从高级电源设置中把它禁用掉,从而使用传统的Sleep。