Friday, April 3, 2009

Emacs Example Tutorial: find words and letters in a file

When using Emacs probably my #1 most used and favorite feature is to "find" or search for a string - strings are groups of characters that may or may not be part of a word, a whole word, or a sentence (to be exact there is no maximum length that I know of).

Once emacs is open just hit Ctrl-s (Hold down the Control Key and while holding it, tap the s key once, then let go of Ctrl). Many people will recognize this as the typical "save" command many graphical application use to save a file. Don't worry, emacs has a different save command [link]. After you have hit Ctrl s, emacs will wait for you to start typing the characters (letters) you are looking for. It sill search "as you go", meaning that if you type "f" it will jump right to the first "f", then if you type "i" it will move to the first occurrence of "fi", then if you continue typing "nd" it will move to the first occurrence of the word "find". If it does not find the character sequence you've typed in, it will tell you so.

After it finds the first occurrence of your string of characters, you can hit "Ctrl s" again to find the next occurrence of that same string in your document. You can continue hitting "Ctrl s" until you get to the end of your file, at which point Emacs will tell you "Failing I-search: [search-string-here]" which means it has hit the end of the file. If you continue taping "Ctrl s" even after that, Emacs will wrap your search back around to the start of the file and continue searching from the top again. You will know it has gone back to the top of the file when you see Emacs display "Overwrapped I-search: [search-string-here]" at the bottom of the window.