V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
raquant
V2EX  ›  Vim

vim 干货

  •  
  •   raquant · 2017-03-19 19:17:25 +08:00 · 6633 次点击
    这是一个创建于 2592 天前的主题,其中的信息可能已经有所发展或是发生改变。

    基础篇

    • :e filename Open filename for edition
    • :w Save file
    • :q Exit Vim
    • :q! Quit without saving
    • :x Write file (if changes has been made) and exit
    • :sav filename Saves file as filename
    • . Repeats the last change made in normal mode
    • 5. Repeats 5 times the last change made in normal mode
        提醒你一下,
    
        :e filename 可以打开多个文件,切换方法为 ctrl+shift+^
    
        再提醒你一下,
    
        :e .   可以看看当前目录有哪些东西
    

    在文件中移动

    • k or Up Arrow move the cursor up one line
    • j or Down Arrow move the cursor down one line
    • e move the cursor to the end of the word
    • b move the cursor to the begining of the word
    • 0 move the cursor to the begining of the line
    • G move the cursor to the end of the line
    • gg move the cursor to the begining of the file
    • L move the cursor to the end of the file
    • :59 move cursor to line 59. Replace 59 by the desired line number.
    • 20| move cursor to column 20.
    • % Move cursor to matching parenthesis
    • [[ Jump to function start
    • [{ Jump to block start
        提醒你一下,
    
        ctrl+b,ctrl+e,ctrl+f 都可以试一试哦
    

    剪切、复制和粘贴

    • y Copy the selected text to clipboard
    • p Paste clipboard contents
    • dd Cut current line
    • yy Copy current line
    • y$ Copy to end of line
    • D Cut to end of line
        提醒你一下,
    
        还可以 3yy,复制 3 行
    
        再提醒一下,使用标记可以复制一个范围的,比如挪到某行 ma  另外一行 mb
    
        然后:'a,'by  可复制 a 到 b 直接的内容
    

    搜索

    • /word Search word from top to bottom
    • ?word Search word from bottom to top
      • Search the word under cursor
    • /\cstring Search STRING or string, case insensitive
    • /jo[ha]n Search john or joan
    • /< the Search the, theatre or then
    • /the> Search the or breathe
    • /< the> Search the
    • /< ¦.> Search all words of 4 letters
    • // Search fred but not alfred or frederick
    • /fred|joe Search fred or joe
    • /<\d\d\d\d> Search exactly 4 digits
    • /^\n{3} Find 3 empty lines
    • :bufdo /searchstr/ Search in all open files
    • bufdo %s/something/somethingelse/g Search something in all the open buffers and replace it with somethingelse
        没看明白?
        /\<the\> 只选 the 这个单词
    

    替换

    • :%s/old/new/g Replace all occurences of old by new in file
    • :%s/onward/forward/gi Replace onward by forward, case unsensitive
    • :%s/old/new/gc Replace all occurences with confirmation
    • :2,35s/old/new/g Replace all occurences between lines 2 and 35
    • :5,$s/old/new/g Replace all occurences from line 5 to EOF
    • :%s/^/hello/g Replace the begining of each line by hello
    • :%s/$/Harry/g Replace the end of each line by HarryI
    • :%s/onward/forward/gi Replace onward by forward, case unsensitive
    • :%s/ *$//g Delete all white spaces
    • :g/string/d Delete all lines containing string
    • :v/string/d Delete all lines containing which didn ’ t contain string
    • :s/Bill/Steve/ Replace the first occurence of Bill by Steve in current line
    • :s/Bill/Steve/g Replace Bill by Steve in current line
    • :%s/Bill/Steve/g Replace Bill by Steve in all the file
    • :%s/^M//g Delete DOS carriage returns (^M)
    • :%s/\r/\r/g Transform DOS carriage returns in returns
        可能有的人找这种删除命令很久了
        :g/string/d
    
    43 条回复    2017-03-20 20:05:40 +08:00
    wangxn
        1
    wangxn  
       2017-03-19 19:30:04 +08:00 via Android
    都是最基本的啊
    sagaxu
        2
    sagaxu  
       2017-03-19 19:59:05 +08:00
    干货在哪里?
    raquant
        3
    raquant  
    OP
       2017-03-19 20:00:56 +08:00
    @wangxn 哈哈,先上基础的
    raquant
        4
    raquant  
    OP
       2017-03-19 20:01:17 +08:00
    @sagaxu 干货随后来,不是每个人都像你这么厉害的
    zhangchioulin
        5
    zhangchioulin  
       2017-03-19 20:04:15 +08:00
    收藏观望
    LittleKey
        6
    LittleKey  
       2017-03-19 20:04:17 +08:00 via Android
    好像有错的。。
    Remember
        7
    Remember  
       2017-03-19 20:16:48 +08:00
    这是给你自己自学用的吧,不用发论坛了
    vcfvct
        8
    vcfvct  
       2017-03-19 20:21:05 +08:00 via Android
    @Remember 发出来也不错,大家醒查一下自己有什么没有掌握的,查漏补缺嘛
    hasdream
        9
    hasdream  
       2017-03-19 20:51:18 +08:00
    多 tab 及切换
    :tabnew
    :e filename 正常模式下 gt 进行切换

    多行删除前几个字符或者追加几个字符
    正常模式下 ctrl + v 选中多行开头几个字符 delete 删除

    多行最前面追加相同的字符
    正常模式下 ctrl + v 选中多行最开头 Shift +i 写入字符 ESC 完成选择位置追加字符

    执行系统命令
    :!ls 命令前面加!
    aristotll
        10
    aristotll  
       2017-03-19 21:08:44 +08:00
    比较干...
    littleylv
        11
    littleylv  
       2017-03-19 21:14:50 +08:00
    会 vim 的人这些基本都会了吧。。。
    greyterry
        12
    greyterry  
       2017-03-19 21:33:39 +08:00
    。。。。。这种贴应该支持还是
    freshmanc
        13
    freshmanc  
       2017-03-19 21:37:46 +08:00 via Android
    感觉做完 tutorial 这些也…
    rashawn
        14
    rashawn  
       2017-03-19 21:39:13 +08:00 via iPhone
    有的功能基本不会用到 被效率更高的插件代替了
    colorwin
        15
    colorwin  
       2017-03-19 21:41:36 +08:00
    刚入门 vim 的表示还是有帮助的
    shidenggui
        16
    shidenggui  
       2017-03-19 21:53:51 +08:00
    学到了 D 和 search 的几个用法, 挺有用的
    myself659
        17
    myself659  
       2017-03-19 22:00:19 +08:00
    还不余我上传一张图
    mingyun
        19
    mingyun  
       2017-03-19 22:56:48 +08:00
    网上有个 vim 小抄 挺全的
    raquant
        20
    raquant  
    OP
       2017-03-19 23:24:29 +08:00
    @myself659 贵在整理啊
    mintist
        21
    mintist  
       2017-03-19 23:57:12 +08:00
    不错,学习了,谢谢楼主整理,又过了一遍,,,
    mintist
        22
    mintist  
       2017-03-19 23:57:59 +08:00
    其实,不太明白一些缩写的含义,如 y 为什么是 Copy the selected text to clipboard

    p 是 paste 还好理解点
    binux
        23
    binux  
       2017-03-19 23:59:43 +08:00
    LZ ,在菜市买的干货虽然也叫干货,但是和我们平时说的干货还是不一样的。
    raquant
        24
    raquant  
    OP
       2017-03-20 00:01:21 +08:00
    @binux 谢谢,真好,让我了解到 vim 版的强大,逼迫自己提高要求了
    raquant
        25
    raquant  
    OP
       2017-03-20 00:02:07 +08:00
    @mintist y 可能是 yank 的缩写啊
    will0404
        26
    will0404  
       2017-03-20 01:08:05 +08:00
    纠正一下。
    G 是移动到文件最底部。
    L 是移动到屏幕最底部。
    jigloo
        27
    jigloo  
       2017-03-20 01:16:08 +08:00
    大兄弟,给你看一个 14 年的文章, https://dougblack.io/words/a-good-vimrc.html 也比你的干货要干啊。
    xiaket
        28
    xiaket  
       2017-03-20 06:35:53 +08:00
    v2 不欢迎全文转载的.
    defunct9
        29
    defunct9  
       2017-03-20 07:08:35 +08:00 via iPhone
    湿答答的
    allotory
        30
    allotory  
       2017-03-20 08:47:49 +08:00 via Android
    其实还好吧,大家不要这样打击人。。。
    97dog
        31
    97dog  
       2017-03-20 08:49:34 +08:00
    很基本呀
    gejigeji
        32
    gejigeji  
       2017-03-20 09:27:23 +08:00
    骗点击的感觉
    irenicus
        33
    irenicus  
       2017-03-20 09:34:42 +08:00
    用于替代记事本,写字板, gedit , kwrite 的话,这些够用了
    半导体行业,我大部分同事也就止步于这里的 80%
    我下功夫学 perl 和 vim ,属于另类。。。
    BFDZ
        34
    BFDZ  
       2017-03-20 09:41:18 +08:00
    这货不干,整理的也不好,看起来没条理。这篇才是真正的干货 http://coolshell.cn/articles/5426.html
    Antidictator
        35
    Antidictator  
       2017-03-20 09:43:48 +08:00
    @BFDZ 额, 10 分钟前我看过,现在又看到你发了。。
    BFDZ
        36
    BFDZ  
       2017-03-20 10:04:47 +08:00
    @Antidictator #35 我的 vim 技能基本上是看这篇教程入门和进阶的,所以碰到说 vim 我就会推这篇教程
    chinese_zmm
        37
    chinese_zmm  
       2017-03-20 10:13:45 +08:00 via Android
    vim cheatsheet 应该很多
    lococo
        38
    lococo  
       2017-03-20 10:30:07 +08:00
    潮到出水
    psklf
        39
    psklf  
       2017-03-20 10:44:47 +08:00
    哈哈 菜市场的干货
    andysheng
        40
    andysheng  
       2017-03-20 10:48:00 +08:00
    不如 vimtutor
    Antidictator
        41
    Antidictator  
       2017-03-20 11:34:41 +08:00
    @BFDZ 哈哈哈,所以我说经典
    ROG
        42
    ROG  
       2017-03-20 11:47:33 +08:00
    mark 看干货
    liuenyan
        43
    liuenyan  
       2017-03-20 20:05:40 +08:00
    还是推荐看《 vim 使用技巧》这本书吧。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5314 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 39ms · UTC 08:41 · PVG 16:41 · LAX 01:41 · JFK 04:41
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.