In Emacs' normal find-and-replace mode you can't replace special characters like "beginning of line" or "end of line" with anything because it treats the special characters representing end of line (^ and $, respectively) as regular characters - a literal ^ and a literal $.
If you want to replace the beginning of line or end of line with another string (and this can come in extremely handy some times) you have to use a different kind of replace: RegExp replace (regexp stands for the term regular expression, which is a phrase used to describe computer pattern matching code or directions. Think "filters"). To get Emacs into this mode type:
M-x replace-regexp
Remember, M is the meta key, which is usually equal to holding down the alt key or tapping on the escape key. So you can think of this as:
ALT x
Emacs will prompt you that it is waiting for more instructions by displaying "M-x". Then you type in:
replace-regexp
and hit enter. Emacs will now ask you "Replace regexp: " - it is waiting to know the regular expression that matches the characters you want to replace.
So let's say you have a file of information - a list of names of employees, for example. This hypothetical list contains nothing but their names. Let's say you want to add a note at the end of the line that reads "Note: give this employee a new mouse pad:."
While emacs is sitting there asking you "Replace regexp: " type in just the dollar sign and then hit enter: $
Now Emacs will ask you "Replace regexp $ with: "
You can now type in any text you want, and then hit enter - Emacs will append that text to the end of the line. Unlike the regular find-and-replace, replace-regexp does not ask you for confirmation one line at a time, it will simply apply this "replacement" to every line of the file at the end-of-line mark.
As a very cool side benefit, you can combine this with block editing mode to constrain the replacement to a fixed block of lines. For more on block editing mode and how to define a block, see this article on emacs block editing.
Showing posts with label Mac OS X. Show all posts
Showing posts with label Mac OS X. Show all posts
Thursday, July 9, 2009
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.
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.
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.
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!
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.
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.
Labels:
administration,
computer,
linux,
Mac OS X,
system administration
Wednesday, November 28, 2007
How to enable the root user in Mac OS X using NetInfo, or change root's password:
- Open the NetInfo Manager utility (located under /Applications/Utilites/).
- Click the lock (padlock icon) in the NetInfo Manager window.
- Enter an administrator account name and password, then click OK (this isn't Administrator as in root, it's Administrator as in a user account with administrative privileges, typically the first account created during the initial install or setup).
- For Mac OS X 10.2 and later, choose Enable Root User from the Security menu.
- If you have not previously set a root password, an alert box may appear that says "NetInfo Error," indicating that the password is blank. Click OK.
- Enter the root password you wish to use and click Set.
- Enter the password again for verification and click Verify.
- The root user is now enabled.
- Click the lock again to prevent changes.
Wednesday, May 2, 2007
Mac OS X Keyboard Shortcuts
Apple provides a nice list of keyboard shortcuts for OS X:
http://docs.info.apple.com/article.html?artnum=75459
My favorite: "Shift-Command-Delete": Empty Trash
http://docs.info.apple.com/article.html?artnum=75459
My favorite: "Shift-Command-Delete": Empty Trash
Subscribe to:
Posts (Atom)