Thursday, June 11, 2009

Emacs Example Tutorial: replace words in a file

This example shows how to search-and-replace words in a file.

First we assume you are used to searching for strings or characters in a word, if you are not, please see this article on how to use emacs to find words and letters in a file.

You can replace strings or characters (words, letters) in a file in a manner very similar to find. You type this command (remember, M is the meta key, which is usually equal to holding down the alt key or tapping on the escape key):


    M %

So you can also think of this as:

    Alt Shift 5

After you type that key command sequence, Emacs will ask you:

    Query replace:

Then you type in the characters that you are looking for that you want to replace and hit enter. Then Emacs will ask you:

    Query replace [search-string-here] with:

Now you type in the characters you want to insert in place of the old word and again hit enter. Emacs will find the first instance of the "search" string (the word you want to replace), it will highlight that occurrence and then ask you:

    Query replacing [search-string-here] with [replacement-string-here]: (? for help)

If you want that occurrence to be replaced, you just tap the y key for "yes". Emacs will automatically move on to the next occurrence of the string you searched for and ask again:

    Query replacing [search-string-here] with [replacement-string-here]: (? for help)

If you want to skip any particular occurrence, just tap the n key for "no". Emacs will continue find and replace until you reach the bottom of the file.

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.

Sunday, March 8, 2009

Emacs Example Tutorial: exit emacs and save a file or discard changes

For those who are just starting to learn emacs, the key command sequence to save a file is:

Ctrl x s

That means hold down the Control key, tap the x key once, and the s key once, let go of Control key. If you're used to using "Cntl+s" in a graphical progam to save file, this key binding might seem a little awkward at first but you'll get used to it quickly.

You can also choose whether or not to save by simply exiting emacs and telling it "y" for "yes" or "n" for "no" when it asks you if you want to save your changes (also known as "Exit saving changes") by typing the "quit" key command sequence:

Ctrl x c

Emacs will then prompt you "Save file /path/to/filename? (y, n, !, ., q, C-r or C-h)" which means you must answer with one of the options listed inthe parenthesis. To save just tap the "y" key and you're done.

If you decide you don't want to save your changes, tap "n" for "no". Emacs will then double-check your decision by asking you:

Modified buffers exist; exit anyway? (yes or no)

Type "yes", which in this context means "yes I really do want to exit and discard my changes"
If you type "no" emacs will send you back to editing mode with Emacs still open and your unsaved changes still visible and accessable in the window.

Tuesday, March 3, 2009

Installing Mac OS X software updates from the commandline

I don't know why I didn't know this existed earlier - it's the most helpful computer administration command I've found in a year.

Apple's Software updates can be downloaded and installed on Mac OS X with the command "softwareupdate". This makes Updating your OS X remotely via ssh a snap compared to using ARD or VNC.

The command:

softwareupdate -l

will scan the apple update site for pending updates, and display them complete with a commandline-friendly label (name).

You can then install the updates you want with

softwareupdate -i [update_label]

For example:

$ softwareupdate -l
Software Update Tool
Copyright 2002-2005 Apple

Software Update found the following new or updated software:

   * JavaForMacOSX104Release7-1.0
      Java for Mac OS X 10.4, Release 7 (1.0), 82580 [recommended]

$ softwareupdate -i JavaForMacOSX104Release7-1.0

The command output shows which upates are recommended and which will requite a system restart.

The install command prints out a handy character based progress meter while it's downloading and installing:

Downloading Java for Mac OS X 10.4, Release 7
 Downloading Java for Mac OS X 10.4, Release 7
 0..20..40..60..80..100

 Expanding Java for Mac OS X 10.4, Release 7
 Installing Java for Mac OS X 10.4, Release 7
 0..20..40..60..80..100

 Done.

This is one terrific feature!

Wednesday, October 15, 2008

Emacs Example Tutorial: insert a block of text

As I explained in my previous Emacs tutorial on killing a block of text, it is sometimes hard to find easy to understand documentation on how to use emacs.
One of the first things I wanted to learn how to do in Emacs was block editing. By "block editing" I mean select a vertical area of text (one or more columns across multiple rows making a square or rectangle - you might call it zero or more since you don't actually have to select any characters, only the spaces between them) and do things to it - you might call it column editing or column mode or "Block Mode Editing" like in Kate, or UltraEdit.

Inserting a column of characters in emacs (without using a mouse) is really easy: you start by highlighting or selecting an area - when you do this you are said to "mark" the region.

You mark a region by positioning your cursor in one corner of the area you want to mark and hitting Control-Spacebar. You will see Emacs respond with the message "Mark set". Then navigate your cursor (I always use the arrow keys, but there is probably other ways to do it) to the opposite corner of the region you want to mark. The "region" is now marked as a rectangle between wherever you started (when you hit Ctrl-Space) and wherever your cursor is now sitting. Note that you don't do anything special to mark the end of the region -- the end is where ever your cursor is positioned.

Emacs calls the space you've now selected the "region-rectangle". Once it's highlighted, you can insert characters into the region by typing:

C-x r t

(That means press x while holding down the control key, release both keys, press and release the r key, press and release the t key). Emacs will respond with the message "String rectangle": it is asking you what text you wish to insert. Type what ever characters (letters) you want to "paste" into the region, and hit enter. It will automatically fill in that same text on every line that you've selected in your region.

I find this to be a great way to comment out areas of code with # characters. Emacs also has a "comment region" command which sometimes works, but occasionally emacs is too smart for it's own good and doesn't know what comment character to use (in which case it will usually ask you which to use with the question "No comment syntax is defined. Use:"), or worse it will use the wrong one.

You can use the comment region function by selecting a region in the same manner descried above, then type:

M-x comment-region

(That means press x while holing down the ALT key, release both keys, type "comment-region" - without the quotes of course - and hit enter).

I find C-x r t easier and faster to use than "M-x comment-region" in most cases anyway. It's fewer letters to type, even when you consider tab completion.

Tuesday, October 7, 2008

Linux on a Dell Vostro 1710 laptop - don't try it [yet]

Recently my company needed a new laptop for one of our employees. After my great success with installing SuSE Linux on a Dell Vostro 1700 laptop, we decided to buy a Dell Vostro 1710, hoping that it would have better, newer hardware and still work with SuSE.

Wrong.

For this first time in my entire 11+ years of using Linux, the installer could not use the trackpad or even the keyboard. Yes, you read that right. I was able to complete the install with an external USB mouse and keyboard, but after the install finished and I rebooted the system without the peripherals (hoping that it was only the installer having trouble), the laptop's built-in keyboard and mouse still wouldn't work. I can't image what Dell has done to prevent detection/use of a simple keyboard.

I never bothered to try the other hardware (camera, etc) - we sent the 1710 back and bought a refurbished 1700.

I strongly suggest nobody buy a 1710 until at least another round of distros has been released this coming winter or spring. It was openSuSE 11.0 that I tried on the 1710 - perhaps 11.1 will work with the 1710, but I'm sure I won't be ordering another one to try it on.

Monday, March 24, 2008

Setting the computer hardware clock (BIOS or CMOS) from inside a running Linux system

Normally after setting the date and time in Linux (for example using the NTP service or ntpdate, you can set the hardware clock to match with the following command:

hwclock --systohc

That way when you reboot, your clock will still be correct (or close to it).

I've discovered that this method does not work on the newer Dells, such as our 1U PowerEdge 1850s. Supposedly this has something to do with the kernel RTC module, and hardware changes. However, the following command appears to set the clock just fine on the problematic Dell servers:

hwclock --systohc --directisa