When you need to edit a configuration file from the command line in a pinch, use these tips for the ins and (especially) outs of vim.
If one thing is for sure about Linux, it's that it has no shortage of text editors. This reflects the largely text-based nature of Linux, from the command line, to the source code, to the configuration files that programs refer to. When you are in a desktop environment, you can use one of many graphical tools to edit text files; however, in an emergency situation, or when logged into a machine remotely, you may need to fall back on a command-line text editor. Under Ubuntu, the default text editor is vim, and this hack provides you with the basic information you need to make changes to configuration files using vim.
The vi editor has a rather mixed reputation. Some people love how quickly you can edit files with it, and others hate its steep learning curve. The vim editor (short for "Vi IMproved") takes vi and adds not only a number of powerful improvements, but also a number of user-friendly changes such as a complete, integrated help environment. Ubuntu includes vim by default, and even if you run vi, you are actually running vim in a vi-like emulation mode.
So, to edit a file, run vim on the command line followed by the filename as an argument. For instance, to edit the filesystem table (/etc/fstab), type:
$ sudo vim /etc/fstab
Unlike most other editors you may be used to, vim operates via a number of modes. Different keys perform different functions depending on which mode you are in. (This can be rather confusing the first time you use vim, since you will start typing only to see all sorts of strange and undesirable behavior.) For starters, there are two main modes you will be operating in: Normal mode (for navigating the file) and Insert mode (for editing text).
Navigate Through the File
Normal mode is the main mode you default to when you start vim. In this mode, you use a number of different keys to move through the file. Table 8-1 lists some of the main navigation keys and their functions.
Table VIM Normal Mode keybindings
Navigation key
nction
h, j, k, l |
Move left, up, down, or right, respectively. The arrow keys will also move the cursor. |
w, b |
Move forward or backword one word at a time, respectively. Useful when you want to skip through a file quickly. |
^, $ |
Move to the beginning or the end of the current line, respectively. |
gg, G |
Move to the beginning or the end of the file, respectively. |
: number |
Move to the specified line number in the file. |
/ keyword |
Search for the specified keyword; hit n to move to the next keyword. |
Edit a Line
Once you have moved to where you wish to edit, you have a number of different options to edit a line. If you simply need to delete some text, the x key will delete the character under the current cursor. To add or change text, you will need to switch to Insert mode. Once in Insert mode, vim behaves a lot like other text editors; when you type a particular key, it will appear in the file. There are a number of ways to get into Insert mode, depending on where you want to start editing, but the simplest is to hit i, which will let you edit at the current position of the cursor.
Once in Insert mode, you can make any changes you need to a particular file. You can hit the Backspace or Delete key to delete characters and can still use the arrow keys to move around in the file. Once you are finished, hit the Esc key to exit Insert mode.
Copy and Paste
Sometimes when editing a file it is useful to copy and paste one line or a section of a line to another line. There are a number of ways to do so, some faster than others, but for new vim users, one of the easiest ways is through vim's Visual mode. To select a portion of text, type Esc until you are in Normal mode, move to the beginning of the text you want to copy, and then hit the v key to switch to Visual mode. Now move the cursor as normal, and you will notice that characters will be selected as it moves. When you have selected all of the characters you want, hit the y key to copy (or yank) the text. Then move to where you want to paste it and hit p.
|
If you know you want to copy full lines at a time, enter Visual mode with capital V instead of v. Now move up or down to select a number of lines and type y to copy and then p to paste your lines.
|
|
Undo
Once nice feature that vim has that vi doesn't is unlimited undos. If you make a mistake and want to undo it, hit Esc until you are in Normal mode, and then simply type the u key to step back through each edit. If you find you need to redo a change, type Ctrl-R.
Save and Exit
After you have made your changes and are ready to save, you need to switch to Command mode. Command mode lets you run special vim commands, including the commands you need to save changes and exit. To save (or write) your changes, type :w and press Enter. When you are ready to quit, type :q and press Enter. You can also combine these two commands into a single command, and type :wq and press Enter.
Search and Replace
A common need when editing files is to substitute one word for another throughout the file. To do so in vim, type:
:s/word/replacement/g
to change all instances of the word on a particular line. To change all instances of the word within a file, type:
:%s/word/replacement/g
If you want to give confirmation before making each change, add a c to the end of the command:
:%s/word/replacement/gc
and vim will ask you before it makes any changes.
One particularly handy use of the search-and-replace feature when editing configuration files is for commenting or uncommenting multiple lines in the file. To do so, enter Visual mode, select each line you want to comment, and then type:
:s/^/#/
to comment the lines out with the # character. Replace # with whatever character that file uses for comments. If you instead want to uncomment a number of lines, select them in Visual mode and then type:
:s/^#//
or replace # with the character your file uses for comments.
Help
If you find you are lost, or you just want some help with vim's options, type:
:help
to enter vim's help screen. You can also follow :help with a particular keyword, so if you want to find out what the w key does, for instance, type:
:help w
Where to Learn More
This hack is by no means an exhaustive tutorial on how to use vim. vim has a number of advanced and powerful features that, once learned, allow you to make rapid changes to files. If you want to know more about vim's features and how to use them, type:
$ vimtutor
at the command line to try vim's built-in interactive tutorial program.
分享到:
相关推荐
"vim-configure.tar.gz_vim_vim_configure" 是一个包含了 Vim 配置文件的压缩包,旨在帮助用户优化 Vim 的使用体验,提供自动提示、补全功能以及代码高亮等特性。 在 Vim 中配置这些功能,可以使编辑效率大幅提升。...
### Ubuntu下升级与安装指定版本Vim的方法 #### 背景介绍 Vim是一款功能强大的文本编辑器,因其高效性和可定制性受到广大开发者的喜爱。在Ubuntu系统中,默认安装的Vim版本可能无法满足某些用户的特定需求,比如...
【vim plugin vim changes to ide update】这一主题表明我们将探讨如何使用Vim插件来提升其作为集成开发环境(IDE)的功能。在这个过程中,我们将重点分析新增的三个插件:wintagexplorer.vim、echofunc.vim以及vimrc...
**Linux中的Vim编辑器** Vim(Vi Improved)是一款强大的文本编辑器,在Linux系统中广泛使用。它基于早期的Vi编辑器,并进行了大量的增强和改进,提供了丰富的特性集,适用于程序员、系统管理员以及普通用户。Vim在...
Vim是一款强大的文本编辑器,深受程序员和系统管理员的喜爱,其可高度自定义的特性使得用户可以根据个人喜好和工作需求定制自己的编辑环境。在Vim中,`.vim`文件夹和`.vimrc`配置文件是两个至关重要的元素,它们帮助...
针对“vim离线安装rpm包”这个主题,我们将详细讲解如何在Linux系统中通过rpm命令来实现vim的离线安装,以及相关的重要知识点。 首先,RPM(Red Hat Package Manager)是Linux下广泛使用的软件包管理器,它负责软件...
使用 Vim 更多的是一种技能而 不是一种知识,Vim 的学习更需要的不是头脑而是双手,经常按书中的指示 进行示例性的操作,在学习 Vim 众多精致的技巧时,不要贪图一下子全都 掌握,最好是看一条技巧后,马上在编辑器...
在Linux系统中,Vim(Vi Improved)是一款强大的文本编辑器,被广泛用于代码编辑、配置文件修改等任务。在没有网络连接的情况下,为CentOS 7安装Vim需要通过手动下载所需软件包并使用本地安装方法。以下是离线安装...
在Ubuntu系统中,Vim(Vi IMproved)是一款强大的文本编辑器,被广泛用于编程和其他文本操作。离线安装Vim对于没有网络连接或者网络不稳定的情况尤为重要。以下是如何在Ubuntu上离线安装Vim的详细步骤,以及相关知识...
Vim是一款非常强大的文本编辑器,它以其高度可配置性和高效编辑能力深受程序员喜爱。在Go语言的开发环境中,Vim可以通过安装特定的插件来增强对Go语言的支持,其中包括语法高亮功能。"vim go 语法高亮"这个主题正是...
由于提供的文件信息中包含了Vim 8.2的PDF用户手册的结构和内容概览,我们可以从提供的内容中提炼出关于Vim编辑器的知识点,这些知识点覆盖了Vim的基本使用、编辑技巧、高级功能以及如何在不同情况下高效使用Vim。...
Vim是一款极其强大的文本编辑器,尤其在程序员和系统管理员中广受欢迎。它以其高度可配置性和丰富的功能而闻名,其中“主题”是提升Vim使用体验的重要元素之一。标题"Vim特别好看的主题全部"暗示了这个压缩包可能...
VIM(Vi IMproved)是一款强大的文本编辑器,尤其在编程领域中被广泛使用,因为它提供了丰富的自定义功能和高效的操作方式。对于PHP开发者来说,配置一个适合自己的VIM环境可以显著提高编码效率。以下是对"VIM FOR ...
离线安装Vim是Linux系统用户在没有网络连接时安装软件的一种常见方法,尤其是在服务器环境或者网络条件有限的场合。本教程将详细介绍如何通过提供的离线资源安装Vim编辑器。 Vim(Vi Improved)是一款强大的文本...
linux下用vim编辑器配合jeffy插件实现比较顺手的编程环境。Jeffy插件和配置文件(.vim):代码高亮 自动提示等。 vim版本必须大于7.1。 1,备份下vim配置文件(可选) 2,解压jeffy-vim.zip 和里面的jeffy-vim-read.tar...
Vim(Vi Improved)是一款强大的文本编辑器,广泛应用于Linux系统中,包括CentOS操作系统。它以其高效、可配置和可扩展性而受到系统管理员和开发者的青睐。本压缩包包含的是Vim的离线安装包,适用于那些无法或者不便...
Vim是一款强大的文本编辑器,深受程序员喜爱,尤其在系统级Verilog编程中,通过搭配合适的插件,可以显著提高代码编写效率。SystemVerilog是一种广泛应用于数字电路设计的硬件描述语言,它允许设计师在高层次上描述...
【标题】"vim_rpm.zip" 提供的是一个包含了 Vim 编辑器的 RPM 包,主要用于在 CentOS 7 的环境中进行离线安装。RPM(Red Hat Package Manager)是基于Linux系统的软件包管理器,它能够方便地安装、升级、查询和删除...
本资源提供的"Vim QT版本源代码"是Vim的一个特殊构建,它集成了QT库,为Vim提供了一个图形用户界面(GUI),使得在桌面环境中使用Vim变得更加方便,尤其对于那些习惯于图形化操作的用户。 首先,我们需要理解Vim的...
Vim 是一款强大的文本编辑器,它以其高度可配置性和高效的工作流而闻名。Vim 插件系统是其灵活性的核心,允许用户通过安装各种插件来扩展编辑器的功能,以适应不同工作场景的需求。"vim-plugin插件" 涵盖了多种提升 ...