`
ubuntu
  • 浏览: 107722 次
  • 来自: ...
文章分类
社区版块
存档分类
最新评论

vim使用指南

阅读更多

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使用指南

    ### Vim使用指南及基础知识 #### 一、简介 Vim是一款功能强大的文本编辑器,它在程序员和技术人员中非常受欢迎。Vim具有高度可定制性,并且提供了丰富的功能集,如代码高亮、语法检查、多级撤销机制、宏记录等。本...

    VIM使用指南_推荐

    ### VIM使用指南知识点 #### 1. 引言与目标 VIM,全称Vi IMproved,是一种功能强大且广泛使用的文本编辑器,尤其在Linux/UNIX平台中备受推崇。该指南旨在通过一种独特的“清单式学习”方法,帮助读者在短时间内掌握...

    VIM使用指南.rar

    本使用指南将带你深入了解VIM的基本操作和高级技巧,帮助你成为VIM的熟练使用者。 一、VIM启动与退出 要启动VIM,只需在命令行输入`vim`加上你要编辑的文件名。退出VIM有多种方式:在正常模式下,输入`:q`退出当前...

    vim 、vi使用指南

    vi使用指南 vim使用指南,让你轻松学会vi

    Vim完整中文手册.pdf

    Vim 中文手册 Vim 是一个功能强大且灵活的文本编辑器,它的使用方法和命令非常多样化,这使得新手用户感到困惑和难以学习...本手册包含了 Vim 的所有基本命令、功能和使用方法,为用户提供了一个全面的 Vim 使用指南。

    vim完整中文手册-上

    本手册旨在为用户提供全面且深入的VIM使用指南,包括基本操作、编辑技巧、高级定制等多个方面。 **快速参考:** - **移动操作:** - 使用光标键进行上下左右移动。 - 使用 `h` 向左移动。 - 使用 `j` 向下移动...

    vim指令使用指南

    最全的vim使用指令,帮助你更好的在Linux中使用vim来开发项目

    ubuntu_vim命令.pdf

    ### Ubuntu下VIM使用指南 #### 一、VIM简介 VIM (Vi IMproved) 是一款基于 Vi 的增强版文本编辑器,广泛应用于 Linux 和 Unix 系统中。它以其强大的功能、高度的可定制性和高效性而受到开发者的喜爱。本文档旨在...

    Vim入门到精通 适合0基础学习

    * Vim官方文档:提供了详细的Vim使用指南和配置选项介绍。 * Vim社区:提供了Vim社区讨论平台,可以讨论Vim相关问题。 * Vim插件库:提供了多种Vim插件,可以快速安装和管理插件。 Vim是一个功能强大且灵活的文本...

    VIM 编辑器操作指南.doc

    要使用Vim编辑器,你可以通过命令行输入`vim`,后面跟上你想要编辑的文件名。例如,`vim myfile.txt`将打开名为myfile.txt的文件。此外,Vim还支持多种启动参数,比如: - `-v`或`--vi`:启动Vi模式。 - `-e`:启动...

    VIM中文文档PDF有目录

    VIM中文文档PDF有目录,这意味着这份资源为中国的VIM使用者提供了一份方便查阅的手册,其中包含了详细的VIM使用指南和功能介绍,且附带目录,使得用户可以快速定位到所需信息。 在VIM中文用户手册中,你可以期待...

    ubuntuvim命令.pdf

    ### Ubuntu下VIM使用指南知识点总结 #### 一、VIM简介及基本操作 ##### VIM简介 VIM(Vi IMproved)是一款基于经典编辑器Vi改进而来的文本编辑工具,广泛应用于各种Linux发行版中,包括Ubuntu。VIM以其强大的功能、...

    Vim User Manual (for printing)

    - **手册版本与目的**:本手册为用户提供了详尽的 Vim 使用指南,旨在帮助用户熟悉 Vim 的各项功能及操作技巧。 - **手册排版特点**:该手册经过精心排版,适合正反面打印,仅需 32 张纸即可完整打印。 #### 二、...

Global site tag (gtag.js) - Google Analytics