vim:你的不二选择

最好的方法就是在使用中学习,即 :help

vim 升级到 9 版本

  • yum list installed | grep -i vim 查看当前已安装的 vim 版本
    查看当前已安装的 vim 版本
  • sudo yum remove vim-enhanced vim-common vim-filesystem 移除老的安装
  • yum install gcc make ncurses ncurses-devel
  • github 下载、编译、安装

https://github.com/vim/vim/archive/refs/tags/v9.0.1940.tar.gz
make distclean # if you build Vim before
make
sudo make install
vim --version 即可看到版本为 9 了。

参考

Vim Cheat Sheet:基础经典;
vimdoc.sourceforge:非常完整;
Book - Practical Vim: Edit Text at the Speed of Thought 2nd Edition
www.vim.org(请翻墙)
Vim Tips Wiki @ vim.wikia.com

vim --help
  • vim -o2 - 上下两个窗口
  • vim -O2 - 左右两个窗口
  • vim 已经打开文件,切分窗口使用 press <Ctrl>+<w> then press <v> 则会在右侧开窗。
  • 左右切换(在 vim 中 h 左移,l 右移)
    切到左边:pressing <Ctrl>+<w> and then pressing <h>
    切到右边:pressing <Ctrl>+<w> and then pressing <l>
  • diff -E -Z -b -y --suppress-common-lines old new 左右窗口对比两个文件。如何安装 diff 见 这里
基本
  • 按 "i"或"I" 进入编辑模式,编辑后按 "esc" 退出编辑模式,然后按 ":" 进入命令行模式,":wq" 保存退出 vim.
  • R - 进入替换模式,和 i 模式相对应;
    Enter overtype (replace) mode, where you destructively retype everything until you press ESC;
  • r - 替换当前字符;
  • 使用 vim 打开文件,编辑了半天,要保存时发现 readonly 打开的,如何办?
    Using vim to force edit a file when you opened without permissions
    有 sudo 权限时:

    Issue: :w !sudo tee %. This will write the buffer to tee, a command that receives pipe information and can write to files. And as tee is run with sudo powers, tee can modify the file.`

定位
  • <num>G [enter] - Go To that line
  • : <num>[enter] - Go To that line
  • :set nu 或者 : set number 设置显示行号。
  • gg - go to the top,相当于 1 G [enter]
  • G - go the bottom,相当于 0 G [enter]
  • 0 - 行头
  • $ - 行尾
  • % - {} 花括号对的查找切换,找对应的花括号(匹配花括号);
  • <num>h, <num>l:左右移动光标 num 字符;
  • <num>j, <num>k:下上移动光标 num 行;

Undo and Redo

  • u:undo
  • Ctrl + r:redo

Copy, cut and paste

  • y - yank

  • yy - yank (copy) a line。

  • <num>yy 则 yank <num> lines,如 4yy。

  • y0y$yG - yank to the begin of line, to the end of line,to the end of file.

  • 进入 v 模式后,选择,然后 y
    对于不太好确定边界的复制,v模式非常方便。

  • p - put (paste) the clipboard after cursor;
    P 则插在当前光标位置(P 即 shift+p);

  • x - delete (cut) current character;
    <num>x:删除光标右侧的 num 字符;
    <num>X:删除光标左侧的 num 字符;

  • d - 删除

  • dd - delete (cut) a line,5dd - 删除5行。

  • d0d$dggdG - 删除到行头、行尾、文件头、文件尾。
    *i_0_CTRL-D*:0 CTRL-D Delete all indent in the current line. {Vi: CTRL-D works only when used after autoindent}
    <num>dw, <num>db:光标右侧、左侧删 num 个 word(/-#=空白分隔,何为 word 不太好讲,不常用;包括 yw, yb 在内都不常用);
    df(d f space),这是一个 自定义分隔符的简单做法

  • v - visual selection
    visual selection, cut-and-paste or copy-and-paste
    v, d v进入visual,然后选择,然后 d 即可删除。
    v, y
    v, p

Working with multiple files

:e filename - Edit a file
:tabe - make a new tab (tabedit)
:r <file> 把文件 file 的内容追加到当前光标行之后;

gt - go to the next tab
gT - go to the previous tab
ctrl+wv - Split windows verticall(:vsp - vertically split windows)
ctrl+ww - switch between windows
分窗口后,使用 :e 切换文件。
:split 横向分割为上下窗口,:close,:10split 则新窗口10行。
:vsplit 纵向分割为左右窗口

Search

  • search forwards
    /<pattern>,Then press n to search forwards for the next occurrence, or N to search backwards, or # to search backwards.

  • /\c<pattern>:search, case insensitive matching(不区分大小写)。

  • /\C<pattern> 区分大小写搜索;

  • Search backwards
    ?<pattern>,Pressing n searches in the same direction (backwards), while N searches in the opposite direction (forwards).

  • Searching for the current word
    Press * to search forwards for the next occurrence of that word, or press # to search backwards. n next forwards, N backwards.

  • 搜索 only whole words
    the pattern begins with \< and ends with \>, so only whole words are found. *# 就是典型的 word 搜索。

Search and replace 搜索和替换

  • g flag
    The g flag means global,即:Replace all matches,而不是 replace the first occurrence.

  • c flag
    The c flag means asking for confirmation.

  • %:in all the lines (on each line), 即搜索每一行,而不是仅仅当前行。

  • :%s/foo/bar/gc
    Change each 'foo' to 'bar', but ask for confirmation first.
    / 分隔符可以改为 @ 或者 # 等字符,但不能是 | 字符,这在替换含有 / 字符的字符串时很好。
    :%s/\<foo\>/bar/gc

Change only whole words exactly matching 'foo' to 'bar'; ask for confirmation.`

  • :3,8 s/foo/bar/g 搜索替换第3到第8行的所有foo并替换为bar

