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.