vim cook 中文版

618阅读 0评论2012-10-13 wuxiaobo_2009
分类:Python/Ruby

Vim cook 中文版

自己学习vim, 贡献一下,不知道对于后人是好,是坏。

Character twiddling  If you type fast your fingers can easily get ahead of your mind. Frequently people transpose characters. For example the word "the" comes out "teh".

To swap two characters, for example "e" with "h", put the cursor on the cursor on the "e" and type xp.

The "x" command deletes a character (the "e") and the "p" pastes it after the cursor (which is now placed over the "h".)

如果你的手的速度已经超越了你的大脑,说明你对一件事情达到了心理学中的高度自动化。人们在敲入英文字符时候经常会出现这样的错误: the -->  teh. 一般的解决方法: e -> h-> xp 三个命令组合。

Interactively replacing one word with another (n. method)

用其他的字符串交互式替换现在的word,这里有种方法

Suppose you want to replace every occurrence of the word "idiot" with the word "manager". But you want the chance to review each change before you do it. 

Here's what you do:

1.

1G

Go to the top of the document

2.

/idiot

Find the first occurrence of the word "idiot"

3.

cwmanager

Change the word (cw) to manager.

4.

n

Repeat the last search (find the next idiot.)

5.

.

Repeat the last edit (change one word to manager)
(If you do not want to change the word, skip this step.)

Repeat steps 4 and 5 until you have replaced all occurred.

如果一段文字中有idiot ,我们要用manager 来进行替换,如何做呢:

1. 1G /idiot :  从首行起查找idiot 这个单词

2. cwmanager: cw change word)命令实现替换manger ,之后一定要esc 跳出插入模式

3. 寻找下一个idiot ,之后用. (重复命令,可以实现替换),这里注意 实际替代了 cwmanager esc 

Interactively replacing one word with another (command line method)

命令行模式实现上边的替换

Suppose you want to replace every occurrence of the word "idiot" with the word "manager". But you want the chance to review each change before you do it. 

Execute the command:

:%s/\/manager/gc

This command will make the change and pause after each change to give you a chance to confirm it. You can enter "y" to accept the change or "n" to not accept it.

实际上就是上边的一条命令:用实现全局, s/// 替换, \<\> 实现了对于一个单词的隔离,gc 是一行上所有出现的情况,当然是确认的意思(y:确认,n:不同意)

Replacing one word with another using one command

如果不想确认,直接替换,那么下边一条命令搞定:

:%s/\/manager/g

Moving Text (Vi style commands

移动一块内容(vi 风格的命令)

1. 把光标移动到你要移动的一个block 的首行,之后用ma(标记首行为a

2. 移动到移动的block 的末行,之后按下:d'a ,这里你就把这块内容保存在buffer 中了

3. 现在移动光标到你想插入的位置一下就ok

Moving Text (Visual Method)

移动一块内容(vi Visual模式)

通过vi visual 模式来移动一块内容,这样更加直观。

首先你就在选中的首行,之后进入visual 模式,nG->$ 选中一块区域,之后就,保存到buffer 中,以后一样找到合适位置p一下就ok

Copying a block of text from one file to another (Vi Style)

通过刚才上边的两个例子,我们完全可以引申实现:从一个文件中copy 一块内容到另外一个文件中。

1. 把上边的变成yok

2. split 一个文件,之后选好位置ok

当然vim Visual 模式和和上边的大同小异。

Sorting a section (Vi Style)

排序(vi风格)

version.o

pch.o

getopt.o

util.o

getopt1.o

inp.o

patch.o

backupfile.o

对于上边的这段文字实现部分排序:

还是一个原则,先选中后操作, 比如从pch..o  到 patch.o ,你就可以在pch.o 敲入ma 标记,之后patch.o 按下:!'asort ,表示从a到现在的这部分进行排序。

Visual 模式:

差不多一样,进入首行,之后进入visual 模式,移动到待排序的末行,之后进入命令模式: !sort 就可以实现排序。

------------------休息一下,very easy I know you got it 

起床了,开始搞起!

For example, the following is correct:

        prog: prog.c

                cc -g -o prog prog.c

The following is not:

        prog: prog.c

                cc -g -o prog prog.c

上边的内容和下边的什么区别,上边是tab ,下边是空格。 你能看出来,那说明你是神!有时候用空格的文字本来就不符合文件的格式,可能以后要报错。

Drawing comment boxes

如何画出下边的东东:

/*******************************************************      

* Program -- Solve it -- Solves the worlds problems.  *      

* All of them.  At once.  This will be a great      *      

* program when I finish it.                      *      *******************************************************/

你可以在你的.exrc 中放入:

:set autoindent

:set autowrite

:ab #d #define

:ab #i #include

:ab #b /************************************************

:ab #e ************************************************/

:ab #l /*----------------------------------------------*/

:set sw=4 -----> 这个主要定义的《   》 这个缩进符

Removing carriage returns from MS-DOS file

:1,$s/{Ctrl+V}{Ctrl+M}//{Enter}

Trimming the blanks off an end of line

   :1,$s/[ ]*$//

How to edit all the files containing a given word

    $ vim `fgrep -l indentation_level *.c`

可以用fgrep 快速找到还有 indentation_level 的当前目录下的程序的文件名

How to edit all the files containing a given word using the built-in grep

在一个vim 中找出多个文件中还有相同的要修改的东西。如:test.c test.sh 中都有我们要修改的文字main

打开vim ,输入:grep main test.c test.sh  。之后就能看到第一次出现的main 的地方,你可以修改,:cn 命令可以找到第二次出现的地方,接着修改。

上一篇:vim 纵向编辑 ----转
下一篇:手把手教你把Vim改装成一个IDE编程环境(图文) --转载修正版