以DEBIAN和ubuntu为例
修改源文件:
vi /etc/apt/sources.list
debian官方源:
deb http://deb.debian.org/debian buster main contrib non-free
deb http://deb.debian.org/debian buster-updates main contrib non-free
deb http://deb.debian.org/debian-security/ buster/updates main contrib non-free
deb-src http://deb.debian.org/debian buster main contrib non-free
deb-src http://deb.debian.org/debian buster-updates main contrib non-free
deb-src http://deb.debian.org/debian-security/ buster/updates main contrib non-free
更新为阿里云的源:
deb http://mirrors.aliyun.com/debian/ buster main non-free contrib
deb http://mirrors.aliyun.com/debian/ buster-updates main non-free contrib
deb http://mirrors.aliyun.com/debian/ buster-backports main non-free contrib
deb http://mirrors.aliyun.com/debian-security buster/updates main
海外UBUNTU源:
# See http://help.ubuntu.com/community/UpgradeNotes for how to upgrade to
# newer versions of the distribution.
deb http://us.archive.ubuntu.com/ubuntu/ bionic main restricted
# deb-src http://us.archive.ubuntu.com/ubuntu/ bionic main restricted
## Major bug fix updates produced after the final release of the
## distribution.
deb http://us.archive.ubuntu.com/ubuntu/ bionic-updates main restricted
# deb-src http://us.archive.ubuntu.com/ubuntu/ bionic-updates main restricted
## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
## team. Also, please note that software in universe WILL NOT receive any
## review or updates from the Ubuntu security team.
deb http://us.archive.ubuntu.com/ubuntu/ bionic universe
# deb-src http://us.archive.ubuntu.com/ubuntu/ bionic universe
deb http://us.archive.ubuntu.com/ubuntu/ bionic-updates universe
# deb-src http://us.archive.ubuntu.com/ubuntu/ bionic-updates universe
## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
## team, and may not be under a free licence. Please satisfy yourself as to
## your rights to use the software. Also, please note that software in
## multiverse WILL NOT receive any review or updates from the Ubuntu
## security team.
deb http://us.archive.ubuntu.com/ubuntu/ bionic multiverse
# deb-src http://us.archive.ubuntu.com/ubuntu/ bionic multiverse
deb http://us.archive.ubuntu.com/ubuntu/ bionic-updates multiverse
# deb-src http://us.archive.ubuntu.com/ubuntu/ bionic-updates multiverse
## N.B. software from this repository may not have been tested as
## extensively as that contained in the main release, although it includes
## newer versions of some applications which may provide useful features.
## Also, please note that software in backports WILL NOT receive any review
## or updates from the Ubuntu security team.
# deb http://us.archive.ubuntu.com/ubuntu/ bionic-backports main restricted universe multiverse
# deb-src http://us.archive.ubuntu.com/ubuntu/ bionic-backports main restricted universe multiverse
## Uncomment the following two lines to add software from Canonical's
## 'partner' repository.
## This software is not part of Ubuntu, but is offered by Canonical and the
## respective vendors as a service to Ubuntu users.
# deb http://archive.canonical.com/ubuntu bionic partner
# deb-src http://archive.canonical.com/ubuntu bionic partner
deb http://security.ubuntu.com/ubuntu bionic-security main restricted
# deb-src http://security.ubuntu.com/ubuntu bionic-security main restricted
deb http://security.ubuntu.com/ubuntu bionic-security universe
# deb-src http://security.ubuntu.com/ubuntu bionic-security universe
deb http://security.ubuntu.com/ubuntu bionic-security multiverse
# deb-src http://security.ubuntu.com/ubuntu bionic-security multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
deb cdrom:[Ubuntu 20.04.2.0 LTS Focal Fossa - Release amd64 (20210209.1)]/ focal main restricted
See http://help.ubuntu.com/community/UpgradeNotes for how to upgrade to
newer versions of the distribution.
deb http://br.archive.ubuntu.com/ubuntu/ focal main restricted
deb-src http://br.archive.ubuntu.com/ubuntu/ focal main restricted
Major bug fix updates produced after the final release of the
distribution.
deb http://br.archive.ubuntu.com/ubuntu/ focal-updates main restricted
deb-src http://br.archive.ubuntu.com/ubuntu/ focal-updates main restricted
N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
team. Also, please note that software in universe WILL NOT receive any
review or updates from the Ubuntu security team.
deb http://br.archive.ubuntu.com/ubuntu/ focal universe
deb-src http://br.archive.ubuntu.com/ubuntu/ focal universe
deb http://br.archive.ubuntu.com/ubuntu/ focal-updates universe
deb-src http://br.archive.ubuntu.com/ubuntu/ focal-updates universe
N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
team, and may not be under a free licence. Please satisfy yourself as to
your rights to use the software. Also, please note that software in
multiverse WILL NOT receive any review or updates from the Ubuntu
security team.
deb http://br.archive.ubuntu.com/ubuntu/ focal multiverse
deb-src http://br.archive.ubuntu.com/ubuntu/ focal multiverse
deb http://br.archive.ubuntu.com/ubuntu/ focal-updates multiverse
deb-src http://br.archive.ubuntu.com/ubuntu/ focal-updates multiverse
N.B. software from this repository may not have been tested as
extensively as that contained in the main release, although it includes
newer versions of some applications which may provide useful features.
Also, please note that software in backports WILL NOT receive any review
or updates from the Ubuntu security team.
deb http://br.archive.ubuntu.com/ubuntu/ focal-backports main restricted universe multiverse
deb-src http://br.archive.ubuntu.com/ubuntu/ focal-backports main restricted universe multiverse
Uncomment the following two lines to add software from Canonical's
'partner' repository.
This software is not part of Ubuntu, but is offered by Canonical and the
respective vendors as a service to Ubuntu users.
deb http://archive.canonical.com/ubuntu focal partner
deb-src http://archive.canonical.com/ubuntu focal partner
deb http://security.ubuntu.com/ubuntu focal-security main restricted
deb-src http://security.ubuntu.com/ubuntu focal-security main restricted
deb http://security.ubuntu.com/ubuntu focal-security universe
deb-src http://security.ubuntu.com/ubuntu focal-security universe
deb http://security.ubuntu.com/ubuntu focal-security multiverse
deb-src http://security.ubuntu.com/ubuntu focal-security multiverse
This system was installed using small removable media
(e.g. netinst, live or single CD). The matching "deb cdrom"
entries were disabled at the end of the installation process.
For information about how to configure apt package sources,
see the sources.list(5) manual.
编程软件下载/编译器下载
C语言辅导班 C++辅导班 算法/数据结构辅导班 登录 | 注册
C语言中文网首页C语言教程C++教程Python教程Java教程Linux入门更多>>VIP会员 辅导班
首页 / Linux教程 /
10分钟掌握Linux vi编辑器常见命令的使用,最简单的vi编辑器教程
<上一节下一节>关注我们:微信公众号新浪微博
C语言中文网推出辅导班啦,包括「C语言辅导班、C++辅导班、算法/数据结构辅导班」,全部都是一对一教学:一对一辅导 + 一对一答疑 + 布置作业 + 项目实践 + 永久学习。QQ在线,随时响应!
Linux下的文本编辑器有很多种,vi 是最常用的,也是各版本Linux的标配。注意,vi 仅仅是一个文本编辑器,可以给字符着色,可以自动补全,但是不像 Windows 下的 word 有排版功能。
vi 是十年磨一剑的产品,虽然命令繁多,并且大多数功能都是依靠键盘输入来完成,但是一旦你熟悉后,会发现 vi 的功能和效率是其他图形界面编辑器无法比拟的。
Vim 是 Vi improved 的缩写,是 vi 的改进版。在Linux中,vi 被认为是事实上的标准编辑器,因为:
所有版本的 Linux 都带有 vi 编辑器;
占用资源少;
与 ed、ex 等其他编辑器相比,vi 对用户更加友好。
你可以使用 vi 编辑器编辑现有的文件,也可以创建一个新文件,还能以只读模式打开文本文件。
进入 vi 编辑器
可以通过以下方式进入 vi 编辑器:
命令 描述
vi filename 如果filename存在,则打开;否则会创建一个新文件再打开。
vi -R filename 以只读模式(只能查看不能编辑)打开现有文件。
view filename 以只读模式打开现有文件。
例如,使用 vi 编辑器创建一个新文件并打开:
$vi testfile
|
~
~
~
~
~
~
~
~
~
~
~
~
"testfile" [New File]
竖线(|)代表光标的位置;波浪号(~)代表该行没有任何内容。如果没有 ~,也看不到任何内容,那说明这一行肯定是有空白字符(空格、tab 缩进、换行符等)或不可见字符。
工作模式
进一步了解 vi 之前先来了解一下 vi 的工作模式,vi 有三种工作模式:
1) 普通模式
由Shell进入vi编辑器时,首先进入普通模式。在普通模式下,从键盘输入任何字符都被当作命令来解释。普通模式下没有任何提示符,输入命令后立即执行,不需要回车,而且输入的字符不会在屏幕上显示出来。
普通模式下可以执行命令、保存文件、移动光标、粘贴复制等。
2) 编辑模式
编辑模式主要用于文本的编辑。该模式下用户输入的任何字符都被作为文件的内容保存起来,并在屏幕上显示出来。
3) 命令模式
命令模式下,用户可以对文件进行一些高级处理。尽管普通模式下的命令可以完成很多功能,但要执行一些如字符串查找、替换、显示行号等操作还是必须要进入命令模式。
注意:有些教程中称有两种工作模式,是把命令模式合并到普通模式。
工作模式切换:
在普通模式下输入 i(插入)、c(修改)、o(另起一行) 命令时进入编辑模式;按 esc 键退回到普通模式。
在普通模式下输入冒号(:)可以进入命令模式。输入完命令按回车,命令执行完后会自动退回普通模式。
提示:如果不确定当前处于哪种模式,按两次 Esc 键将回到普通模式。
退出 vi 编辑器
一般在命令模式下退出 vi 编辑器。
退出命令 说明
q 如果文件未被修改,会直接退回到Shell;否则提示保存文件。
q! 强行退出,不保存修改内容。
wq w 命令保存文件,q 命令退出 vi,合起来就是保存并退出。
ZZ 保存并退出,相当于 wq,但是更加方便。
退出之前,你也可以在 w 命令后面指定一个文件名,将文件另存为新文件,例如:
w filename2
将当前文件另存为 filename2。
注意:vi 编辑文件时,用户的操作都是基于缓冲区中的副本进行的。如果退出时没有保存到磁盘,则缓冲区中的内容就会被丢失。
移动光标
为了不影响文件内容,必须在普通模式(按两次 Esc 键)下移动光标。使用下表中的命令每次可以移动一个字符:
命令 描述
k 向上移动光标(移动一行)
j 向下移动光标(移动一行)
h 向左移动光标(移动一个字符)
l 向右移动光标(移动一个字符)
两点提醒:
vi 是区分大小写的,输入命令时注意不要锁定大写。
可以在命令前边添加一个数字作为前缀,例如,2j 将光标向下移动两行。
当然,还有很多其他命令来移动光标,不过记住,一定要在普通模式(按两次 Esc 键)下。
用来移动光标的命令
命令 说明
0 或 | 将光标定位在一行的开头。
$ 将光标定位在一行的末尾。
w 定位到下一个单词。
b 定位到上一个单词。
( 定位到一句话的开头,句子是以 ! . ? 三种符号来界定的。
) 定位到一句话的结尾。
{ 移动到段落开头。&&&&&&
} 移动到段落结束。&&&&&&&&&
[[ 回到段落的开头处。&&&&&&&&&&
]] 向前移到下一个段落的开头处。&&&&&&&&&&
n| 移动到第 n 列(当前行)。
1G 移动到文件第一行。
G 移动到文件最后一行。
nG 移动到文件第 n 行。
:n 移动到文件第 n 行。
H 移动到屏幕顶部。
nH 移动到距离屏幕顶部第 n 行的位置。
M 移动到屏幕中间。
L 移动到屏幕底部。
nL 移动到距离屏幕底部第 n 行的位置。
:x x 是一个数字,表示移动到行号为 x 的行。
控制命令
有一些控制命令可以与 Ctrl 键组合使用,如下:
命令 描述
CTRL+d 向前滚动半屏
CTRL+f 向前滚动全屏
CTRL+u 向后滚动半屏
CTRL+b 向后滚动整屏
CTRL+e 向上滚动一行
CTRL+y 向下滚动一行
CTRL+I 刷新屏幕
编辑文件
切换到编辑模式下才能编辑文件。有很多命令可以从普通模式切换到编辑模式,如下所示:
命令 描述
i 在当前光标位置之前插入文本
I 在当前行的开头插入文本
a 在当前光标位置之后插入文本
A 在当前行的末尾插入文本
o 在当前位置下面创建一行
O 在当前位置上面创建一行
删除字符
下面的命令,可以删除文件中的字符或行:
命令 说明
x 删除当前光标下的字符
X 删除光标前面的字符
dw 删除从当前光标到单词结尾的字符
d^ 删除从当前光标到行首的字符
d$ 删除从当前光标到行尾的字符
D 删除从当前光标到行尾的字符
dd 删除当前光标所在的行
可以在命令前面添加一个数字前缀,表示重复操作的次数,例如,2x 表示连续两次删除光标下的字符,2dd 表示连续两次删除光标所在的行。
建议各位读者多加练习上面的命令,再进一步深入学习。
修改文本
如果你希望对字符、单词或行进行修改,可以使用下面的命令:
命令 描述
cc 删除当前行,并进入编辑模式。
cw 删除当前字(单词),并进入编辑模式。
r 替换当前光标下的字符。
R 从当前光标开始替换字符,按 Esc 键退出。
s 用输入的字符替换当前字符,并进入编辑模式。
S 用输入的文本替换当前行,并进入编辑模式。
粘贴复制
vi 中的复制粘贴命令:
命令 描述
yy 复制当前行
nyy 复制n行
yw 复制一个字(单词)
nyw 复制n行
p 将复制的文本粘贴到光标后面
P 将复制的文本粘贴到光标前面
高级命令
下面的一些命令虽然看起来有些古怪,但是会让你的工作更有效率,如果你是 vi 重度用户,就了解一下吧。
命令 说明
J 将当前行和下一行连接为一行
<< 将当前行左移一个单位(一个缩进宽度)
将当前行右移一个单位(一个缩进宽度)
~ 改变当前字符的大小写
^G Ctrl+G组合键可以显示当前文件名和状态
U 撤销对当前行所做的修改
u 撤销上次操作,再次按 'u' 恢复该次操作
:f 以百分号(%)的形式显示当前光标在文件中的位置、文件名和文件的总行数
:f filename 将文件重命名为 filename
:w filename 保存修改到 filename
:e filename 打开另一个文件名为 filename 的文件
:cd dirname 改变当前工作目录到 dirname
:e # 在两个打开的文件之间进行切换
:n 如果用 vi 打开了多个文件,可以使用 :n 切换到下一个文件
:p 如果用 vi 打开了多个文件,可以使用 :n 切换到上一个文件
:N 如果用 vi 打开了多个文件,可以使用 :n 切换到上一个文件
:r file 读取文件并在当前行的后边插入
:nr file 读取文件并在第 n 行后边插入
文本查找
如果希望进行全文件搜索,可以在普通模式(按两次 Esc 键)下输入 / 命令,这时状态栏(最后一行)出现"/"并提示输入要查找的字符串,回车即可。
/ 命令是向下查找,如果希望向上查找,可以使用 ? 命令。
这时,输入 n 命令可以按相同的方向继续查找,输入 N 命令可以按相反的方向继续查找。
搜索的字符串中可以包含一些有特殊含义的字符,如果希望搜索这些字符本身,需要在前面加反斜杠()。
部分特殊字符列表
字符 说明
^ 匹配一行的开头
. 匹配一个字符
- 匹配0个或多个字符
$ 匹配一行的结尾
[ ] 匹配一组字符
如果希望搜索某行中的单个字符,可以使用 f 或 F 命令,f 向上搜索,F 向下搜索,并且会把光标定位到匹配的字符。
也可以使用 t 或 T 命令:t 命令向上搜索,并把光标定位到匹配字符的前面;T 命令向下搜索,并把光标定位到匹配字符的后面。
set 命令
set 命令可以对 vi 编辑器进行一些设置。使用 set 命令需要进入命令模式。
:set 命令选项
命令 说明
:set ic 搜索时忽略大小写。
:set ai 设置自动缩进(自动对齐)。
:set noai 取消自动缩进(自动对齐)。
:set nu 显示行号。
:set sw 设置缩进的空格数,例如,将缩进空格数设置为4::set sw=4。
:set ws 循环搜索:如果直到文件末尾也没有查找到指定字符,那么会回到开头继续查找。
:set wm 设置自动换行,例如,设置距离边际2个字符时换行::set wm=2 。
:set ro 将文件类型改为只读。
:set term 输出终端类型。
:set bf 忽略输入的控制字符,如 BEL(响铃)、BS(退格)、CR(回车)等。
运行命令
切换到命令模式,再输入 ! 命令即可运行 Linux 命令。
例如,保存文件前,如果希望查看该文件是否存在,那么输入
:! ls
即可列出当前目录下的文件。
按任意键回到 vi 编辑器。
文本替换
切换到命令模式,再输入 s/ 命令即可对文本进行替换。语法为:
:s/search/replace/g
search 为检索的文本,replace 为要替换的文本,g 表示全局替换。
几点提示
vi 编辑器的使用讲解完毕,但是请记住下面几点:
输入冒号(:)进入命令模式,按两次 Esc 键进入普通模式。
命令大小写的含义是不一样的。
必须在编辑模式下才能输入内容。
C语言中文网推出辅导班啦,包括「C语言辅导班、C++辅导班、算法/数据结构辅导班」,全部都是一对一教学:一对一辅导 + 一对一答疑 + 布置作业 + 项目实践 + 永久学习。QQ在线,随时响应!
编程帮,一个分享编程知识的公众号。跟着站长一起学习,每天都有进步。
通俗易懂,深入浅出,一篇文章只讲一个知识点。
文章不深奥,不需要钻研,在公交、在地铁、在厕所都可以阅读,随时随地涨姿势。
文章不涉及代码,不烧脑细胞,人人都可以学习。
当你决定关注「编程帮」,你已然超越了90%的程序员!
编程帮二维码
微信扫描二维码关注
<上一节下一节>关注我们:微信公众号新浪微博
Linux教程
Linux简介
Linux文件管理
Linux目录
Linux文件权限和访问模式
Linux环境变量
Linux打印文件和发送邮件
Linux管道和过滤器
Linux进程管理
Linux网络通信工具
vi编辑器
Linux文件系统
Linux文件存储结构
Linux用户管理
Linux系统性能分析
Linux系统日志及日志分析
Linux信号机制与信号处理
关于网站 |关于站长 |如何完成一部教程 |联系我们 |网站地图 |手机版网站
精美而实用的网站,关注编程技术,追求极致,让您轻松愉快的学习。
Copyright ©2011-2015 biancheng.net, All Rights Reserved,陕ICP备15000209号
底部Logo
本文由 admin 创作,采用 知识共享署名4.0 国际许可协议进行许可
本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名
最后编辑时间为: Apr 27, 2022 at 02:22 am