`
jimlaren
  • 浏览: 42233 次
文章分类
社区版块
存档分类
最新评论
阅读更多

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用户手册中文版7.3.pdf.zip_vim 7

    使用 Vim 更多的是一种技能而 不是一种知识,Vim 的学习更需要的不是头脑而是双手,经常按书中的指示 进行示例性的操作,在学习 Vim 众多精致的技巧时,不要贪图一下子全都 掌握,最好是看一条技巧后,马上在编辑器...

    vim设置20130906, 适合编程爱好者

    "source ~/.vim/bundle/unite.vim/conf.vim "neosnippet.vim source ~/.vim/bundle/neosnippet/conf.vim "vimshell.vim, vim中使用shell, 最好一款 source ~/.vim/bundle/vimshell/conf.vim "omnicppcomplete.vim...

    Mastering Vim Build a software development environment with Vim and Neovim

    Mastering Vim: Build a software development environment with Vim and Neovim By 作者: Ruslan Osipov ISBN-10 书号: 1789341094 ISBN-13 书号: 9781789341096 出版日期: 2018-11-30 pages 页数: (330) Vim is ...

    vim8.0.zip --Linux-CentOS7离线安装VIM及相关依赖包

    Linux-CentOS7离线安装VIM及相关依赖包 vim-8.0.zip gpm-libs-1.20.7-6.el7.i686.rpm ncurses-5.9-14.20130511.el7_4.x86_64.rpm ncurses-base-5.9-14.20130511.el7_4.noarch.rpm ncurses-devel-5.9-14.20130511.el7...

    简明 Vim 练级攻略

    下面的文章翻译自《Learn Vim Progressively》 ——————————正文开始—————————— 你想以最快的速度学习人类史上最好的文本编辑器VIM吗?你先得懂得如何在VIM幸存下来,然后一点一点地学习各种戏法...

    Vim的auto-pairs插件

    auto-pairs是vim的一个极小的插件。它对于用vim来编写程序,提供了极大的便。我ubuntu16中,用vim编写c和c++程序,安装了此插件,极大地方便了代码的输入。 二、功能介绍: 它可以自动完成大括号、小括号、中括号、...

    vimconfig-vim配置脚本

    vim自动提示等多种功能 先安装ctags 然后进入./vim文件中执行 sh vim_config_ctags-v2.0.sh 再进入./vimconfig中执行 解压缩压缩报 tar jxvf vimconfig.tar.bz2 然后执行 ./config.sh 脚本会自动更新 用户根目录下...

    学习vi和vim编辑器_第7版_中文+英文_PDF

    《学习vi和Vim编辑器(中文版第7版)》将说明使用这两种程序编辑文档的基础技巧,并讨论高级工具,例如交互式宏与扩展编辑器的脚本——我们的内容编写成容易遵循步骤操作的风格,成就本书的经典地位。读者将学到: ·...

    vim plugin vim changes to ide update

    add wintagexplorer.vim , echofunc.vim , vimrc

    Vim实用技巧.pdf_vim实用_vim_

    讲述 vim 编辑器的几十个实用上手技巧

    vim-configure.tar.gz_vim_vim_configure

    vim 配置文件配置,自动提示,补全,高亮等.

    Vim实用技巧电子书

    vim是一款功能丰富而强大的文本编辑器,其代码补全、编译及错误跳转等方便编程的功能特别丰富,在程序员中得到非常广泛的使用。vim能够大大提高程序员的工作效率。对于vim高手来说,vim能以与思考同步的速度编辑文本...

    Vim-IDE.zip

    基于win32版本vim82搭建IDE,解压文件到自定义目录下即可,并设置系统环境变量VIM 值为该目录。并自带以下插件 Plugin 'VundleVim/Vundle.vim' Plugin 'Valloric/YouCompleteMe' "代码补齐 Plugin 'SirVer/ultisnips...

    开发工具vim73

    vim

    vim使用的详细教程

    VIM (Unix及类Unix系统文本编辑器) Vim是一个类似于Vi的著名的功能强大、高度可定制的文本编辑器,在Vi的基础上改进和增加了很多特性。 VIM是自由软件。 Vim普遍被推崇为类Vi编辑器中最好的一个,事实上真正的劲敌...

    VIM快捷键 VIM插件

    VIM快捷键 VIM快捷键 VIM插件 VIM快捷键 VIM插件

    vim.tar.gz

    sudo apt-get remove vim-tiny sudo apt-get install vim # 调试插件 pip install -i https://pypi.tuna.tsinghua.edu.cn/simple ipdb git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle...

    VIM 使用手册.pdf

    VIM 简介 VIM 是 Unix 世界里极为普遍的全萤幕文书编辑器,几乎可以说任何一台 Unix 机器都会提供这套软体。 熟悉 DOS 下的文书处理後,也许会感到 VIM 并不好用;Unix 上也已经发展出许多更新、更好用的文 书编辑器...

    vim离线安装rpm包

    解压上传后执行rpm -ivh *.rpm --force --nodeps即可安装vim成功,内置所有vim所需的rpm依赖包

    VIMIM-VIM中文输入法

    VimIM 是专为 Vim 玩家开发的插件,可作为 Vim 内嵌输入平台。  只要您的 Vim 能显示中文,您就能用 Vim 输入搜索中文。  VimIM 把 Vim 的中文编辑潜能,提升到一个全新的高度。 官方网址: ...

Global site tag (gtag.js) - Google Analytics