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

发布于:2024-05-05 ⋅ 阅读:(28) ⋅ 点赞:(0)

1.vim的基本概念

切换

本文将介绍vim的三种模式,分别位:命令模式、插入模式、低行模式。他们的功能区分如下:
正常/普通/命令模式
控制屏幕光标的移动、字符、字或行的删除、移动某区段及进入插入模式,或者到末行模式
插入模式
只有在插入模式才可以做文字输入,按【ESC】键可退回命令行模式。该模式是我们后面使用最为频繁的编辑模式
末行(底行)模式
文件保存或者退出,也可以进行文件的替换,找字符串,列出行号等操作。在命令模式下,【shift + ;】即可进入该模式。要查看你的所有模式:打开vim,底行模式直接输入:help vim-modes
就会弹出这这个窗口:

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行行首

在这里插入图片描述


网站公告

今日签到

点亮在社区的每一天
去签到