Vim

2021 年 4 月 27 日 星期二(已编辑)
1
这篇文章上次修改于 2021 年 4 月 27 日 星期二,可能部分内容已经不适用,如有疑问可询问作者。

Vim

Vim

What is Vim?

Vim is a greatly improved version of the good old UNIX editor Vi. Many new features have been added: multi-level undo, syntax highlighting, command line history, on-line help, spell checking, filename completion, block operations, script language, etc. There is also a Graphical User Interface (GUI) available. Still, Vi compatibility is maintained, those who have Vi "in the fingers" will feel at home. See runtime/doc/vi_diff.txt for differences with Vi.

This editor is very useful for editing programs and other plain text files. All commands are given with normal keyboard characters, so those who can type with ten fingers can work very fast. Additionally, function keys can be mapped to commands by the user, and the mouse can be used.

Vim runs under MS-Windows (XP, Vista, 7, 8, 10), macOS, Haiku, VMS and almost all flavours of UNIX. Porting to other systems should not be very difficult. Older versions of Vim run on MS-DOS, MS-Windows 95/98/Me/NT/2000, Amiga DOS, Atari MiNT, BeOS, RISC OS and OS/2. These are no longer maintained.

——Github/Vim

How to use Vim?

In Terminal

  • vim : Open Vim

  • vim [filename] : Open [filename] in Vim

Modes

In Vim, there are three modes of operation: Normal, Insert, and Visual.

Normal mode

Normal mode is the initial mode of the Vim editor. When you open a new file edit an existing one, it starts in normal mode by default. In normal mode, you cannot insert any character. Normal mode is also known as command mode because all the keystrokes you perform are interpreted as commands.

To access normal mode from other modes, press Esc key.

Insert mode

Insert mode is where you can insert your text in the file. This mode inserts every character you type at the current cursor location.

Visual mode

Visual mode allows you to select text so that you may perform certain operations (cut, copy, delete) on it.

Changing the modes

  • When creating or opening a file in vim, it first opens in Normal mode.
  • Switch to Insert mode from Normal mode: i or I, o, O, a, A.
  • Switch to Visual mode from Normal mode: v or V
  • Switch between Inset mode and Visual mode: shift to Normal mode first.

Commands (in Normal mode)

KeyCommand
:wWrite the file to the disk
:w [filename]Save the file as [filename]
:qQuit vi without saving the file
:wqWrite the file to disk and quit vim
:wq [filename]Quit and save the file as [filename]
:q!Ignore the warning and discard the change

Moving the cursor

KeyCommand
h / LeftMove the cursor left one character
j / DownMove the cursor down one line
k / UpMove the cursor up one line
l / RightMove the cursor right one character
wMove the cursor forward one word
bMove the cursor backward one word
Control f / Page DownMove the cursor forward one page
Control b / Page UpMove the cursor backward one page
[number]ggMove the cursor to line [number]
[number]jMove the cursor down [number] lines
[number]kMove the cursor up [number] lines

Finding text

KeyCommand
/[text]Find all [text]
nMove to next [text]
Shift nMove to previous [text]

Inserting text

KeyCommand
IInsert text at the beginning of the line
iInsert text before the current cursor location
aInsert text after the current cursor location
oCreate a new line for the text below the current cursor location
OCreate a new line for text above the current cursor location

Changing text

KeyCommand
ccRemove the whole line and start Insert mode
c[number]cRemove the whole [number] lines and start Insert mode
sRemove the character under the cursor and start Insert mode
rReplace the character under the cursor

Copying & Pasting

KeyCommand
yCopy the selected text to clipboard
yyCopy current line
Pinsert the text before the cursor
pInsert the text at the point after the cursor

Deleting text

KeyCommand
XDelete the character before the current location
xDelete the character under the current location
DCut to the end of line
ddCut current line

Undo & Redo

KeyCommand
uUndo last change
Control rRedo last change

Customize Vim

Enter vim ~/.vimrc in Terminal

  • syntax on : Code highlighting
  • set number : Line number

使用社交账号登录

  • Loading...
  • Loading...
  • Loading...
  • Loading...
  • Loading...