v 进入 -- VISUAL -- 模式

  • :'<,'>s/foo/bar/g
    v 进入 visual 模式,选中区域后,: 进入命令行,输入s/foo/bar/g 即可。
    When compiled with +visual, change each 'foo' to 'bar' for all lines within a visual selection. Vim automatically appends the visual selection range ('<,'>) for any ex command when you select an area and enter :.

  • :'<,'>s/ /#/
    visual 选中若干行,以 # 注释若干行,仅将最前面的一个空格替换为#(replace the first occurrence)。直接在行头插入 # 则 :'<,'>s/^/#/ 即可。

缩进 indent

  • In command mode
    you can use >> to indent a single line. 4>> will indent the current and next three lines. 可结合 . 重复操作。
  • visual mode
    先选中若干行,然后 >,可结合 . 重复操作。
  • 使用 < 则相反方向。
  • Shifting blocks visually

tab 字符转 空格 space

~/.vimrc 配置文件

vim 个性设置,里面是各个 vim 命令。The vimrc file contains optional runtime configuration settings to initialize Vim when it starts.
使用 " 标识注释(comments),可以使用中文;

set number " display line number
set laststatus=2 " status line: display filename
set ignorecase " Do case insensitive matching
set tabstop=4 " ts
set shiftwidth=4 " sw
set expandtab " tab to spaces,当你键入tab时,自动转为空格。
set cursorline " highlight the cursor line
set hlsearch " highlight search: hlsearch/hls/nohlsearch/nohls
set nowrap
  • Syntax highlighting in vim,通常默认支持;
:syntax enable
or
:sy enable
or
:syn enable
  • c2a0 问题;
    在页面上出现的代码,由于 页面字符处理转换 的缘故,经常会出现一些奇奇怪怪的东西,比如 c2a0,看起来也是一个空格,但实际上不是(空格 Space 的十六进制20),网页上使用
    奇怪字符可以使用 xxd 或者 hexdump 观察;错误提示和语法着色也能看出问题所在;
  • 查看当下设置
:verbose set
:verbose set ts
:verbose set shiftwidth
:verbose set all
  • 查看 highlight 设置
:hi
:hi search
  • vim 下查看ASCII 码
    :%!xxd 十六进制
    :%!xxd -b 二进制

  • /etc/vimrc
    System wide Vim initializations.

  • vim


    vim.png
  • vim --version


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,393评论 5 467
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,790评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,391评论 0 330
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,703评论 1 270
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,613评论 5 359
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,003评论 1 275
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,507评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,158评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,300评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,256评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,274评论 1 328
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,984评论 3 316
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,569评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,662评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,899评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,268评论 2 345
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,840评论 2 339

推荐阅读更多精彩内容

  • 文/面包 01 上次回家,一进门,我妈刚好在和爷爷奶奶通电话,我妈一见到我,就招呼我过去跟他们说几句。想想上一次见...
    面包走走停停阅读 336评论 0 0