Linux编辑器——vim的基础使用

文章目录

1.vim的基本概念

本文将介绍vim的三种模式,分别位:命令模式、插入模式、低行模式。他们的功能区分如下:
正常/普通/命令模式

控制屏幕光标的移动、字符、字或行的删除、移动某区段及进入插入模式,或者到末行模式
插入模式

只有在插入模式才可以做文字输入,按【ESC】键可退回命令行模式。该模式是我们后面使用最为频繁的编辑模式
末行(底行)模式

文件保存或者退出,也可以进行文件的替换,找字符串,列出行号等操作。在命令模式下,【shift + ;】即可进入该模式。要查看你的所有模式:打开vim,底行模式直接输入:help vim-modes

就会弹出这这个窗口:

powershell 复制代码
Vim has six BASIC modes:

                                        Normal Normal-mode command-mode
Normal mode             In Normal mode you can enter all the normal editor
                        commands.  If you start the editor you are in this
                        mode (unless you have set the 'insertmode' option,
                        see below).  This is also known as command mode.

Visual mode             This is like Normal mode, but the movement commands
                        extend a highlighted area.  When a non-movement
                        command is used, it is executed for the highlighted
                        area.  See Visual-mode.
                        If the 'showmode' option is on "-- VISUAL --" is shown
                        at the bottom of the window.
Select mode             This looks most like the MS-Windows selection mode.
                        Typing a printable character deletes the selection
                        and starts Insert mode.  See Select-mode.
                        If the 'showmode' option is on "-- SELECT --" is shown
                        at the bottom of the window.

Insert mode             In Insert mode the text you type is inserted into the
                        buffer.  See Insert-mode.
                        If the 'showmode' option is on "-- INSERT --" is shown
                        at the bottom of the window.

Command-line mode       In Command-line mode (also called Cmdline mode) you
Cmdline mode            can enter one line of text at the bottom of the
                        window.  This is for the Ex commands, ":", the pattern
                        search commands, "?" and "/", and the filter command,
                        "!".  Cmdline-mode
Ex mode                 Like Command-line mode, but after entering a command
                        you remain in Ex mode.  Very limited editing of the
                        command line.  Ex-mode                       

2.vim的基本操作

进入vim,在系统提示符号输入vim及文件名称后,就进入vim全屏编辑画面:
注意:刚进入vim中位命令模式,只有切换为插入模式才正常输入文字。

命令模式切换插入模式

输入:【a/i/o】

插入模式切换命令模式

【ESC】

命令模式切换底行模式

【shift+;】即【:】

退出vim及保存文件,在命令模式下,按【:】进入底行模式,输入

【w】保存当前文件

【wq】保存并退出

【q!】不保存强制退出,(!为强制符号,还可以和其他字符搭配)

3.vim命令模式命令集

模式切换插入模式:

按「i」切换进入插入模式「insert mode」,按"i"进入插入模式后是从光标当前位置开始输入文件;

按「a」进入插入模式后,是从目前光标所在位置的下一个位置开始输入文字;

按「o」进入插入模式后,是插入新的一行,从行首开始输入文字

3.1移动光标

vim可以直接用键盘上的光标来上下左右移动,但正规的vim是用小写英文字母「h」、「j」、「k」、

「l」,分别控制光标左、下、上、右移一格

按「G」:移动到文章的最后

按「 $ 」:移动到光标所在行的"行尾"

按「^」:移动到光标所在行的"行首"

按「w」:光标跳到下个字的开头

按「e」:光标跳到下个字的字尾

按「b」:光标回到上个字的开头

按「#l」:光标移到该行的第#个位置,如:5l,56l

按[gg]:进入到文本开始

按[shift+g]:进入文本末端

按「ctrl」+「b」:屏幕往"后"移动一页

按「ctrl」+「f」:屏幕往"前"移动一页

按「ctrl」+「u」:屏幕往"后"移动半页 按「ctrl」+「d」:屏幕往"前"移动半页

注意:【】中的大写字母即【shift+相应小写字母】

3.2删除文字

按「x」:每按一次,删除光标所在位置的一个字符

按「#x」:例如,「6x」表示删除光标所在位置的"后面(包含自己在内)"6个字符

按「X」:大写的X,每按一次,删除光标所在位置的"前面"一个字符

按「#X」:例如,「20X」表示删除光标所在位置的"前面"20个字符

按「dd」:删除光标所在行

按「#dd」:从光标所在行开始删除#行

3.3复制

按「yw」:将光标所在之处到字尾的字符复制到缓冲区中。

按「#yw」:复制#个字到缓冲区

按「yy」:复制光标所在行到缓冲区。

按「#yy」:例如,「6yy」表示拷贝从光标所在的该行"往下数"6行文字。

按「p」:将缓冲区内的字符贴到光标所在位置。注意:所有与"y"有关的复制命令都必须与"p"配合才能完 成复制与粘贴功能

3.4替换

按「r」:替换光标所在处的字符。

按「R」:替换光标所到之处的字符,直到按下「ESC」键为止。

3.5撤销

按「u」:如果您误执行一个命令,可以马上按下「u」,回到上一个操作。按多次"u"可以执行多次回 复。

按「ctrl + r」: 撤销的撤销

3.6更改

按「cw」:更改光标所在处的字到字尾处

按「c#w」:例如,「c3w」表示更改3个字

3.7跳到指定的行

按「ctrl」+「g」列出光标所在行的行号。

按「#G」:例如,「15G」,表示移动光标至文章的第15行行首

相关推荐
三品吉他手会点灯42 分钟前
C语言学习笔记 - 50.流程控制4 - 流程控制为什么非常非常重要
c语言·开发语言·笔记·学习
chushiyunen2 小时前
langchain4j笔记、tools
笔记·python·flask
影视飓风TIM4 小时前
数据结构 | 链表超全笔记(单链表+双链表+高频算法题)
数据结构·笔记·链表
二哈赛车手4 小时前
新人笔记---最终版智能体图片分析完整方案,包括一些总结于经验,以及各种优化点讲解
java·笔记·spring·ai·springboot
_李小白4 小时前
【智能驾驶:视觉感知后处理 阅读笔记】Day4: 相机成像模型与畸变
笔记·数码相机
zyplayer-doc5 小时前
企业知识库安全与权限管理完全指南:从加密到审计的六层防护
人工智能·安全·pdf·编辑器·创业创新
十月的皮皮5 小时前
C语言学习笔记20260615-有序升序序列合并
c语言·笔记·学习
暮云星影5 小时前
全志linux开发屏幕适配(一)屏幕参数设置说明
linux·arm开发
古德new5 小时前
鸿蒙PC迁移:Photoflare Qt 图片编辑器鸿蒙PC适配全记录
qt·编辑器·harmonyos
swordbob6 小时前
NIO 的 Channel 里有多个 BIO 吗?
linux·网络·nio