vim学习笔记

在网上广为流传着一句话:VIM是编辑器之神,EMACS是神的编辑器.可见VIM的地位之重.我用vim也有差不多两年了,至今仍停留在新手阶段,只会一些vim的基本操作.但是就算只会这些基本操作,我的编码效率也得到显著地提高,所以我想将vim推荐给大家.

vim的设计哲学

多种模式

我觉得vim的一个很突出的特点就是它拥有多种模式:命令模式,插入模式,可视模式.在每一个模式下,触发相同的按键,它的功能是不同的.这样就使得我们好像凭空多出了几副键盘,原本一些按键需要将手移动到很远的地方才能按到,如今却不用把手移出工作区,只用切换模式就可以了.

极其合理的键位安排

vim拥有着很完美的键位分布,它便于记忆而有能够显著地提高输入效率.

强大的插件支持

单独一个vim可能很单薄,但是vim提供了一个插件接口,我们可以通过插件实现很多功能.


待施工…

02.1 Running Vim for the First Time

02.2 Inserting text

  • set showmode (option)

    display the mode at thr bottom of the window

02.3 Moving around

02.4 Deleting characters

  • x – delete a character

  • dd – delete a line

  • J – delete a line break

02.5 Undo and Redo

  • u – undo

  • CTRL-R – redo

02.6 Other editing commands

  • a – append

  • o – open a new line

02.5 Getting out

  • q! – quit and throw things away

  • e! – reloads the original version of the file

  • wq – write the file and quit

02.7 Finding help

usr_03.txt moving around

03.1 word movement

  • w – move the cursor forward one word
  • b – move backward to the start of the previous word
  • e – moves to the next end of a word
  • ge – moves to the previous end of a word

03.2 moving to the start or end of a line

  • $ – moves the cursor to the end of a line
  • ^ – moves to the first non-blank character of the line
  • 0(zero) – moves to the very first non-blank character of the line

03.3 moving to a character

  • fx – searches forward in the line for the single character x.
    hint:“f” stands for “Find”
    the “F” command searches to the left
  • tx – works like the “fx” command,except it stops one character before the searched character.
    hint:“t” stands for “To”
    the backward version of the command is “Tx”

these four command can be repeated with “;”,"," repeats in the other direction

03.4 matching a parenthesis

  • % – moves to the matching paren

03.5 moving to a specific line

  • G – positions you at the end of the file
  • gg – positions you at the start of the file
  • number G – positions you to the given line number

03.6 telling where you are

  • set number option

    – display a line number in front of every line

  • set ruler option

    – display the cursor position in the lower right corner of the Vim window

03.7 scrolling around

  • CTRL-u

    – scrolls down half a screen of text

  • CTRL-d

    – scrolls the text up half a screen

  • CTRL-f

    – scroll forward by a whole screen

  • CTRL-b

    – scroll backward by a whole screen

  • zz

    – put the cursor line at the middle
    more commands: zt zb

03.8 simple searches

  • /string

    – search for a string
    to find the next occurrence of the same string use the “n” command,the “N” command repeats the last search the opposite direction

  • ?

    – works like “/” but searches backwards

  • set ignorecase option

    – don’t care about upper or lowercase in a word

you can use UP DOWN to use the words you searched before

  • set hlsearch option

    – highlight all matches

  • set incsearch option

    – display the match for the string while you are still typing it

  • set nowrapscan option

    – stops the search at the end of the file

03.9 simple search patterns

we will take about it in usr_027.txt

