【MIT Missing Semester L3】熟练掌握Vim操作


lecture
当程序员显然很大一部分时间都在写代码,熟练掌握editor工具对提升效率帮助挺大的
Tips太长不看版:可以直接 vimtutor去跟着教程实践一遍

如何掌握新的editor

  • 看教程
  • 即使最开始有点慢也要坚持用这个editor进行各种编辑
  • 查找改进用法,如果有可能有更好用的方式,那很可能真的有
    一般1-2小时就可以学会基本用法,20小时后就和之前的editor一样熟练了,之后就可以显示出新editor的强大功能来节省时间了
    会有新的有意思的编辑器出现,学的越多越快!
    可以看Stack Overflow来看当前比价流行的editor

Vim特点

由于编程中很多时间是在读、来回移动和少量的编辑,而不是大段大段连续的写,因而vim分为不同的模式,插入文本和操作文本是不一样的模式。
vim不用鼠标,因为鼠标太慢了,也不用上下左右箭头,因为需要比较大的移动,因此熟练使用vim的话就会非常快

vim model

  • normal 浏览文件,做一些编辑处理
  • insert 插入文本
  • replace 替换文本
  • visual(plain line block) 选择文本段
  • command line 运行命令
    同样的key在不同的模式下有不同的作用,一般在左下角显示model,通常情况下大部分是在Normal(initial/default)和Insert下
    所有模式按ESC可以回到Normal模式下

在这里插入图片描述
因为会频繁的涌到ESC,所以可以把大写锁定key换成esc macOS solutionwindows solution

基本用法

插入文本:

i进入insert mode

buffers,tabs,windows

vim可以打开很多文件,称为buffers,一个vim session有多个tabs,每个tabs有多个windows,每个windows展示一个Buffer。不像网页浏览器那样tbuffer和window之间是1对1的关系,vim中windows只负责展示数据。
一个Buffer可以在多个window中打开,一个 Window 在同一时间内只能展示一个 Buffer,一个 Buffer 可以同时被多个 Window 展示。这就意味着可以同时打开一个文件的不同部分。一个 Tab 上可以有多个 Window,不同 Tab 之间的 Window 互不影响。

官方解释这三者是:

A buffer is the in-memory text of a file.
A window is a viewport on a buffer.
A tab page is a collection of windows.

可以看下blog
打开多个window的时候,:q只会退出顶部的那个window, :qa全关

command-line

在normal node下按冒号可以进入命令模式。

  • :q quit (close window) 关闭top的tab(:qa会关掉各个打开的windows)
  • :w save (“write”) 保存
  • :wq save and quit 保存并关闭
  • :e {name of file} open file for editing 打开文件
  • :ls show open buffers 展示打开的文件
  • :sp拆成两个视图
  • :help {topic} 后面跟特定的key或者命令来查看具体的帮助
    • :help :w opens help for the :w command
    • :help w opens help for the w movement
    • 按F1也可以直接打开帮助界面,:q离开

移动选择等操作

Vim接口本身就可以看作是编程语言,按键本身就是命令,还可以组合。当这些变成肌肉记忆的时候就会格外的快

移动

