Thursday, May 20, 2010
Editing commands require that you be in command mode. Many of the editing commands have a different function depending on whether they are typed in upper or lower case. Often, editing commands can be preceded by a number to indicate a repetition of the command.
NOTE: Unless specified otherwise, the instructions in this section assume that you are in the command mode.
Deleting characters. To delete a character from a file, move the cursor until it is on the incorrect letter, then type x. The character under the cursor disappears. To remove four characters (the one under the cursor and the next three) type 4x. To delete the character before the cursor, type X (upper case).
Deleting words. To delete a word, move the cursor to the first letter of the word and type dw. The command deletes the word and the space following it. To delete three words, type 3dw.
Deleting lines. To delete a whole line, type dd. The cursor does not have to be at the beginning of the line. dd deletes the entire line containing the cursor and places the cursor at the start of the next line. 2dd deletes two lines.
To delete from the cursor position to the end of the line, type D (upper case).
Replacing characters. To replace one character with another, move the cursor to the character to be replaced. Type r, then the replacement character. The new character will appear, and you will still be in command mode.
Replacing words. To replace one word with another, move to the start of the incorrect word and type cw. The last letter of the word to be replaced will turn into a $. You are now in change mode and may type the replacement. The new text does not need to be the same length as the original. Press ESC to get back to command mode. 3cw allows you to replace three words.
Replacing lines. To change text from the cursor position to the end of the line, type C (upper case). Type the replacement text and press ESC. Press ESC again to get back to command mode.
Inserting text. To insert text in a line, position the cursor where the new text should go and type i. Enter the new text. The text is inserted before the cursor. Press ESC to get back to command mode.
Appending text. To add text to the end of a line, position the cursor on the last letter of the line and type a. Enter the new text. This adds text after the cursor. Press ESC to get back to command mode.
Opening a blank line. Type o (lower case) to insert, or open, a blank line below the current line. Type O (upper case) to insert a blank line above the current line. Press ESC to get back to command mode.
Joining lines. The command J (upper case) lets you join two lines together. Put the cursor on the first line to be joined and type J. 3J lets you join 3 lines together.
Undoing. Undo your most recent edit by typing u (lower case). U (upper case) undoes all the edits on a single line, as long as the cursor stays on that line. Once you move off a line, you can't use U to restore it.
Moving Around In a File
These shortcuts, which work in command mode, allow you to move more quickly through a file.
forward word by word
backward word by word
to end of line
to beginning of line
to top line of screen
to middle line of screen
to last line of screen
to last line of file
to first line of file
jump forward one screen
jump backward one screen
scroll down one-half screen
scroll up one-half screen
Moving by searching. To move quickly in a file, search for text. In command mode, type a / (slash) followed by the text to search for. Press Return. The cursor moves to the first occurrence of that text. Repeat the search in a forward direction by typing n (lower case), or in a backward direction by typing N (upper case).
Locate commands by keyword lookup.
Terminate your current session, or shell.
Display the Unix manual page describing a given Unix command.
Return to your home directory.
Change directory to make directory your current directory.
Determine file type.
List the contents of the current directory.
List the contents of the directories; names can name files and/or directories:
. . . in a long format, showing permissions, owner, size, and other file info.
. . . all files, including "hidden" files (file names that begin with a dot ".").
. . . Recursively, for all subdirectories.
. . . in time order (when modified, newest to oldest) rather than in name order.
Display the name of the current directory, or "print working directory."
Reduces the size of a file.
Restores compressed files to their original form.
cp file1 file2
cp files directory
Copy file(s) into a directory.
cp -r dir1 dir2
Copy a directory and, recursively, its subdirectories.
Create, or "make" a directory.
mv file1 file2
Move a file or, if file1 and file2 are in the same directory, rename a file.
mv files directory
Move files into a directory.
mv dir1 dir2
If directory dir2 exists, move dir1 into dir2; otherwise, rename dir1 as dir2.
Remove (erase) file(s).
rm -r names
Remove files, directories, and recursively, any subdirectories.
Remove directory (directory must be empty).
Concatenate file(s); you can use cat to display the contents of a file (this is not advisable if the file is a binary file).
grep "pattern" files
Display all lines in the files that match a pattern.
Display contents of files one screen at a time.
Order the lines in a file or files alphabetically (this command does not alter the file or files -- it merely displays the sorted output to the screen):
sort -r files
. . . in reverse order.
sort -n files
. . . numerically (puts 2 before 10 instead of after).
Displays information about a U-M user from the U-M Online Directory.
Connect to a remote host using Secure Shell.
Connect to a remote host using the telnet protocol.
Initiate a conversation with another user (end conversation with Control-C); talk works only between machines of the same architecture.
Repeat last shell command.
Repeat last shell command that began with string (for example, type "!m" to repeat the last command that began with "m").
Display a calendar of the current month.
cal month year
Display a calendar of the given month and year. Note that the year must be fully qualified, for example, "2003" and not "03."
Clears terminal screen.
Display the current local date and time.
Display a list of users currently logged in.
Q. What is echo $? Command do?
A. It returns the status of last command run. E.g if we run grep command then if the patter is found in the file it shows 0, pattern not found then 1 and file not found then 2.