Header Ads

3.2 - Linux : Visual Editor

Introduction to Visual Editor

• vi or vim
• To open a file

$ vi <filename> or vim <filename>
• vi opens a file in command mode while starting
• The power of vi comes from its 3 modes
• Escape mode (Command mode)
       o Search mode
       o File mode
• Editing mode
       o Insert mode
       o Append mode
       o Open mode
       o Replace mode
• Visual mode.

Cursor Movement

• You will clearly need to move the cursor around your file. You can move the cursor in command mode.
• vi has many different cursor movement commands. The four basic keys appear below

Command Process
1. k Move up one line.
2. h Move one character to the left.
3. l Move one character to the right.
4. j Move down one line.

• Yes! Arrow keys also do work. But these makes typing faster

Basic vi Commands

• How to exit?

Command Meaning
1. :q Quit without saving.
2. :wq Close the file with saving.
3. :q! Quit the file forcefully without saving.

Escape mode or Command mode

In command mode, characters you perform actions like moving the cursor, cutting or copying text, or searching for some particular text
Search mode:
    • vi can search the entire file for a given string of text. A string is a sequence of characters. vi searches forward with the slash (/) key and string to search.
    • To cancel the search, press ESC .You can search again by typing n(forward) or N (backward).
    • Also, when vi reaches the end of the text, it continues searching from the beginning. This feature is called wrap scan. Instead of (/), you may also use question (?). That would have direction reversed
    • Now, try out. Start vi as usual and try a simple search. Type /<string> and press n and N a few times to see where the cursor goes.


• Escape mode.

File mode:
    • Changing (Replacing) Text

Command Meaning
1. :%s/first/sec Replaces the first by second every where in the file
2. :%s/old/new/gc For all lines in a file, find string “old" and replace with string “new" for each instance on a line.
3. :e Open another file without closing the current.
4. set all Display all settings of your session.
5. :r filename Reads file named filename in place.

• Editing Modes.

Command Mode name Insertion Point
a Append Just after the current character
A Append End of the current line
i Insert Just after the current character
I Insert Beginning of the current line
o Open New line below the current line
O Append New line below the current line

• Editing Text

  • Deleting Text Sometimes you will want to delete some of the text you are editing. 
  • To do so, first move the cursor so that it covers the first character of the group you want to delete, then type the desired command from the table below.
Command Meaning
dd For deleting a line
ndd For deleting a n lines.
x To delete a single character.
shift + d Delete contents of line after cursor
dw Delete word
ndw Delete 'n' word

Some Useful Shortcuts

Command Meaning
shift+g Go to last line in file
shift+j Joining the two lines.
. Repeat the previous command executed.
ctrl+a Increment number under the cursor
ctrl+x Decrements numbers under the cursor

 

Visual Mode:

Visual mode helps to visually select some text, may be seen as a sub mode of the command mode to switch from the command mode to the visual mode type one of
• Ctrl+v Go's to visual block mode.
• Only v for visual mode
• d or y Delete or Yank selected text
• I or A Insert or Append text in all lines (visual block only)


....

No comments