在normal mode里

  • Basic movement: hjkl (left, down, up, right)左下上右移动(4j下移4次)
  • Words: w (next word移动到下一个单词), b (beginning of word移动到单词开头), e (end of word移动到单词末尾)
  • Lines按行移动: 0 (beginning of line), ^ (first non-blank character), $ (end of line)
  • Screen按屏移动: H (top of screen), M (middle of screen), L (bottom of screen)
  • Scroll滚动: Ctrl-u (up), Ctrl-d (down)
  • File: gg (beginning of file), G (end of file)
  • Ctrl+g会显示当前在第几行; 数字+G会回到第n行
  • Line numbers: :{number}<CR> or {number}G 比如3G (line {number})
  • Misc: % (corresponding item) 按%可以让光标跳到对应的当前括号对应的括号上{([
  • Find: f{character}, t{character}, F{character}, T{character}
    find/to forward/backward {character} on the current line
    , / ; for navigating matches 比如fo向下找到第一个字母o
  • Search: /{regex}, n /N for navigating matches /
    • 比如/range回车,会到range第一个字母处,按n到下一个匹配位置 ,N反向
    • ?代替/的话可以反向查找
  • .可以重复之前的编辑命令,比如第一个位置添加“end=’’”后,jj.会向下两行之后也加上“end="”。这样就不用重复打字了
  • Ctrl+o回到之前的位置,Ctrl+i向前移动
    在这里插入图片描述

选择

在normal mode下:
Visual: v
Visual Line: V
Visual Block: Ctrl-v
进入选择模式后可以移动进行选择
选择完了可以y进行复制,又回到normal mode下

edit

所有需要用到鼠标的地方都可以用组合key来进行

  • i插入,a光标后面append,A行末尾追加插入
  • o O在这行的下面或者上面插入
  • d{modtion}进行各种删除 dw删除单词直到下一个单词开始,de删到这个单词结束, d$删到这行结束,d0删到这行开头,dd删除整行,d命令总是需要和移动动作结合在一起用, dd完了在p可以直接复制刚刚删除的内容
  • c{motion}修改,相当于d{motion}+i,比如ce之后就可以直接输入修改后的单词,基本用法和d一致
  • x删除字符
  • s替换,相当于xi
  • r 替换,到想要替换的字母前按r然后按想要替换的那个目标字母,可以替换掉当前光标后的字母;大写R可以替换多个字符,按ECS退出替换
  • 在visual mode下,移动来选择,d删除,c修改
  • u undo , Ctrl+r redo,U撤回整行的操作
  • y copy 也是需要结合movement,比如yw复制当前行,yy复制当前行
  • p paste or 复制刚刚删除的内容
    还有非常多

计数

给定操作执行多次,数字+动作,比如

  • 3w move 3 words forward
  • 5j move 5 lines down
  • 7dw delete 7 words,7dw也可以,2dd可以连续删除两行

modifiers

可以用来改变操作的意思
比如i意味着内部,a意味着周围

  • ci( change the contents inside the current pair of parentheses改变小括号内的内容
  • ci[ change the contents inside the current pair of square brackets改变中括号内的内容
  • da' delete a single-quoted string, including the surrounding single quotes 删除单引号之间的内容,包括单引号
  • 这些都是可以组合改变的,像编程一样

配置vim

通过编辑~/.vimrc来配置
可以参考课程给出的配置,粘贴到文件里:link
可以多看看别人在github上分享出来的配置,明白到底配置了什么东西

创建文件夹~/.vim/pack/vendor/start/然后通过git clone来下载plugins可以拓展vim
推荐的几个vim插件:

甚至Web也可以配置成vim的一些用法,比如chrome的Vimium,也可以在jupyter notebook中配置

高级用法

搜索和替换

  • :s 替换 documentation

  • %s/foo/bar/g -->:%s/old/new/g

    • replace foo with bar globally in file
    • :%s/foo/bar/gc每次替换前都问一下
    • :s/foo/bar/g只换当前行,不加/g只换出现的第一个
  • 可以加行号:#,#s/old/new/g

  • %s/\[.*\](\(.*\))/\1/g

    • replace named Markdown links with plain URLs

多窗口

  • :sp / :vsp to split windows
  • 一个Buffer可以有多个views
  • Ctrl w Ctrl w在window之间跳

  • q{character} to start recording a macro in register {character}
  • q to stop recording
  • @{character} replays the macro
  • Macro execution stops on error
  • {number}@{character} executes a macro {number} times
  • Macros can be recursive
  • Vim commands / macros
    • Gdd, ggdd delete first and last lines
    • Macro to format a single element (register e)
      • Go to line with <name>
      • qe^r"f>s": "<ESC>f<C"<ESC>q
    • Macro to format a person
      • Go to line with
      • qpS{j@eA,j@ejS},q
    • Macro to format a person and go to the next person
      • Go to line with
      • qq@pjq
    • Execute macro until end of file
      • 999@q
      • Manually remove last , and add [ and ] delimiters

其他

  • :!后可以加其他命令,比如:!ls回车,就像在shell中一样
  • :w xxx可以把当前文件另存为xxx文件
  • 部分保存,按v进入选择模式进行选择,然后:会看到在这里插入图片描述,接着在后面输入w xxx就可以部分保存到文件xxx中
  • 插入文件中的内容: :r FILENAME,还可以插入命令执行的结果,比如:r !ls
  • 配置搜索选项,一次搜索忽略大小写可以/something\c回车,设置一直忽略大小写可以:set ic,关闭则set noic,想要高亮历史搜索结果可以set hls is,关闭可以:nohlsearch,一边搜索一边高亮可以:set is。一般来说就是:set设置,:set no xxx关掉
  • 命令补全,Ctrl D+TAB

参考资料

  • vimtutor 装了vim之后可以直接在shell运行 vimtutor 查看官方教程
  • Vim Adventures is a game to learn Vim
  • Vim Tips Wiki
  • Vim Advent Calendar has various Vim tips
  • Vim Golf is code golf, but where the programming language is Vim’s UI
  • Vi/Vim Stack Exchange
  • Vim Screencasts
  • Practical Vim (book)

练习

1.完成vimtutor. Note: it looks best in a 80x24 (80 columns by 24 lines) terminal window. √
2.Download our basic vimrc and save it to ~/.vimrc. Read through the well-commented file (using Vim!), and observe how Vim looks and behaves slightly differently with the new config. √
3.Install and configure a plugin: ctrlp.vim.

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值