31 October, 2009

Filter file content in Vim

The following is a simple method to do a 'grep' in the Vim buffer, without do it in the shell.

Consider the following file content:

  public class SomeThing {
    // some constant
    final static public String CONSTANT_A = "a";
    // another constant
    final static public String CONSTANT_B = "b";
    // yet another constant
    final static public String CONSTANT_C = "c";
  }

To filter out the constant definition lines ('final static public ...'), you can use a 'g' command in Vim to filter lines by regex and append to a register 'x'.

:g/final static public/normal "Xyy

Then, paste the register in somewhere you need it. For example, to put it in new buffer.

Vim command line editing

Paste the contents of register into the command line is very useful, for example, you can edit a non-trivial command in normal vim buffer, or you can copy some text from the file. There are at least two way to do it:

The 'CTRL_R', in command mode, can be used for this [:help c_CTRL-R]

  1. Assume you saved some text in register 'a'
  2. Under command mode, press 'Ctrl-R a'
  3. The text in register 'a' will pasted in the command line

One powerful alternative is the command window [:help q:]

  1. Under normal mode, use 'q:' to enter command window
  2. Editing the command
  3. Press 'Enter' to execute, or 'Ctrl-C' to quit this mode

This two method is provided by Tim on the thread

CVS client in Cygwin and CVSNT doesn't play with each other!

I have puzzled by the strange CVS error for a few weeks and finally I sit down and dig out the real problem. If you need to use CVS on Windows and you are a Cygwin user, read on!

If you checkout the source using CVSNT client, and try to use Cygwin client to do other CVS operation, you will found CVS client will quickly failed with the error: "Repository not found".

If you try to checkout the source using Cygwin client and try to use CVSNT client to do other CVS operation, the CVSNT client will have strange result too.

The real reason is the silly line-ending issue. CVSNT will always convert sources from Unix line ending to DOS line ending, including the CVS specific files (CVS/Root for example). If you don't enable the line-ending conversion in Cygwin (I never do this), Cygwin client will never read the CVS file correctly, and no CVS operation can be performed properly.

On the other hand, if you checkout the source using Cygwin client, all source files and CVS specific files will be in Unix line-ending. CVSNT client cannot handle this properly too! There is a setting in CVSNT client to treat the work copy in Unix line ending, but it just seems doesn't work for me.

The solution? Stick with only one tools. I choose to stick with Cygwin client as it is the more natural for me. In additions, I found Eclipse and NetBeans can handle the source checked out via Cygwin client and CVSNT client. Hence, I haven't yet got any functionality miss out in Cygwin client + Eclipse/NetBeans combination.

There is a entry in Cygwin FAQ to enable the DOS/Unix line ending conversion mode in Cygwin, I haven't try this as I believe this mode may cause some problems on other programs / tools in Cygwin.

If you need a more details on this issue, the following places may helps:

Rescue Debian on QNAP TS-209

I've a QNAP TS-209 serve sitting on my home network and serving as file server, web server, etc. Thanks to the work of Martin, Debian can run on my box with only trivial effort. Just like most of the NAS, however, you can only access it via ssh/telnet unless you have serial access. If you made some silly mistake and it won't booting up properly, you will need some rescue methods. As it doesn't won't support to boot from CD/floppy, and there is no easy way to have console access, Live CD or old floppy bootdisk just doesn't help. Fortunately, the boot loader of QNAP (uBoot) support recovery mode, which will fetch a image and flash memory. This mechanism is very useful, and can be used to recover the Debian without reinstalling everything. Recovery mode is basically using tftp to download a binary image, hence, in order to use this, you must backup the correct image once you have installed the Debian. The image can be backup with the following command:


