ARTICLE DETAIL

资讯详情

深耕郑州网站建设与运营推广的一线实战洞察。

Vim 速查手册:从光标移动、编辑到多文件与多标签的完整命令指南(A-to-Z-Resources-for-Students CheatSheet 实战详解)

Vim 速查手册:从光标移动、编辑到多文件与多标签的完整命令指南(A-to-Z-Resources-for-Students CheatSheet 实战详解) 文档教程教育【免费下载链接】A-to-Z-Resources-for-Students✅ Curated list of resources for developers项目地址https://gitcode.com/GitHub_Trending/at/A-to-Z-Resources-for-Students点击查看免费下载本篇技术指南以开源仓库 A-to-Z-Resources-for-Students 中 CheatSheet/Vim_Cheatsheet.md 为骨架系统梳理 Vim 编辑器从基础操作到高级编排的完整命令体系。无论你是刚接触终端编辑器的初学者还是希望在面试、日常开发中快速提升文本操作效率的工程师读完本文都能掌握 Vim 的光标移动、插入模式、编辑与撤销、可视模式、剪切粘贴、搜索替换、多文件窗口以及标签页管理等核心技能并直接对照仓库中的速查表进行实战练习。该速查表与仓库内 ClanguageCheatSheet.md、Git-Cheatsheet.md 等文档同属 CheatSheet 资源模块是该精选资源库中面向开发者日常工具链的高密度速查集合之一。一、Vim 操作前的必备认知模式与全局命令Vim 的核心设计理念是模式驱动普通模式Normal Mode用于移动与操作插入模式Insert Mode用于输入文本可视模式Visual Mode用于选中文本。绝大多数命令在普通模式下执行按Esc可以从任何模式返回普通模式。打开 Vim 后最先需要掌握的是几个全局命令Global:help keyword # open help for keyword :o file # open file :saveas file # save file as :close # close current pane:help keyword查看任意命令、选项或主题的内置帮助例如:help w、:help motion。这是离线自学 Vim 的第一入口。:o file即:open file在当前窗口打开指定文件等价于:e file。:saveas file将当前缓冲区另存为新的文件名常用于另存为场景。:close关闭当前窗口窗格若该窗口是最后一个窗口则退出 Vim。从源码结构看本仓库将 Vim 速查表归类在 CheatSheet 目录下与 Docker、Git、Ruby、Jupyter、数据结构等速查表并列定位就是拿来即用的高密度参考因此下面的每一组命令都按命令 作用的格式完整呈现便于对照练习。二、光标移动Vim 效率的根基光标移动是 Vim 使用频率最高的操作掌握移动命令等于掌握了到达目标位置的语法。2.1 字符级与方向键移动h # move cursor left j # move cursor down k # move cursor up l # move cursor righth/j/k/l分别对应左/下/上/右移动一个字符或一行。它们取代方向键让右手不必离开主键盘区这是触手可及的效率起点。所有移动命令都支持数字前缀例如5j表示向下移动 5 行。2.2 屏幕级定位H # move to top of screen M # move to middle of screen L # move to bottom of screen zz # center cursor on screen Ctrl b # move back one full screen Ctrl f # move forward one full screen Ctrl d # move forward 1/2 a screen Ctrl u # move back 1/2 a screenHHome、MMiddle、LLast将光标跳到当前屏幕的顶部、中部、底部不改变滚动位置。zz把当前行滚动到屏幕正中央在长文件中定位当前编辑点时非常实用。Ctrl b/Ctrl f整屏向上/向下翻页b backwardf forward。Ctrl d/Ctrl u半屏向下/向上滚动d downu up滚动幅度更小适合阅读长文本。2.3 词级移动理解 word 与 WORDw # jump forwards to the start of a word W # jump forwards to the start of a word (words can contain punctuation) e # jump forwards to the end of a word E # jump forwards to the end of a word (words can contain punctuation) b # jump backwards to the start of a word B # jump backwards to the start of a word (words can contain punctuation)小写w/e/b按word移动word 由字母、数字、下划线组成遇到标点即断开大写W/E/B按WORD移动WORD 以空白符空格、制表符、换行为界标点符号算作词的一部分。例如在foo_bar, baz中小写w会把foo_bar整体视为一个词而大写W会把foo_bar,视为一个词。理解这两者的差异是精准跳转的关键。2.4 行级移动0 # jump to the start of the line ^ # jump to the first non-blank character of the line $ # jump to the end of the line g_ # jump to the last non-blank character of the line0跳到行首第 0 列包括行首空白。^跳到行内第一个非空白字符对缩进代码更实用。$跳到行尾含行尾空格。g_跳到行内最后一个非空白字符与$的区别在于忽略行尾空白。2.5 文档级与字符查找移动gg # go to the first line of the document G # go to the last line of the document 5G # go to line 5 fx # jump to next occurrence of character x tx # jump to before next occurrence of character x } # jump to next paragraph (or function/block, when editing code) { # jump to previous paragraph (or function/block, when editing code)gg到文件首行G到文件末行5G或:5跳到第 5 行——数字前缀在此表示行号。fx向右查找并跳到当前行内下一个字符x上tx则停在字符x之前。继续查找可用;反向用,。}与{按段落空行分隔前后跳转在编辑代码时许多语言模式下会按函数或代码块边界跳转这是在大段代码间快速穿梭的利器。三、插入模式在正确的位置输入文本进入插入模式有 8 种常见方式区别在于从哪里开始输入i # insert before the cursor I # insert at the beginning of the line a # insert (append) after the cursor A # insert (append) at the end of the line o # append (open) a new line below the current line O # append (open) a new line above the current line ea # insert (append) at the end of the word Esc # exit insert modei在光标前插入a在光标后插入I跳到行首第一个非空白字符处插入A跳到行尾插入等价于$a。o在当前行下方新开一行并进入插入模式O在上一行新开一行——这是补行的标准操作。ea是移动到词尾再追加的组合用法常用于在词尾补字符而不必先按e再按a。任何时刻按Esc退出插入模式返回普通模式继续执行其他命令。四、编辑操作替换、删除与撤销重做r # replace a single character J # join line below to the current one cc # change (replace) entire line cw # change (replace) to the start of the next word ce # change (replace) to the end of the next word cb # change (replace) to the start of the previous word c0 # change (replace) to the start of the line c$ # change (replace) to the end of the line s # delete character and substitute text S # delete line and substitute text (same as cc) xp # transpose two letters (delete and paste) . # repeat last command u # undo Ctrl r # redor原地替换单个字符替换后自动回到普通模式不进入插入模式。J将下一行拼接到当前行末尾中间自动补一个空格。cchange系列是 Vim 最强大的编辑语法之一c 移动命令 删除到目标位置并进入插入模式。cc改整行cw改到下一个词的词首ce改到当前词尾cb改到上一个词首c0改到行首c$改到行尾。s删除光标处字符并进入插入模式等价于xiS删除整行并进入插入模式与cc相同。xp交换光标处字符与下一个字符先删后贴是修正拼写错位的快捷技巧。.重复上一次修改命令批量修正重复问题时威力极大。u撤销Ctrl r重做。五、可视模式与可视命令先选中再操作5.1 进入可视模式v # start visual mode, mark lines, then do a command (like y-yank) V # start linewise visual mode Ctrl v # start visual block mode o # move to other end of marked area O # move to other corner of block aw # mark a word ab # a block with () aB # a block with {} ib # inner block with () iB # inner block with {} Esc # exit visual modev进入字符级可视模式V进入行级可视模式Ctrl v进入块级列级可视模式——块模式是批量编辑多列文本的核心工具。选中过程中o把光标跳到选区另一端方便调整选区范围O在块模式中跳到选区的对角。文本对象Text Objects直接给出语义化选区aw选中一个词含周围空白ab/aB选中包含括号()/{}的整块ib/iB选中括号内部内容不含括号本身。文本对象也可与d、c、y组合如diw删除光标所在词。5.2 可视命令 # shift text right # shift text left y # yank (copy) marked text d # delete marked text ~ # switch case选中文本后整体右移缩进整体左移缩进y复制选区d删除选区~切换选区字符的大小写。可视模式让先划定影响范围、再执行操作成为可能是重构代码块、批量调整缩进的首选路径。六、剪切与粘贴yank 与 put 的完整语法yy # yank (copy) a line 2yy # yank (copy) 2 lines yw # yank (copy) the characters of the word from the cursor position to the start of the next word y$ # yank (copy) to end of line p # put (paste) the clipboard after cursor P # put (paste) before cursor dd # delete (cut) a line 2dd # delete (cut) 2 lines dw # delete (cut) the characters of the word from the cursor position to the start of the next word D # delete (cut) to the end of the line d$ # delete (cut) to the end of the line d^ # delete (cut) to the first non-blank character of the line d0 # delete (cut) to the begining of the line x # delete (cut) character复制yank命令以y开头同样遵循y 移动命令的组合语法yy复制当前整行2yy复制 2 行yw复制从光标到下一词词首的字符y$复制到行尾。粘贴putp在光标后粘贴P在光标前粘贴行级内容粘贴时会新起一行。删除即剪切命令以d开头dd删除当前行2dd删除 2 行dw删除到下一词词首D等价于d$删除到行尾d^删除到行首第一个非空白字符d0删除到行首含空白x删除光标处单个字符。被删除或复制的文本都会进入寄存器可用p/P取出因此先dd再p就是标准剪切粘贴流程。七、搜索与替换正则与全局替换7.1 搜索命令/pattern # search for pattern ?pattern # search backward for pattern \vpattern # very magic pattern: non-alphanumeric characters are interpreted as special regex symbols (no escaping needed) n # repeat search in same direction N # repeat search in opposite direction :noh # remove highlighting of search matches/pattern向下搜索?pattern向上搜索。搜索后按n沿同方向继续按N反向继续。\vpattern使用very magic正则模式此时(、、|等非字母数字字符直接按正则特殊符号解释无需转义例如/\v(foo|bar)可直接书写。这是 Vim 正则中书写最简洁的模式。搜索命中默认高亮:nohnohlsearch可临时关闭高亮避免视觉干扰。7.2 全局替换:%s/old/new/g # replace all old with new throughout file :%s/old/new/gc # replace all old with new throughout file with confirmations:%s/old/new/g将全文件所有old替换为new。语法拆解%表示作用于所有行s是 substitute替换命令g表示行内所有匹配而非仅每行第一个。加cconfirm后每次替换前会询问确认适合高风险的大规模替换。进阶组合:%s/old/new/gc可配合y确认、n跳过、a全部替换、q退出等按键交互。八、跨文件搜索vimgrep 与结果列表:vimgrep /pattern/ {file} # search for pattern in multiple files :cn # jump to the next match :cp # jump to the previous match :copen # open a window containing the list of matches:vimgrep /pattern/ {file}在指定文件支持通配符如**/*.py中搜索模式把匹配结果填入快速修复列表quickfix list。:cn跳到下一个匹配处:cp跳到上一个匹配处。:copen打开包含全部匹配行的列表窗口点击或回车即可跳转。这套流程是纯 Vim 环境下跨文件代码检索的标准方案与插件Ctrl f类搜索工具的目标一致。九、保存与退出五种退出语义:w # write (save) the file, but dont exit :w !sudo tee % # write out the current file using sudo :wq or :x or ZZ # write (save) and quit :q # quit (fails if there are unsaved changes) :q! or ZQ # quit and throw away unsaved changes:w保存但不退出。:w !sudo tee %是一个经典的权限自救技巧当文件属于 root 而无写权限时用sudo tee回写当前文件名%代表当前文件路径避免重新用 sudo 打开文件。:wq、:x、ZZ三种写法都表示保存并退出区别在于:x与ZZ仅在文件有改动时才执行写入。:q直接退出若存在未保存的改动会失败并提示。:q!或ZQ放弃所有未保存改动强制退出。十、多文件与窗口管理buffer 与 split:e file # edit a file in a new buffer :bnext or :bn # go to the next buffer :bprev or :bp # go to the previous buffer :bd # delete a buffer (close a file) :ls # list all open buffers :sp file # open a file in a new buffer and split window :vsp file # open a file in a new buffer and vertically split window Ctrl ws # split window Ctrl ww # switch windows Ctrl wq # quit a window Ctrl wv # split window vertically Ctrl wh # move cursor to the left window (vertical split) Ctrl wl # move cursor to the right window (vertical split) Ctrl wj # move cursor to the window below (horizontal split) Ctrl wk # move cursor to the window above (horizontal split)buffer 是 Vim 内存中的文件副本:e file在新 buffer 中打开文件:bn/:bp在相邻 buffer 间切换:bd关闭删除当前 buffer:ls列出所有已打开的 buffer。窗口window是 buffer 的可视化视口:sp file水平切分并在新窗口中打开文件:vsp file垂直切分。Ctrl w前缀系列是窗口管理的快捷键Ctrl ws水平切分、Ctrl wv垂直切分、Ctrl ww循环切换窗口、Ctrl wq关闭当前窗口Ctrl wh/wl/wj/wk分别把光标移到左/右/下/上方的窗口。掌握这套组合后左右对照代码、上下分屏编辑都只需一次按键。十一、标签页Vim 的浏览器式工作区:tabnew or :tabnew file # open a file in a new tab Ctrl wT # move the current split window into its own tab gt or :tabnext or :tabn # move to the next tab gT or :tabprev or :tabp # move to the previous tab numbergt # move to tab number :tabmove number # move current tab to the numberth position (indexed from 0) :tabclose or :tabc # close the current tab and all its windows :tabonly or :tabo # close all tabs except for the current one :tabdo command # run the command on all tabs (e.g. :tabdo q - closes all opened tabs):tabnew新建空标签页:tabnew file在新标签页中打开文件。Ctrl wT把当前窗口提升为一个独立标签页。gt/:tabnext切到下一个标签页gT/:tabprev切到上一个标签页numbergt直接跳到第number个标签页例如2gt跳到第 2 个标签页。:tabmove number把当前标签页移动到指定序号位置从 0 开始计数。:tabclose关闭当前标签页及其全部窗口:tabonly关闭除当前外的所有标签页。:tabdo command对全部标签页统一执行命令如:tabdo q一次性关闭所有已打开的标签页。注意:tabdo与:bufdo、:windo一样属于批量执行家族命令适合统一保存:tabdo w或统一关闭。十二、总结与练习建议本文完整复刻了仓库 CheatSheet/Vim_Cheatsheet.md 的全部命令条目并按全局命令 → 移动 → 插入 → 编辑 → 可视 → 剪切粘贴 → 搜索替换 → 跨文件搜索 → 退出 → 多文件窗口 → 标签页的操作链路重新组织。建议的学习路径是先花 15 分钟在vimtutor中练熟h/j/k/l、w/b/e、0/$与gg/G等移动命令再对照本文第三节至第六节把c/d/y 移动命令的组合语法如cw、dd、yy、ciw逐一在真实代码上演练最后实践第七至第十一节的多文件、窗口与标签页编排把 Vim 从编辑器升级为工作台。速查表的本质是记忆的外置把本文或仓库中的原文档留在手边配合:help按需查询两周内即可让 Vim 操作形成肌肉记忆。如需浏览仓库内其他语言与工具的速查表可继续阅读 ClanguageCheatSheet.md、Git-Cheatsheet.md、Docker.md、Ruby-CheatSheet.md 与 Jupyter_Notebook_cheatsheet.md构成一套完整的开发者日常工具速查体系。赞分享文档教程教育【免费下载链接】A-to-Z-Resources-for-Students✅ Curated list of resources for developers项目地址https://gitcode.com/GitHub_Trending/at/A-to-Z-Resources-for-Students点击查看免费下载相关推荐Reference 项目 Vim 8.2 速查手册从光标移动到多文件协作的完整命令实战指南Reference 项目 Vim 8.2 速查手册从光标移动到多文件协作的完整命令实战指南 本篇文章基于 Reference 开源仓库中的 Vim 速查文档文档教程知识库解决ics.py时区难题从UTC到本地时间的完美转换技巧解决ics.py时区难题从UTC到本地时间的完美转换技巧 在处理iCalendar文件时时区转换往往是最令人头疼的问题之一。 ics.py 作为Python开发工具A-to-Z-Resources-for-Students技术社区内容标签管理规则A to Z Resources for Students技术社区内容标签管理规则 在技术学习的海洋中面对海量的学习资源如何快速准确地找到自己所需的内容文档教程教育上一篇终极指南阿波罗11号如何精确计算太阳与月球位置下一篇Magisk多设备兼容支持从Android 6.0到最新版本的实现原理与适配指南创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表