play with Vi.
vi cheat sheet

play with Vi.

What is vi?

The UNIX vi editor is a full-screen editor and has two modes of operation:

  1. Command mode: commands which cause action to be taken on the file. every character typed is a command that does something to the text file being edited, a character typed in the command mode may even cause the vi editor to enter the insert mode.
  2. Insert mode: which entered text is inserted into the file. In the insert mode, every character typed is added to the text in the file.

pressing the <Esc> (Escape) key turns off the Insert mode.

Who is Bill Joy?

Bill Joy is a prominent American computer scientist and entrepreneur, best known for his significant contributions to the development of Unix and his co-founding of Sun Microsystems.

Joy earned a Bachelor of Science in Electrical Engineering from the University of Michigan and then went on to receive a Master of Science in Electrical Engineering and Computer Science from the University of California, Berkeley.

Bill Joy created the vi-text editor, which became one of the most widely used text editors in Unix systems. vi is renowned for its efficiency and effectiveness in text editing, particularly in environments where graphical user interfaces are not available.

Start and Exit Of vi.

To start vi:

To use vi on a file, type in vi filename, If the file named filename exists, then the first page (or screen) of the file will be displayed; if the file does not exist, then an empty file and screen are created into which you may enter text.

vi file_name            # edit file_name starting at line 1 .
vi -r file_name       # recover file_name that was being edited when system crashed.        

To Exit vi:

Usually, the new or modified file is saved when you leave vi. However, it is also possible to quit vi without saving the file.

The cursor moves to the bottom of the screen whenever a colon (:) is typed. This type of command is completed by hitting the <Return> (or <Enter>) key.

:x<enter> Or :wq<enter>:-? quit vi, writing out modified file to file named in original invocation.

:q<enter> :- quit vi.

:q!<enter>:- quit vi even though the latest changes have not been saved for this vi call.

Moving the Cursor.

the mouse does not move the cursor within the vi-editor screen. On some UNIX platforms, the arrow keys may be used as well. the arrow keys sometimes produce strange effects in vi and should be avoided.

j or <Return> [or down-arrow]: move the cursor down one line.

k [or up-arrow]: move the cursor up one line.

h or <Backspace> [or left-arrow]: move the cursor left one character.

l or <Space> [or right-arrow]: move the cursor right one character.

0 (zero): move the cursor to the start of the current line (the one with the cursor).

$: move the cursor to the end of the current line.

w: move the cursor to the beginning of the next word.

b: move the cursor back to the beginning of the preceding word.

:0<Return> or 1G: move the cursor to the first line in the file.

:n<Return> or nG: move the cursor to line n.

:$<Return> or G: move the cursor to the last line in the file.

Screen Manipulation.

The following commands allow the vi-editor screen (or window) to move up or down several lines and to be refreshed. The symbol ^ before a letter means that the <Ctrl> key should be held down while the letter key is pressed.

^f: move forward one screen.

^b: move backward one screen.

^d: move down (forward) one-half screen.

^u: move up (back) one-half screen.

^l: redraws the screen.

^r: redraws the screen, removing deleted lines.

Adding, Changing, and Deleting Text.

Perhaps the most important command is the one that allows you to back up and undo your last action. Unfortunately, this command acts like a toggle, undoing and redoing your most recent action. You cannot go back more than one step.

U: UNDO WHATEVER YOU JUST DID.

Inserting or Adding Text:

The following commands allow you to insert and add text. Each of these commands puts the vi editor into insert mode; thus, the <Esc> key must be pressed to terminate the entry of text and to put the vi editor back into command mode:

i: insert text before cursor, until <Esc> hit.

I: insert text at the beginning of the current line, until <Esc> hit.

a: append text after the cursor, until <Esc> hit.

A: append text to the end of the current line, until <Esc> hit.

o: open and put text in a new line below the current line, until <Esc> hit.

O: open and put text in a new line above the current line, until <Esc> hit.

Changing Text:

Changing Text: The following command allows you to modify text:

r: replace single character under cursor (no <Esc> needed).

R: replace characters, starting with the current cursor position, until <Esc> hit.

Deleting Text:

The following commands allow you to delete text:

x: delete a single character under the cursor.

dd: delete the entire current line.

How to cut and paste lines?

Cut the Line:

To cut (delete) the current line, use the dd command in Normal Mode.

To cut multiple lines, prefix the dd command with the number of lines you want to cut.

Copy (Yank):

To copy (yank) a line without deleting it, use the “yy” command.

To copy multiple lines, prefix the yy command with the number of lines.

Pasting Lines:

Use the “p” command to paste the line after the current line.

Use the “P” command to paste the line before the current line.

Undo the Last Change.

Press u in Normal Mode to undo the last change.

Redoing Changes.

If you undo a change and then decide you want to redo it, you can use the Ctrl-r command in Normal Mode.

Search forward and backward

/string: search forward for the occurrence of a string in text.

?string: search backward for the occurrence of a string in text.

n: move to the next occurrence of search string.

N: move to the next occurrence of search string in the opposite direction.


要查看或添加评论,请登录

Muhammad Morsy的更多文章

  • Introduction to Git

    Introduction to Git

    Version control system Version control: is a system that helps to manage changes to documents, code, and other…

  • Linux navigation

    Linux navigation

    What is the Shell? Simply put, the shell is a program that takes commands from the keyboard and gives them to the…

  • GNU Emacs Overview

    GNU Emacs Overview

    What is Emacs( GNU Emacs )? GNU Emacs is a highly extensible, customizable, and powerful text editor developed by the…

社区洞察

其他会员也浏览了