![See all Ubuntu Linux related FAQ](https://www.alaica.com/wp-content/uploads/linux-36.jpg)
我最近从 macOS/OS X 切换到 Ubuntu。当我在终端运行“vim file.c”命令时,我看到一条错误消息,表明未安装 vim。如何修复 vim 未找到错误以及为什么 vim 不是 Ubuntu Linux 上的默认文本编辑器?如何在 Ubuntu Linux 中安装成熟的 vim 文本编辑器?
Vim (Vi IMproved) 是一个向上兼容 Vi 的文本编辑器。它可以用来编辑各种纯文本。它对于编辑配置文件和用 shell、python、perl、c/c++ 等编写的程序特别有用。在本教程中,我们将向您展示在 Ubuntu Linux 上安装 vim 文本编辑器。
教程详情 | |
---|---|
难度级别 | 简单的 |
根权限 | 是的 |
要求 | Linux终端 |
类别 | 文本编辑器 |
操作系统兼容性 | Debian • Mint • Pop!_OS • Ubuntu |
美东时间。阅读时间 | 7分钟 |
Ubuntu Linux 使用 apt 安装 vim
程序如下:
- 打开终端应用程序。您也可以按CTRL+ ALT+T键盘快捷键
- sudo apt update通过键入命令更新包数据库
- 搜索vim包运行:sudo apt search vim
- 在 Ubuntu Linux 上安装 vim,输入:sudo apt install vim
- 通过输入vim --version命令验证 vim 安装
让我们详细看看在 Ubuntu 系统上安装 vim 的所有步骤。
如何在 Ubuntu 上安装完整版 Vim
首先打开终端应用程序,然后使用apt 命令或apt-get 命令更新包数据库:
$ sudo apt update
输出:
点击:1 http://us-central1.gce.archive.ubuntu.com/ubuntu 焦点 InRelease 点击:2 http://us-central1.gce.archive.ubuntu.com/ubuntu focus-updates InRelease 点击:3 http://us-central1.gce.archive.ubuntu.com/ubuntu focus-backports InRelease 点击:4 http://security.ubuntu.com/ubuntu focus-security InRelease 点击:5 http://archive.canonical.com/ubuntu 焦点 InRelease 正在阅读包裹清单...完成 构建依赖树 读取状态信息...完成 所有软件包都是最新的。
在 Ubuntu Linux 上应用所有补丁。例如:
$ sudo apt upgrade
搜索vim包
运行以下任一命令:
这是我在Ubuntu Linux 版本20.04/22.04 LTS 上看到的内容:
$ apt search vim
$ apt-cache search ^vim
# filter outputs using grep command #
$ apt-cache search vim | grep editor
vim - Vi IMproved - 增强的 vi 编辑器 vim-tiny - Vi IMproved - 增强的 vi 编辑器 - 紧凑版 elvis-tiny - 适用于基本系统的 Tiny vi 兼容编辑器 formiko - reStructuredText 和 MarkDown 编辑器和实时预览器 kakoune - 受 Vim 启发、面向选择的代码编辑器 libghc-yi-keymap-vim-dev - Yi 编辑器的 Vim 键盘映射 libghc-yi-keymap-vim-doc - Yi 编辑器的 Vim 键盘映射;文档 libghc-yi-keymap-vim-prof - Yi 编辑器的 Vim 键盘映射;分析库 python3-editor - 以编程方式打开编辑器,捕获结果 - Python 3.x vim-addon-manager - Vim 编辑器的插件管理器 vim-athena - Vi IMproved - 增强的 vi 编辑器 - 带有 Athena GUI vim-editorconfig - Vim 的 EditorConfig 插件 vim-gtk - Vi IMproved - 增强的 vi 编辑器(虚拟包) vim-gtk3 - Vi IMproved - 增强的 vi 编辑器 - 带有 GTK3 GUI vim-nox - Vi IMproved - 增强的 vi 编辑器 - 支持脚本语言 vis - 现代、无遗留、简单而高效的类似 vim 的编辑器
您不必安装上述 vim 软件包。通常我们在 Ubuntu Linux 上安装 vim 或 vim-tiny。
获取有关您选择的 vim 包的信息
按如下方式使用 apt:示例输出:
$ apt show vim-tiny
$ apt show vim
软件包:vim 版本:2:8.1.2269-1ubuntu5.11 优先级:可选 Section: editors Origin: Ubuntu Maintainer: Ubuntu DevelopersOriginal-Maintainer: Debian Vim Maintainers Bugs: https://bugs.launchpad.net/ubuntu/+filebug Installed-Size: 3,112 kB Provides: editor Depends: vim-common (= 2:8.1.2269-1ubuntu5.11), vim-runtime (= 2:8.1.2269-1ubuntu5.11), libacl1 (>= 2.2.23), libc6 (>= 2.29), libcanberra0 (>= 0.2), libgpm2 (>= 1.20.7), libpython3.8 (>= 3.8.2), libselinux1 (>= 1.32), libtinfo6 (>= 6) Suggests: ctags, vim-doc, vim-scripts Homepage: https://www.vim.org/ Task: server, cloud-image, lubuntu-desktop Download-Size: 1,240 kB APT-Manual-Installed: yes APT-Sources: http://in.archive.ubuntu.com/ubuntu focal-updates/main amd64 Packages Description: Vi IMproved - enhanced vi editor Vim is an almost compatible version of the UNIX editor Vi. . Many new features have been added: multi level undo, syntax highlighting, command line history, on-line help, filename completion, block operations, folding, Unicode support, etc. . This package contains a version of vim compiled with a rather standard set of features. This package does not provide a GUI version of Vim. See the other vim-* packages if you need more (or less). Package: vim Version: 2:8.1.2269-1ubuntu5 Priority: optional Section: editors Origin: Ubuntu Maintainer: Ubuntu Developers Original-Maintainer: Debian Vim Maintainers Bugs: https://bugs.launchpad.net/ubuntu/+filebug Installed-Size: 3,111 kB Provides: editor Depends: vim-common (= 2:8.1.2269-1ubuntu5), vim-runtime (= 2:8.1.2269-1ubuntu5), libacl1 (>= 2.2.23), libc6 (>= 2.29), libcanberra0 (>= 0.2), libgpm2 (>= 1.20.7), libpython3.8 (>= 3.8.2), libselinux1 (>= 1.32), libtinfo6 (>= 6) Suggests: ctags, vim-doc, vim-scripts Homepage: https://www.vim.org/ Task: server, cloud-image, lubuntu-desktop Download-Size: 1,238 kB APT-Sources: http://in.archive.ubuntu.com/ubuntu focal/main amd64 Packages Description: Vi IMproved - enhanced vi editor Vim is an almost compatible version of the UNIX editor Vi. . Many new features have been added: multi level undo, syntax highlighting, command line history, on-line help, filename completion, block operations, folding, Unicode support, etc. . This package contains a version of vim compiled with a rather standard set of features. This package does not provide a GUI version of Vim. See the other vim-* packages if you need more (or less).
Installing vim text editor on Ubuntu
Open a terminal and/or login to the remote Ubuntu server using the ssh client. Type the following apt-get command to install vim text editor (when promoted type your own password):
$ sudo apt-get update
$ sudo apt-get install vim
OR
$ sudo -s
$ apt-get update
$ apt-get install vim
Sample outputs:
Reading package lists... Done Building dependency tree Reading state information... Done The following additional packages will be installed: libasound2 libasound2-data libcanberra0 libltdl7 libogg0 libpython3.8 libtdb1 libvorbis0a libvorbisfile3 vim-common vim-runtime Suggested packages: libasound2-plugins alsa-utils libcanberra-gtk0 libcanberra-pulse ctags vim-doc vim-scripts The following NEW packages will be installed: libasound2 libasound2-data libcanberra0 libltdl7 libogg0 libpython3.8 libtdb1 libvorbis0a libvorbisfile3 vim vim-common vim-runtime 0 upgraded, 12 newly installed, 0 to remove and 0 not upgraded. Need to get 9412 kB of archives. After this operation, 42.9 MB of additional disk space will be used. Do you want to continue? [Y/n] y Get:1 http://us-central1.gce.archive.ubuntu.com/ubuntu focal/main amd64 vim-common all 2:8.1.2269-1ubuntu5 [85.1 kB] Get:2 http://us-central1.gce.archive.ubuntu.com/ubuntu focal/main amd64 libasound2-data all 1.2.2-2.1 [19.0 kB] .... .. ... Unpacking libpython3.8:amd64 (3.8.2-1ubuntu1.1) ... Selecting previously unselected package vim-runtime. Preparing to unpack .../10-vim-runtime_2%3a8.1.2269-1ubuntu5_all.deb ... Adding 'diversion of /usr/share/vim/vim81/doc/help.txt to /usr/share/vim/vim81/doc/help.txt.vim-tiny by vim-runtime' Adding 'diversion of /usr/share/vim/vim81/doc/tags to /usr/share/vim/vim81/doc/tags.vim-tiny by vim-runtime' Unpacking vim-runtime (2:8.1.2269-1ubuntu5) ... Selecting previously unselected package vim. Preparing to unpack .../11-vim_2%3a8.1.2269-1ubuntu5_amd64.deb ... Unpacking vim (2:8.1.2269-1ubuntu5) ... Setting up libogg0:amd64 (1.3.4-0ubuntu1) ... Setting up libtdb1:amd64 (1.4.2-3build1) ... Setting up libasound2-data (1.2.2-2.1) ... Setting up vim-common (2:8.1.2269-1ubuntu5) ... Setting up libpython3.8:amd64 (3.8.2-1ubuntu1.1) ... Setting up libvorbis0a:amd64 (1.3.6-2ubuntu1) ... Setting up libltdl7:amd64 (2.4.6-14) ... Setting up libasound2:amd64 (1.2.2-2.1) ... Setting up vim-runtime (2:8.1.2269-1ubuntu5) ... Setting up libvorbisfile3:amd64 (1.3.6-2ubuntu1) ... Setting up libcanberra0:amd64 (0.30-7ubuntu1)
Verify vim installation
Run the following command to see vim version:
$ vim --version
How do I edit file with vim
You can now use vim text editor. In this example, create a new file named file.c:
$ vim file.c
$ sudo vim /etc/sysctl.conf
Sample outputs:
![Fig.01: Vim in action on Ubuntu](https://www.alaica.com/wp-content/uploads/linux-129.png)
Fig.01: Vim in action on Ubuntu
The two modes of VIM
The VIM (and vi) text editor has two modes:
- Command mode : The command mode allows the entry of commands to manipulate text. VIM always starts in command mode. You can always press the Esc key to get back to command mode.
- Insert mode : This mode allows you to edit text or programs. Several commands put the VIM editor into insert mode. The most commonly used commands to get into insert mode are a and i . Once in insert mode, you get out of it by hitting the Esc key.
How to add/append/edit text
- Press Esc key
- Type i to insert text at the cursor position.
- Or type a to add text after the current cursor position.
How to exit from vim
Switch to command mode by hitting the Esc key. Next, you can type the following commands:
- :q to quit (press Esc, type : and q and press the Enter key).
- :q! to quit without saving data/file.
save and quit.
- :qa to quit all open files.
See how to save and exit vim tutorials:
- How To Exit Vim Text Editor Command
- How To: Vi / Vim Save And Quit The Editor Command
- Vi / Vim Quit Without Saving Command
- Recommended for new users ~ Mastering the vim text editor in Linux or Unix { download full PDF version }
Conclusion
In this tutorial, you learned how to install vim text editor on Ubuntu Linux using the apt-get command or apt command. For more information read the man page and see vim page here.