Thursday, August 31, 2006

[Vim] Sex or Ex

Ooooh racy title eh? Sex is a vim command that stands for Split Explorer. What it does is split your gvim screen and displays a file explorer in the new window. You can then double click on the file you wish to open and it will be opened on the same window. Ex (take note of the uppercase E) is merely an explorer. It displays the explorer in the same window. You can then select the file you want to open. The file you are editing prior to the Ex command would be in a different buffer. Type :buffers if you want to see the buffer number it went to.

Thursday, August 24, 2006

[Shell.Utils] Recursively grep for a pattern down a directory tree.

I often find myself changing directories using cd or using relative paths and wildcards to grep for a pattern down a directory tree. This could be done in one command line sequence using the find utility.

find /work –print xargs grep ‘[Ee]rror’

The command listing above uses find to print all the files under the /work directory. The output of the find utility was piped to the xargs utility. Quoting from Linux in a Nutshell 3rd Edition

xargs [options] [command]

Execute command (with any initial arguments), but read remaining arguments from standard input instead of specifying them directly. xargs passes these arguments in several bundles to command, allowing command to process more arguments than it could normally handle at once. The arguments are typically a long list of filenames (generated by ls or find, for example) that get passed to xargs via a pipe.

Xargs enables grep to find the pattern ‘[Er]rror’ in the files piped from the find command.

Another example:

find /work –type f –name ‘*.txt’ –print xargs grep –l ‘[Ee]rror’

in this example we are looking for the word Error or error in the files with extension .txt under the work directory. The –l switch used for grep just prints out the filenames where the pattern was found. The –type f switch for find limits our search to files. The –name switch limits our search to just files with .txt extension.

[AoN] Speaking UNIX

Found two articles with lots of useful tidbits of info about using UNIX. The articles shows the beauty of the new shell ‘zsh’. Hope to learn more of this new shell in the future. Here is a link to the two articles:

Speaking UNIX: Command the power of the command line

Speaking UNIX, Part2: Working Smarter, not harder.

New category! [AON]

I am adding a new category [AoN] to the blog. Stands for Article of Note. Isn’t that nice!

Wednesday, August 23, 2006

[Vim] gf 'get file'

Ever had the moment where in you have a file with complete path in the text file your are editing that you want to view/edit with out using your mouse to highlight it and pasting it in the vim command line? Well gvim have a very nifty shortcut for it. Place your cursor on the file you want to see and type gf . I don’t know what gf stands for. I assume ‘get file’ J. Anyway that sure is a time saver.

Friday, August 18, 2006

[Vim] Adding your own helpfile in Vim

If you want to add your own help file to vim so that you can invoke the file when you type :help (for example a collection of your gvim commands) then do this:

  1. Create your_help.txt file and place it in your $HOME/.vim/doc directory.
  2. Issue the `:helptags ~/.vim/doc` command in vim.
  3. To view your help file just type :help <your_helpfile>
  4. TAB completion works too just type :help <your_<TAB>>

That’s all there is to it!

[Vim] grep in vim

Often times you would want to search for all occurrences of a particular word or pattern in a file. Using ‘*’ would be too tedious. So Here comes grep. Basically you use it this way

:grep <pattern> <file you want to grep>

Now after you do that, vim will show you all the lines matching. By typing :cope the vim window will be split. The bottom window would show all the matching lines, hyperlinked to the document you grepped.

[Vim] The power of '*'!

Use * to search the word where the cursor is.