Tag Archives: vim

the Power of Regex

Today I solved a problem during my work in help of vim and regex. Take a note here, in case I forget it someday.

There are two files(excel A and word B) for me to figure out whether the names in word B are also existed in excel A. Excel A and word B look like this:

Excel A:

From Drop Box

Word B:

From Drop Box

First of all, if I want to compare the two files, I should copy the names in word B to excel A and use the function vlookup, this suppose to be simple, so I don’t want to talk about this part. The main problem here is how to transform the names in word B to some other format, each name in a row actually. So, the objective is set, let’s begin to solve it.

  1. Copy the entire word B to gvim C, because vim is truly a word editor and it is where the regex can take full advantage of.
  2. There are two space between each name, we should turn them to one. It’s time for regex to roll out.

    :%s/ss/ /gc

    “s” means match a white space character, exactly a space and a tab.

  3. And some names contain two character, but they also have a space between them, we should eliminate these space.

    :%s/s(S)s/ 1/gc

    “S” means match anything that isn’t a space and a tab and “(…)” means making matched subtexts available in the replacement part of a substitute command, such as “1”.

  4. Replace every space to a carriage return, this one seems a piece of cake.

    :%s/s/r/gc

    “r” means a carriage return.

  5. Copy the entire contexts to excel A, and transfer the job to function vlookup. The contexts should look like this:
  6. From Drop Box

If the excel A and word B are some big files, we’ll save great amount of time using regex. In my case, they really are. And I think I did my job nice and easy.

How to install Firefox 4 (Stable) In Ubuntu [10.04 and 10.10]

Source: How To Install Firefox 4 (Stable) In Ubuntu [10.04 and 10.10]

Now Firefox 4 (stable) has finally released, I cannot wait to have a try. Though I use Chrome quite a lot nowadays, Firefox was once my favorite. Somehow it’s a little bit slow, but it can highly customize, that win my heart, besides, I’m quite indulge in”Pentadactyl“, an add-on to emulate vim in the browser. Of course, Chrome has the similar extension, “Vimium“, but I suppose  “Pentadactyl” is better.

If your OS is other than Ubuntu, you can probably find Firefox installer in this page, download it, click and install, simple and easy. And if you happened to use Ubuntu like me, then follow these steps:

  1. If you are using the development version of Firefox, remove it with the command,
    sudo apt-get remove firefox-4.0
    If you are not using the development version, skip this step.
  2. Now add the Firefox stable PPA.
    sudo add-apt-repository ppa:mozillateam/firefox-stable
  3. Now update the software list
    sudo apt-get update
  4. Finally install Firefox 4 with the command.
    sudo apt-get install firefox
    After installation, you will find Firefox under Applications > Internet > Firefox Web Browser.

Some of the add-ons are not compatible with Firefox 4, such as “Autoproxy”, I don’t know how to fix it now, but I’m sure with the help Almighty Google, you can find your way.

O.k., let’s surf.

一个简单的vim实例

实际工作中碰到的问题,记录一下,免得忘了。代码如下:

...
<table>
<tbody>
<tr>

</tr>
</tbody>
</table>
...

其实际效果是一个一行的表格,我现在需要把它变成一个两行的表格。如果只有一个这样的表格,那很容易,修改增加红字部分即可:

...
<table>
<tbody>
<tr>

</tr>
<tr>
</tr>

</tbody>
</table>
...

可问题是我有100来个这样的表格,一个个改显然很傻。放到vim里批量改,才是个省时省力的好办法,虽然第一次麻烦点,但一劳永逸。直接给出修改方法:

%s/<tr>n</tr>/<tr>n</tr><tr>n</tr>/g

说明和注意事项如下:

  1. %s/a/b/x: vim中的替换命令,用b的内容替换a,x是参数,设定范围等信息。
  2. n: vim中的换行符。
  3. /: 转义/,因为在这个替换命令中“/”有特殊含义,是命令的分隔符,而我们真正需要的,作为普通字符处理的“/”如何表示呢?就要借助转义符“”,“/”即表示把“/”当普通字符处理。
  4. /g: 表示全文搜索,没有这个参数,默认是搜索当前行。
  5. 这只是个简单应用,其实有更简洁更高级的写法。这里就不罗列了。
  6. 如果你一点也看不懂也没有关系,因为我是写给自己看的,讲得很乱。有兴趣的请自行学习vim,绝对会让你有白活半辈子的感觉。

ubuntu下的快捷键–ctrl+s

先痛骂电信劫持了我网站的域名!登自己网站,上来竟给我来电信的广告,HOLY FUCK!一时激动忘了截图了,下次一定记得。

好了,爽了,我们开始。


今天在ubuntu终端下学习vim的教程,看到vim的帮助文件里写到ctrl-s可以分割窗体,在图形界面下我见识过,可是在终端上还能怎么分呢?我就好奇地试了试,结果它就给我装死了。。。失去任何响应。没办法,我以为是其他终端程序跑多了,这里一时没有资源,等其他终端都关掉,还是不行。再开一个新的终端,同样输入ctrl-s,反应依旧是。。。没有反应。我就纳闷了,马上去google。原来ctrl-s是linux的快捷键,把屏幕锁掉,解锁的话应该按ctrl-q。

我是在这里找到的解决方法,同时也看到了不少其他linux快捷键的介绍(没有全看完,不过乍一看就发现第二条写错了。<Alt><Fn>是无法切换终端的,应该是<Ctrl><Alt><Fn> (n=1..6) 才对),现在的习惯是看到好的资源文章就想办法找找英文的,英文资源比中文资源要好的多,技术文章就更不用说了。。。出处可能是在这里:Linux Shortcuts and Commands. 果然很强大啊,排版都看着舒服,只是不免有些沮丧,这才刚刚是Linux Newbie Administrator Guide,啥时候才能入门啊?