Emergency vi (vim tutorial)

Emergency vi (vim tutorial)

vi is one of unix's fsckup. Sometimes in a emergency situation you are forced to deal with vi, and fscking unix admins refuse to install emacs. Here's your help.

Emergency vi (vim tutorial)_第1张图片vi man
  • To open a file named “myfile.txt”, type in the shell prompt: vi myfile.txt
  • To quit without saving, press: 【Esc : q ! Enter ↵】
  • To save current file, press: 【Esc : w Enter ↵】
  • To quit vi, type: 【Esc : q Enter ↵】
  • Use arrow keys to move the cursor.
  • page up: 【Ctrl+b】
  • page down: 【Ctrl+f】

To insert text, move your cursor to the right place, then press i, then type your text. After you are done, type Esc to exit the insertion mode.

To delete text, move cursor to the right place and press x. (if x is inserted, that means you forgot to exit the insert mode. Type Esc to exit insertion mode. Type u to undo.)

When something doesn't work, or if you accidentally typed something and have no idea what is going on, try press Esc, then type u for undo. If a cat jumped onto your keyboard and you cannot exit vi, type the shell suspend command 【Ctrl+z】, which should get you back on the unix command prompt, then you can kill. (by first find out the pid by ps auwwx | grep vi, then kill -9 pid).

Congratulation! You've learned emergency vi.

It may be confusing, but with the above you can edit any text files with vi.

Installing vim

By default, the vi in Ubuntu 12.04 (as of 2013-02-11) is “vim.tiny”, a very basic vi-like vim. You should install full vim. For how to install and other detail, see: Ubuntu Linux: vim Location and Versions.

Basic vi Commands

Remember, in vi, it has modes. At any one time, you are either in command mode or text insertion mode. To go to the command mode, just press Esc. To go into editing mode, press i.

Goto Insertion Mode

Key Meaning
【i】 go to insert mode. (position before cursor)
【a】 go to insert mode. (position after cursor)
【o】 go to insert mode and insert a line. (position after current line)

Undo & Redo

Key Meaning
【u】 undo
【Ctrl+r】 redo

Cursor Movement

Key Meaning
【j】 down
【k】 up
【h】 left
【l】 right
【w】 next word
【b】 prev word
【0】 beginning of line
【$】 end of line
【:8】 line 8
【:$】 end of file
【Ctrl+f】 page down
【Ctrl+b】 page up

Deleting Text

Key Meaning
【x】 delete char
【8x】 delete 8 chars
【dw】 delete word
【8dw】 delete 8 words
【D】 delete from cursor to end of line
【dd】 delete current line

Copy & Paste

How to copy?

press 【v】 at the beginning of copy/cut position, then move cursor, then press 【d】 to cut, 【y】 to copy.

Key Meaning
【yy】 copy current line
【3yy】 copy 3 lines
【p】 paste after the cursor.
【P】 paste before the cursor.

Note that when you delete a word or many words, the word is automatically put into the clipboard.

Search

Key Meaning
【/aa】 search forward for text “aa”
【n】 next occurrence
【N】 previous occurrence
【*】 find next occurrence of current word

Find & Replace

Key Meaning
【:s/aa/bb/g】 search & replace ALL “aa” by “bb” in CURRENT LINE. “g” (global) means do it for all occurrences.
【:%s/aa/bb/gc】 search & replace ALL “aa” by “bb” in whole file. “c” means ask for confirmation before each replacement.
【:20,30s/aa/bb/gc】 search & replace “aa” by “bb” in line 20 to 30.

Comment/Uncomment Block

How to comment/uncomment a text block?

press 【Ctrl+v】 (called “visual block”), move cursor to select, then press 【I # Esc】 to insert # to all lines in the block.

To uncomment a block, do the same except just press 【x】 to kill a char.

Syntax Coloring

Key Meaning
【:set syn=lang】 the “lang” can be perl, c, cpp, html, php, JavaScript, python etc.
【:syntax on】 Turn on syntax coloring
【:syntax off】 Turn off syntax coloring

Line Numbers

Key Meaning
【:set number】 Turn on line numbers
【:set nonumber】 Turn off line numbers

Advanced Topics

【gg】 beginning of file
【G】 end of file
【dG】 delete from current line to end of file

Opening File

【:tabe path】 opening file
【gf】 open path under cursor
Emergency vi (vim tutorial)_第2张图片

Vim Init Files

default vim init file path: ~/.vimrc

  • vim -u filename → start vim with a different init file.
  • vim -u NONE → start vim without init.
  • vim -u NORC → start vim without init but with plugin.

How to Remap the Escape Key?

Vim: How to Remap the Escape Key?

misc notes

How to refresh buffer?

Type 【:e】

  • on vi Keybinding vs Emacs Keybinding
  • History of Emacs & vi Keys (Keyboard Influence on Keybinding Design)
  • Emacs Basics
  • Arrow Keys Layout Efficiency: vi's {H J K L} vs Inverted T {I J K L}
  • Text Editor's Cursor Movement Behavior (emacs, vi, Notepad++)
  • vi, vim: 3 Decades Pile of History

你可能感兴趣的:(vi,vim)