03.10 using marks

  • CTRL-o

    – jumps to older positions
    hint:o for older

  • CTRL-i

    – jumps back to newer positions

  • ma

    – marks the place under the cursor as mark a

  • `a
    

    – moves you to the beginning of the line containing the mark

usr_04.txt

04.1 operators and motions

operator-motion.You first type an operator command.For example,“d” is the delete operator.Then you type a motion command like “4l” or “w”.This way you can operate on any text you can move over.

04.2 changing text

  • c – It acts just like the “d” operator,except it leaves you in Insert mode.

  • s – change one character

  • S – change a whole line

  • r – waits for you to type a character,and will replace the character under the cursor with it

04.3 repeating a change

  • . – repeats the last change

04.4 visual mode

  • v – enter the visual mode

  • V – enter the visual mode and select the whole line

  • CTRL-v – start bolockwise mode

04.5 moving text

  • when you delete something with the “d”,“x”,or another command,the text is saved.you can paste is back by using the p command.

  • xp – swapping two characters

04.6 copying text

  • y

    1. yw – yank a word
      – copies text into a register
      e.g.
    2. yw – yank a word
    3. yy – yank a whole line
  • p
    – put the text in the next line of the cursor

04.7 using the clipboard

  • 1.n+dd 2.p

usr_05.txt Set your settings

05.1 the vimrc file

05.2 the example vimrc file explained

  • set nocompatible – setting the ‘compatible’ option off,not completely Vi compatible

  • set backspace=indent,eol,start – specifies where in Insert mode the backspace is allowed to delete the character in front of the cursor.
    indent – the white space at the start of the line
    eol – a line break
    start – the character before where insert mode started

  • set autoindent – makes Vim use the indent of the previous line for a newly created line.

  • set history=50 – keep 50 commands and 50 search patterns in the history.

  • set ruelr – always display the current cursor position in the lower right corner of the Vim window

  • set showcmd – display an incomplete command in the lower right corner of the Vim window

05.3 simple mappings

for example:map i{ea}
it tell Vim that F5 key does the job behind it.

05.4 adding a package

05.5 adding a plugin

05.6 adding a help file

05.7 the option window

type options command to the option window

05.8 often used options

  • set cmdheight=3 – set the number of lines used for messages

usr_07 Editing more than one file

07.1 edit another file

  • edit name.type – vim will close the current file and open the new one

07.2 a list of files

  • vim one.c two.c three.c – this command starts vim and tells it that you will be editing three files.vim displays just the first file.

  • next or 2next – to edit the next file you use this command

  • previous – to go back one file

  • last – to move to the very last file in the list

  • first – to move back to the first one again

  • args – show the list of file.this is short for “arguments”.the output might look like this:one.c [two.c] three.c

  • args five.c six.c seven.h – use this command to edit three other files

07.3 jumping from file to file

07.4 backup files

  • set backup – produces a backup file for the file you edit

  • set backupext=.bak – if you do not like the fact that the backup files end with ~,you can change the extension

07.5 copy text between files

use y and p commands

when you want to copy serveral pieces of text from one file to another,having to switch between the files and writing the target file takes a lot of time.to avoid this,copy each piece of text to its own register.for example: fyas

when collecting lines of text into one file,you can use this command:
:write >> logfile
this will write the text of the current file to the end of “logfile”
to append a few lines,select them in visual mode befor typing:write

07.6 viewing a file

  • vim -R file – to start vim in readonly mode

  • vim -M file – every attempt to change the text will fail

07.7 changing the file name

  • file move.c – change the name of the file you are editing,but don’t write to file

  • saveas move.c – editting a new file by using an existing file that contains most of what you need and changes the file’s name

usr_08.txt splitting windows

08.1 split a window

  • split – splits the screen into two windows and leaves the cursor in the top one

  • close – to close the window

  • only – close all windows,except for the current one

08.2 split a window on another file

  • split name.type – opens a second window and starts editing the given file
  • new – open a window on a new,empty file

08.3 window size

the :split command can take a number argument.If specified,this will be the height of the new window.for example:
3split alpha.c

  • CTRL-w + – increase the size of a window

  • CTRL-W - – decrease the size of a window

  • {height}CTRL-W _ – set the window height to a specified number of lines

08.4 vertical splits

  • vsplit or vsplit name.type – to make the window appear at the left side

  • vnew – like new

  • moving between window

CTRL-w h – move to the window on the left
CTRL-w j – move to the window below
CTRL-w k – move to the window above
CTRL-w l – move to the window on the right
CTRL-w t – move to the TOP window
CTRL-w b – move to the bottom window

08.5 moving windows

CTRL-w K – moving window to the top
CTRL-w H – moving window to the far left
CTRL-w J – moving window to the bottom
CTRL-w L – moving window to the far right

08.6 commands for all windows

  • qall wall wqall – some commands

  • vim -o one.txt two.txt three.txt – open a window for each file,the -O argument is used to get vertically split windows

  • when Vim is already running the :all command opens a window for each file in the argument list.vertical all does it with vertical splits

08.7 viewing differences with vimdiff

08.8 various

08.9 tab pages

  • tabedit thatfile – this will edit the file “thatfile” in a window that occupies the whole Vim window

  • gt – switch between tab pages,it means:Goto tab

  • tab split – this makes a new page with one window that is editing the same buffer as window we were in

usr_10.txt making big changes

10.1 record and playback commands

  1. the q{register} command starts recording keystroks into the register name {register}. The register name must be between a and z.

  2. type your commands

  3. to finish recording, press q(without any extra character).

you can now execute the macro by typing the command @{register}

writing to an uppercase register name means to append to the register with same letter,bue lowercase.

10.2 substitutions

the “:substitute”(abbreviated version"") command enables you to perform string replacesments on a whole range of lines.the general form of this command is as follows:
:[range]substitute/from/to/[flags]
this command changes the “from” string to the “to” string in the lines specified with [range].

  • some examples about [range]

    1. 1,5s/this/that/g
    2. 54s/President/Fool/
    3. .,$s/yes/no/
    4. .+3,$-5s/…/…/
    5. you can also use marks
  • some flags you can use

    1. g – global,it means vim will replace all the words in the range
    2. c – ask you some information when do the substitution

10.3 command ranges

the s command, and many other : commands, can be applied to a selection of lines. this is called a range.

  1. 1,5 – line 1 to 5

  2. 54 - address one specific line

  3. . – the current line

  4. $ – the last line

  5. % – all the lines

10.4 the global command

the global command is one of the more powerful features of Vim. It allows you to find a match for a pattern and execute a command there. The general form is:
[range]global/{pattern}/{command}

10.5 visual block mode

Inserting text

  1. you start by pressing CTRL-V to enter visual block mode. Now you move the cursor to define your block. Next you type I to enter Insert mode, followed by the text to insert. As you type, the text appears on the first line only. After you press ESC to end the insert, the text will magically be inserted in the rest of the lines contained in the visual selection.tips:if the block spans short lines that do not extend into the block, the text is not inserted in that line.

  2. the A command works the same way, except that is appends after the right side of the block. And it does insert text in a short line

  3. the Visual block c command deletes the block and then throws you into insert mode to enable you to type in a string. The string will be inserted in each lien in the block.tips:just like with I the short line is not changed

  4. other commands that change the characters in the block:
    ~ swap case (a -> A and A -> a)
    U makt uppercase (a -> A and A -> A)
    u make lowercase (a -> a and A -> a)

  5. to fill the whole block with one character, use the r command.

  6. the command > shifts the selected text to the right one shift amount
    the shift amount is specified with the shiftwidth option. to change is to use 4 spaces:
    set shiftwidth=4

  7. the command < command removes one shift amount of whitespace at the left edge of the block.

  8. the J command joins all selected lines together into one line.

10.6 reading and writing part of a file

when you are writing an e-mail message, you may want to include another file. This can be done with the :read {filename} command.

the read command accepts a range. the file will be put below the last line number of this range. thus $r patch appends the file “patch” at the end of the file. if you want to read the file above, this can be done with the line number zero.

to write a range of lines to a file, the write command can be used. without a range it writes the whole file. with a range only the specified lines are written

  • write >>collection – the >> tells vim the “collection” file is not to be written as a new file, but the line must be appended at the end.

10.7 formatting text

10.8 changing case

  1. gu+motion

  2. gU+motion

  3. g~+motion

  4. guu gUU g~~ – make the operator works on lines

10.9 using an external program

the command !{motion}{program} takes a block of text and filters it through an external program.

usr_12.txt clever tricks

12.1 replace a word

  1. use the \< item to match the start of a word. use \> to match the end of a word.

  2. replace in several files – use tag

12.2 change “last,first” to “first,last”

12.3 sort a list

12.4 reverse line order

12.5 count words

  • g CTRL-g – count words

  • with visual mode, select the text you want to count words in. then type g CTRL-g.

12.6 find a man page

12.7 trim blanks

12.8 find where a word is used# Vim note

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