vi编辑器插入一行_方便的VI编辑器入门技巧

vi编辑器插入一行

When I was first introduced to the command line on Unix in the mid-1990’s, I have to admit I was intimidated. I had already been using DOS for some time, but only in a operative sense day to day.

在1990年代中期,当我第一次被引入Unix的命令行时,我不得不承认我被吓倒了。 我已经使用DOS已有一段时间了,但是只是在日常操作上。

Suddenly I was faced with learning to administer an entire system from a shell prompt. In particular, as I began to learn the ins and outs of using the VI editor, things became quite a bit easier to handle.

突然,我面临着从shell提示符学习如何管理整个系统的问题。 特别是,当我开始学习使用VI编辑器的来龙去脉时,事情变得很容易处理。

VI is launched from the command line and can be used by privileged and non-privileged users alike. In the web development arena, using a screen-based editor like VI can be handy for editing httpd.conf files, modifying firewall rules, quick edits to configuration files and of course work with html or scripting pages such as Perl, PHP and more.

VI是从命令行启动的,特权用户和非特权用户均可使用。 在Web开发领域,使用基于屏幕的编辑器(如VI)可以方便地编辑httpd.conf文件,修改防火墙规则,快速编辑配置文件,当然还可以使用html或脚本页面(例如Perl,PHP等)。

Some have found VI to be more effective for their work then visual editors. I tend to use both, however, VI is fully functional and can be used for the complete editing process, from creating new files to later edits. For advanced files that will need extensive editing, I still fall back on my BBEdit text editor on OS X. However, having the Terminal a click away for local or remote editing is handy!

有些人发现VI比视觉编辑器对他们的工作更有效。 我倾向于同时使用两者,但是,VI具有全部功能,可以用于完整的编辑过程,从创建新文件到以后的编辑。 对于需要大量编辑的高级文件,我仍然使用OS X上的BBEdit文本编辑器。但是,只需单击一下终端即可进行本地或远程编辑,这非常方便!

VI has two modes, command and insert. Command mode alows for command input to move around a file and search text. Insert mode allows complete editing control and inputs keyboard entry into the file. I have included a few of my most used commands when editing files.

VI有两种模式,命令和插入。 命令模式允许命令输入在文件中移动并搜索文本。 插入模式允许完全的编辑控制并将键盘输入输入文件。 编辑文件时,我包含了一些最常用的命令。

Let’s look at httpd.conf for an example. Since this is a restricted file, if you open httpd.conf as a non-privileged user, it will be in read-only mode. So for edits, you must be privileged as root (or whatever user you have set for this purpose).

让我们看一下httpd.conf的示例。 由于这是一个受限制的文件,因此如果您以非特权用户身份打开httpd.conf,它将处于只读模式。 因此,对于编辑,您必须具有root用户(或为此目的设置的任何用户)特权。

To open the file, issuing ‘vi /etc/httpd/conf/httpd.conf’ will do the trick. Once in the file, there are a few tools to use.

要打开文件,请发出'vi /etc/httpd/conf/httpd.conf'。 进入文件后,有一些工具可以使用。

In command mode, typing enters commands. For example, using / tells VI to expect to search for something. So to find all entries in the file related to SSL dynamic loadable modules, I could enter ‘/HAVE_SSL’ and press enter.

在命令方式下,键入输入命令。 例如,使用/告诉VI期望搜索某些内容。 因此,要查找文件中与SSL动态可加载模块相关的所有条目,我可以输入'/ HAVE_SSL'并按Enter键。

VI goes to the first hit for this search term. To move to the next entry and on through the file, simply press the N key (for next).

VI转到该搜索词的第一命中。 要移至下一个条目并继续浏览文件,只需按N键(代表下一个)。

To move to a specific line number, I can use the colon character and enter a number, such as :900 — which goes to line 900 of the file. This is very useful when chasing down errors that reference the line number in a file.

要移至特定的行号,我可以使用冒号字符并输入一个数字,例如:900-转到文件的第900行。 当追查引用文件中行号的错误时,这非常有用。

There are some handy commands for deleting text if you need to remove strings (words), lines or paragraphs. Here are some common deletion keystrokes:

如果需要删除字符串(单词),行或段落,可以使用一些方便的命令来删除文本。 以下是一些常见的删除按键操作:

  • delete current word –> dw

    删除当前单词–> dw
  • delete previous word –> db

    删除上一个单词–> db
  • delete entire line –> dd

    删除整行–> dd
  • delete paragraph –> d}

    删除段落–> d}

One of the advantages to deletion, is you can use it to cut and paste text. For instance, if you had deleted a paragraph which you wanted move from line 10 to line 1500 of a file, you could do the following:

删除的优点之一是可以使用它剪切和粘贴文本。 例如,如果您删除了一个想要从文件的第10行移至第1500行的段落,则可以执行以下操作:

Position your cursor in front of the paragraph, and type d}. The text disappears. Then type in :1500, which moves you to line 1500 in the file. Simply typing “p” will paste this deleted text in the new location.

将光标置于段落的前面,然后键入d}。 文本消失。 然后输入:1500,这会将您移至文件的1500行。 只需键入“ p”,就会将此删除的文本粘贴到新位置。

If changes are necessary, such as adding a new virtual server entry in the httpd.conf file, one needs to move to insert mode. This is accomplished by either pressing the Insert key or the I key on the keyboard.

如果需要更改,例如在httpd.conf文件中添加新的虚拟服务器条目,则需要切换到插入模式。 这可以通过按键盘上的插入键或I键来完成。

Now any keyboard entry is added to the file, so be careful. You can move around the file with the arrow keys, type input into the file and use the delete or backspace keys as in a visual text editor. To exit insert mode after your changes are made, press the escape key (esc) and you are back in command mode.

现在,任何键盘输入都已添加到文件中,因此请小心。 您可以使用箭头键在文件中四处移动,在文件中键入input,然后像在可视文本编辑器中一样使用Delete或Backspace键。 要在更改后退出插入模式,请按退出键(esc),然后返回命令模式。

If you want to save your changes and exit, type :wq and press enter. To exit without saving changes (perhaps a mistake was made), press :q!, which overrides edits made in insert mode. Finally, to simply quit having not made any changes, use :q.

如果要保存更改并退出,请输入:wq并按Enter。 要退出而不保存更改(可能是一个错误),请按:q !,它会覆盖在插入模式下所做的编辑。 最后,要退出而不做任何更改,请使用:q。

翻译自: https://www.sitepoint.com/handy-vi-editor-tips-for-the-beginner/

vi编辑器插入一行

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值