vim

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

1 vi

1.1 Cutting and Pasting Text

yy copy (yank, cut) the current line into the buffer

Nyy or yNy #copy (yank, cut) the next N lines, including the current line, into the buffer

p put (paste) the line(s) in the buffer into the text after the current line

Vim is an text editor.

1.2 Modal

the mode that the editor is in determines whether the alphanumeric keys will input those characters or move the cursor through the document.

1.3 Why use vim?

if do any of the following, you probably want to look into Vim:

• System administration

• Programming

• Working with HTML, LaTeX, or other markup languages

• Heavy editing of plain text files

To start Vim from the shell prompt type:  vim filename <ENTER>

1.4 The Modes

insert mode, command mode, and last-line mode

Esc  #command mode

I        # insert mode

# last-line mode

i.e.

:w # write the file

:q to exit the editor.

1.5 command mode

move around/jump:

      • h moves the cursor one character to the left.

      • j moves the cursor down one line.

      • k moves the cursor up one line.

      • l moves the cursor one character to the right.

      • 0 moves the cursor to the beginning of the line.

      • $ moves the cursor to the end of the line.

      • w move forward one word.

      • b move backward one word.

      • G move to the end of the file.

      • gg move to the beginning of the file.

      • `. move to the last edit.

      • A:append and switch to insert mode

• i → Insert mode. Type ESC to return to Normal mode.

• x : Delete the char under the cursor

• :wq → Save and Quit (:w save, :q quit)

• dd → Delete (and copy) the current line

• p → Paste

To go to (jump) to a user specified line:

change to command model

> Esc

specify line number:

> 3081

> S( shift) +g

From http://yannesposito.com/Scratch/en/blog/Learn-Vim-Progressively/

Here's a handy tip: prefacing a movement command with a number will execute that movement multiple times. So, if you want to move up six lines, enter 6k and Vim will move the cursor up six lines. If you want to move over five words, enter 5w. To move 10 words back, use 10b.

Editing a file

:e[dit] Edit the current file. This is useful to re-edit the current file, when it has been changed outside of Vim.

:e[dit]! Edit the current file always. Discard any changes to the current buffer. This is useful if you want to start all over again.

:e[dit] {file} Edit {file}.

:e[dit]! {file} Edit {file} always. Discard any changes to the current buffer.

gf Edit the file whose name is under or after the cursor. Mnemonic: "goto file".

From https://www.fprintf.net/vimCheatSheet.html

Editing Vim Style

0# move to start of the line

x #delete the character that the cursor was on.

u# undo the last command executed.

U(capital)# undo a line

1.6 Deleting

delete 100 lines forward from (including) the current one

repeat dd (delete current line) 100 times:

100dd

delete from current line to 99 lines forward

d99j

delete 100 lines backwards from (including) the current one

d99k

delete lines in a specific range by line number

:1,100d

delete lines in a range beginning with the current line

:.,.+99d

d #starts the delete operation.

dd# delte a whole line

dw # delete from the cursor up to the next word

de# to the end of the current word, including the last character

d0 #delete to the beginning of a line.

d$ # delete to the end of a line.

dgg # delete to the beginning of the file.

dG will delete to the end of the file.

u will undo the last operation.

Ctrl-r will redo the last undo.

d2w: delete 2 words

The format for a is:

              operator  [number]  motion

    where:

      operator - is what to do, such as  d  for delete

      [number] - is an optional count to repeat the motion

      motion  - moves over the text to operate on, such as  w (word),

  $ (to the end of line), etc.

From https://www.linux.com/learn/vim-101-beginners-guide-vim

/solve/set> courant-number

Solve/set/courant-number

Searching and Replacing

> /bunny, #search for "bunny"

syntax:

/ followed by the text you want to search for

• /text search for text in the document, going forward.

• n move the cursor to the next instance of the text from the last search. This will wrap to the beginning of the document.

• N move the cursor to the previous instance of the text from the last search.

• ?text search for text in the document, going backwards.

• :%s/text/replacement text/g #search through the entire document for text and replace it with replacement text.

• :%s/text/replacement text/gc search through the entire document and confirm before replacing text.

S

Saving and quitting

:w #write the file to the existing filename

:w filename # write the file to a different filename

:q # quit

:q! # leave without saving changes

set font size

gui

Edit \Select Font.

Command

:set guifont=*

PlugIn

The put command

p# put previously deleted text after the cursor

The replace command

rx# replace the character at the cursor with x

The change operator

ce# change until the end of a word

c$# change to the end of a line

The change operator is used with the same motions as delete

c    [number]  motion

The motions are the same, such as  w (word) and  $ (end of line).

Cursor location and file status

Ctrl+ g # show your location in the file and file status

G#move to the bottom of the file

Gg# move to the start of the file

50+G# move to  line 50

Search command

/error # search "error"

To search for the same phrase again, simply type  n .

To search for the same phrase in the opposite direction, type  N .

To search for a phrase in the backward direction, use  ?  instead of  / .

To go back to where you came from press  CTRL-O  (Keep Ctrl down while

    pressing the letter o).  Repeat to go back further.  CTRL-I goes forward.

Matching parentheses search

Type  %  to find a matching ),], or }

NOTE: This is very useful in debugging a program with unmatched parentheses!

The substitute command

:s/thee/the # only substitude  the first 'thee'  in the line

:s/old/new/g # substitude 'new' for 'old' in the line

:#,#s/old/new/g 

#where #,# are the line numbers of the range f lines where the substitution is to be done.

  :%s/old/new/g     

#to change every occurrence in the whole file.

:%s/old/new/gc   

#to find every occurrence in the whole file, with a prompt whether to substitute or not.

Lesson 5 how to execute an external command

:! + 'external command' #execute an external command

Retrieving and merging files

:r filename # to insert the contents of a file

Lessoin 6.1 the open command

o# open a line below the cursor and place you in insert mode

O# open a line above the cursor

a# insert text AFTER the cursor

A# insert text after the end of the line

e# comand moves to the end of a word

y# copy

p# paste

The append command

A

Copy and paste text

y# copy

p# paste

v# visual mode

:help user-manual # read the user manual

The vimrc file

:edit $MYVIMRC

Create a vimrc startup script to keep your preferred settings.

You probably got tired of typing commands that you use very often.  To start

Vim with all your favorite option settings and mappings, you write them in

what is called the vimrc file.  Vim executes the commands in this file when it

starts up.

Created: 2018-09-20 Thu 11:01

Validate

你可能感兴趣的:(vim)