Vim 的常用操作

Vim 的常用操作

一. vim中的查找替换

  • 修改当前行

    :s/str1/str2/
    

    修改当前行的第一个str1为str2

    :s/str1/str2/g
    

    修改当前行的所有str1为str2

  • 修改第n行到第m行

    :n,ms/str1/str2/
    

    修改第n行到第m行的第一个str1为str2

    :n,.s/str1/str2/
    

    修改第n行到当前行的第一个str1为str2

    :.,ms/str1/str2/
    

    修改当前行到第m行的第一个str1为str2

    :n,$s/str1/str2/
    

    修改第n行到最后一行的第一个str1为str2

ps: 如果是修改行的所有str1str2,后面加g即可

  • 替换每一行

    :%s/str1/str2/
    

    修改每一行的第一个str1为str2

    :1,$s/str1/str2/
    

    修改每一行的第一个str1为str2

ps: 如果是修改行的所有str1str2,后面加g即可

  • vim的替换命令的完整形式

    :[range]s/from/to/[flags]
    

ranges: 上面已经提到了
from: 可以用正则表达式
to: 要替换成的字符创
flags: 如下所示,这几个可以组合使用

无      :  只对指定范围内的第一个匹配项进行替换。
g       :  对指定范围内的所有匹配项进行替换。
c       :  在替换前请求用户确认。
e       :  忽略执行过程中的错误。

二. vim的常用快捷键

  • 跳转

1. 基本的上下左右
j: 上
k: 下
h: 左
l: 右

2. 翻一页
ctrl + f:向前翻一页(forward)
ctrl + b:向后翻一页(backward)

3. 翻半页
ctrl + u:向前翻半页(up)
ctrl + d:向后翻半页(down)

4. 翻一行
ctrl + e: 向下翻一行
ctrl + y: 向上翻一行

5. 跳转到函数和变量的定义处
[ + ctrl + i: 跳到定义处
[ + ctrl + d: 跳到#define处

6. 光标的跳转
ctrl + i: 往前跳
ctrl + o: 往后跳

7. 函数体内的跳转
[{: 跳到函数体首部
]}: 跳到函数体尾部
[[, ]], {{, }}, 调转函数体

8. 跳转单词
b: go to the [b]eginnig of this word
e: go to the [e]nd of this word
w: go to the start of the following word

  • 选中

1. ctrl + v的选中,通过上下左右命令的调整的选中

2. shift + v模式下的选中
vi( : 选中小括号中的内容,不包括小括号
va( : 选中小括号中的内容,包括小括号,下同
vi[
va[
vi{
va{
vi'
va'
vi"
va"

3. 删除行末空格

:%s/s+$//

原文地址:https://www.cnblogs.com/zk47/p/4209517.html