Wednesday, October 2, 2013

VI editor Shortcuts and Tricks



VI editor Shortcuts and Tricks

Definition -

The VI editor is a screen−based text editor available on all Unix computers.

Importance of VI editor -

1)      When we log on a remote server using SSH from a PC, only the text editors can be used to edit files.
      2)  Typists love working on it as it reduce the keyboard – mouse switch over time.
Creating new text file or editing an existing text file –
To edit a file named (say) "mytext" on a Unix computer, type the Unix command "vi
mytext".
# vi mytext
Or
#vim mytext
Different modes of VI Editor –
1)      Insert Mode - To switch to insert mode: press i (or a, or o)
In insert mode, everything you type appears in the document at the place where the
blinking cursor is.

2)      Command Mode - To switch to command mode: press Esc
In command mode, keystrokes perform special functions just like mouse and menu bar.
Exiting the VI Editor –
When you want to get out of the editor, switch to command mode (press Esc) if
necessary, and then
· type :wq RTN to save the edited file and quit, or
· type :q! RTN to quit the editor without saving changes, or
· type ZZ to save and quit (a shortcut for :wq Rtn), or
· type :w filename to save the edited file to new file "filename"
Note – RTN means enter key
Moving around in the Editor without Mouse
First Enter into your command mode then you can use the arrow keys to move the cursor up,
down, left, right. In addition, these keystrokes will move the cursor.
h left one character
l right one character
k up one line
j down one line
b back one word
f forward one word
{ up one paragraph
} down one paragraph
$ to end of the line
Control + B = back one page
Control + F = forward one page
17G to line #17
G to the last line.
Inserting Text
From command mode, these keystrokes switch you into insert mode with new text being inserted.

i just before the current cursor position
a just after the current cursor position
o into a new line below current cursor
I at the beginning of the current line
A at the end of the current line
O into a new line above current cursor

Cut, Copy, Paste Shortcuts-

 x delete (cut) character under the cursor
 24x delete (cut) 24 characters
 dd delete (cut) current line
 4dd delete (cut) four lines
 D delete to the end of the line from the cursor
 dw delete to the end of the current word
 yy copy (without cutting) current line
 5yy copy (without cutting) 5 lines
 p paste after current cursor position/line
 P paste before current cursor position/line

Search –
 We can go directly forward or backward to specified text using "/" and "?"

 /adarsh Rtn Move cursor to the next occurrence of the string "adarsh"
?adarsh Rtn  Move cursor backward to the previous occurrence of the string "adarsh"
 n repeat the last search given by "/" or "?"

Replace Commands -

 r replace 1 character (under the cursor) with another character
 8r replace each of the next 8 characters with a given character
R overwrite; replace text with typed input, ended with Esc
 C replace from cursor to end of line, with typed input (ended with Esc)
 S replace entire line with typed input (ended with Esc)
 4S replace 4 lines with typed input (ended with Esc)
 cw replace (remainder of) word with typed input (ended with Esc)

Source - Internet

Searching in linux



Searching in linux

In Linux there are two commands for searching through files and directories.

1)       Grep.
2)      Find.

The grep command searches the contents of files and can extract information from them according to the pattern specified by us. The find command locates files and directories according to our query.

GREP -

By the help of grep command you can search through files to print lines containing specified words and phrases. And here we need not to bother to open the file with an editor.

Syntax - grep options expression filename

Examples of options in Grep

-v
Reverses the normal use of the grep command - Instead of selecting lines, it rejects the lines that match the given criteria.
-c
It only prints the total line count of matching lines.
-i
Ignores the case of the text when matching the given pattern.
-w
Checks if the given pattern is a word by itself and not a part of another word.
-l
Only gives the names of the files in which the given pattern was found

Expression is the word or phrase you want to find.

Filename is the name of the file or files you want to search.

For example, if you wanted to find all lines containing the word "hill" in a file named "MLK" use -

grep hill MLK

Output
unity% grep hill MLK
I have a dream that one day on the red hills of Georgia, sons of...
hill and mountain shall be made low, the rough places shall be made...
So let freedom ring from the prodigious hilltops of New...
Let freedom ring from every hill and molehill of Mississippi,...
unity%

To search through a directory of files for every occurence of a word or phrase, use the syntax grep word *
For instance, using the command grep people * might produce a list like the following:

unity% grep people *
my_speech:people who are self-employed.
my_speech:too few people in too many rooms. I recommend three
my_speech:paper is about how several people
old_speech:people working on a project together, but a broader
from_boss:say that co. should not be afraid of people
grep: Read error on work_dir: Is a directory

The name of each file containing the sought word is listed, along with the line in that file containing the word. The last line of this example is an error message generated when the grep command attempted to read a directory. In most cases (such as this one) these errors can be ignored.

Searching for files (find)

The find command searches for files according to their name.

Syntax - find pathname expression

The pathname is the directory where you want the search to begin; in most instances just put a period (.) to represent the current directory and all subdirectories.
You should use find to search only within your own directories. If you attempt to search someone else's, the search will take much longer and you will be denied access to many of the directories that the find command locates.
To search your home directory (and all directories within it) for a file named "elmrc" you would enter

find . -name elmrc -print

unity% find . -name elmrc -print
./.elm/elmrc
unity%

In this example

. is the pathname, the directory from which find begins its search.

-name elmrc indicates what find should search for (a file named "elmrc").

-print instructs find to print the results of its search to the screen.

The find results indicate that the "elmrc" file is in the ".elm" directory which is in the current directory.
If you're not sure what file you're looking for, you can use wildcard characters, provided they're
enclosed in quotation marks.

For instance, to search for all files starting with the "elm" you would enter the command
 find . –name "elm*" -print

unity% find . -name "elm*" -print
./.elm/elmrc
./.elm/elmrc.old
./misc/elm.saving
unity%