Tuesday, June 24, 2008

Learning Emacs

The learning curve for Emacs is difficult but challenging. I have been using Emacs for a few years now. But I am still a beginner using less than 5 different keys of the Emacs capabilities.

Few Emacs shortcuts are very useful and I shall summarized them below.

Etags

Etags are useful feature that could quickly tag files to be used in Emacs and vi.

Example:

#etags file1.c file2.h

This will generate a TAGS in the directory. Once you execute the opened file in emacs with require"Alt+.", the message "Find tag: (default printf)" will appear. Then it will ask you the TAGS file. Visit tags table: (default TAGS ~/test/). If the tag table can be found, the cursor will jump to the definition. To get back to the original cursor position, use "Alt+Shift+*".

Search

Searching for a text in Emacs is easy. Forward search using "Ctrl+s" and I-search: will appear.

Reverse the search using "Ctrl+r" and I-search backward: will appear.

To repeat the search, just press "Ctrl-s" or "Ctrl-r" repeatedly.

Cut and Paste

This is a complex operation using keyboard which requires lots of practice.

On the required line, "Ctrl+space" to start the cursor mark. Mark set will appear to indicate the operation is correct. The next command is sort of an invisible command. But rest assured that it just worked. Then move the cursor to the end location. "Esc" then "w" to copy the information from the start of the cursor to current cursor position. This completes the copy operation.

Next, is the paste operation. Go to the required location and "Cltr+y" will paste(a.k.a yank) the buffer(a.k.a clipboard) information.

Undo

This operation is very important because it will undo any mistake that you have made.

In most Emacs book, it is written in "C-x-u" which means "Ctrl-u" + "u".

Saving the file

"Ctrl-x" + "Ctrl-s" will save the current file.

Exit Emacs

"Mx-Ctrl-c" or "Cltr-x" + "Ctrl-c"

Extra Programming feature of Emacs

Splitting the Window

"Ctrl-x 2" will split current window into 2.

"Cltr-x o" will switch the cursor between the two windows.

"M-x shell" will open up the shell. M is "Alt key"

Tuesday, June 17, 2008

Useful search commands

Search for file with a specific name in a set of files (-name)

find . -name "rc.conf" -print

This command will search in the current directory and all sub directories for a file named rc.conf.

Note: The -print option will print out the path of any file that is found with that name. In general -print wil print out the path of any file that meets the find criteria.

How to search for a string in a selection of files (-exec grep ...).

Examples:

find . -exec grep -s "GMAC reset complete" '{}' \; -print

find . -exec grep -s "GMAC reset complete"#space#'{}'#space#\;#space#-print

Output:
printk(KERN_INFO "GMAC reset complete\n");
./vendor/linux-kernel/arch/arm/mach-oxnas/gmac-napi.c

Very important point to remember is to have a space between the end of the } and \; otherwise bash will complain cannot use the -exec option in find. The -s option in grep is used to suppress all the error message in traditional grep. For newer version of grep, use the -q option.

find . -exec grep "GMAC reset complete" '{}'\; -print

This command will search in the current directory and all sub directories. All files that contain the string will have their path printed to standard output.

If you want to just find each file then pass it on for processing use the -q grep option. This finds the first occurrance of the search string. It then signals success to find and find continues searching for more files.

find . -exec grep -q "www.athabasca" '{}' \; -print

This command is very important for process a series of files that contain a specific string. You can then process each file appropriately. An example is find all html files with the string "www.athabascau.ca". You can then process the files with a sed script to change those occurrances of "www.athabascau.ca" with "intra.athabascau.ca".