[debian-in-qnap] $ cat /dev/mtd1 /dev/mtd2 /dev/mtd3 > qnapimg.bin Once you made the image, backup it, it is the only file needed to recover the system. Note that the image is actually the linux kernel and initrd image, hence, you should redo the backup every time you upgrade kernel on the system. Now, when you need to recover the system, following the steps:
  • Setup tftp on the local network with IP address: 192.168.0.11
  • Put the qnapimg.bin under root directory of tftp
  • Start the QNAP in recocovery (shutdown, press and hold reset button, and start the system, release the reset until you hear two short beap sound
After some minutes, you system will restart using your recovery image.

01 May, 2009

Vim subsitute with expression expansion

Just read a really interesting Vim tips and tricks post on Vim substitute with expression. In a nutshell, Vim substitute command allow you to use Vim expression, whose used in Vim script. For example, the following command will change all 'CONFIG' to 'config'.

:%s,CONFIG,\=tolower(submatch(0)),

In this example, the key is '\=' notation, which is a switch to enter expression evaluation mode. The function submatch(0) refers to the full match of the substitute command, and both tolower() and submatch() is normal Vim functions only.

Read the Vi and Vim Editor: 12 Powerful Find and Replace Examples for more examples.

28 February, 2009

5 watt Linux server in a Power Plug

Marvell has announced a extremely low power consumption SoC device.

This is a really cool device, think you can have a box running web application, dhcp, dns proxy with just a power plug sized device.

When attached with an external disk, this will become an ultra low cost NAS and running media streaming, file sharing service

21 January, 2009

Oracle slow initial connection (default installation on Windows)

Recently, I'm managed to create a few Oracle DBs on various Windows box for development purpose.

As all are aimed for development purpose and no special configuration is planned. However, with default DB configuration on Windows, you will experience very slow initial connection lag time. To solve this problem, you need to disable NTS authentication.

To do this, edit sqlnet.or
SQLNET.AUTHENTICATION_SERVICES=(NONE)

Note that, you should see the original setting is NTS (which is native authentication method on Windows platform).

09 November, 2008

Great profiling tools VisualVM bundled with JDK6

Whenever encounter memory leak or performance problem, profiler will be your great friend.

Sometimes before, I have used some well known profiling tools like YourKit or hprof, jmap, tools, but recently, I discover JDK 6 has bundled with a new tools called VisualVM.

Indeed, the VisualVM is itself a self contained project, it provides some common monitoring features like thread/heap monitoring, as well as cpu / memory profiling features. It also allows you taking snapshots and compare memory snapshot.

IMO, it should deserve much more attention than its currently have. And it is really a perfect tools for most performance monitoring / tunning needs.

27 September, 2008

Happy Vimming again!

Vim has been my favorite editor for years. However, for Java related development I've switched to Eclipse. Vim is then only been used for general text editing (writing notes, scripts, etc).

Recently, I have to do fairly amount of coding in PL/SQL. After struggling for weeks to writing code in SQLDeveloper, I remember the lovely Vim.

Vim already bundled the syntax file for PLSQL and Oracle SQL,. However, Vim doesn't bundled with a good PL/SQL indentation setup, which can be found in here.

Another important setup is connecting sqlplus and vim and it is can be done by adding the line below into glogin.sql (sqlplus init file):

 DEFINE _EDITOR='vim -c "set filetype=plsql"'

Finally, to make the sqlplus usable, it is better to use rlwrap (which bring basic readline features like command history and hot key to the sqlplus).

09 August, 2008

Olympic, China, Microsoft

I'm originally going to blog about the amazing opening ceremony of the 2008 Olympic games on last night. But I just changed my mind as my good feeling is substituted with big anger!

Today is the second day of the Olympic games, I'm just going to checkout for the official means on watching the games via Internet. After all, I found that, i-cable is the only authorized company to provide the Olympic video online in Hong Kong. Hence, I visit the site and well, fail to watch any video clips.

The site shown that, Microsoft Windows Media Player 9 is required. Currently, I'm using Mac OSX as my primary desktop and I'm already installed the official WMV codec from MS website. And the sad fact is that, clearly MS Windows is prerequisite for watching the games. But hey, Olympic is the event of anyone on the earth, it seems now, Olympic is game for Microsoft users.

Nowadays, we can watch TV and listen to radio as long as you have working TV set or radio. But on the Internet, you must pay to one vendor: MS. Sorry I believe there is something wrong, what do you think?