Merge pull request #7435 from ljgibbslf/master

翻译完成 work with vi editor
This commit is contained in:
Xingyu.Wang 2018-01-28 21:38:39 +08:00 committed by GitHub
commit 6d9956506e
2 changed files with 153 additions and 140 deletions

View File

@ -1,140 +0,0 @@
translating by ljgibbslf
Working with VI editor : The Basics
======
VI editor is a powerful command line based text editor that was originally created for Unix but has since been ported to various Unix & Linux distributions. In Linux there exists another, advanced version of VI editor called VIM (also known as VI IMproved ). VIM only adds funtionalities to already powefrul VI editor, some of the added functionalities a
* Support for many more Linux distributions,
* Support for various coding languages like python, c++, perl etc with features like code folding , code highlighting etc
* Can be used to edit files over network protocols like ssh and http,
* Support to edit files inside a compressed archive,
* Allows screen to split for editing multiple files.
Now let's discuss the commands/options that we can use with VI/VIM. For the purposes of this tutorial, we are going to use VI as an example but all the commands with VI can be used with VIM as well. But firstly we will start out with the two modes of VI text editor,
### Command Mode
This mode allows to handle tasks like saving files, executing a command within vi, copy/cut/paste operations, & tasks like finding/replacing. When in Insert mode, we can press escape to exit into command mode.
### Insert Mode
It's where we insert text into the file. To get into insert mode, we will press 'i' in command line mode.
### Creating a file
In order to create a file, use
```
$ vi filename
```
Once the file is created & opened, we will enter into what's called a command mode & to enter text into the file, we need to use insert mode. Let's learn in brief about these two modes,
### Exit out of Vi
To exit out of Vi from insert mode, we will first press 'Esc' key to exit into command mode & here we can perform following tasks to exit out of vi,
1. Exit without saving file- to exit out of vi command mode without saving of file, type : `:q!`
2. Save file & exit - To save a file & exit, type: `:wq`
Now let's discuss the commands/options that can be used in command mode.
### Cursor movement
Use the keys mentioned below to manipulate the cursor position
1. **k** moves cursor one line up
2. **j ** moves cursor one line down
3. **h ** moves cursor to left one character postion
4. **i** moves cursor to right one character position
**Note :** If want to move multiple line up or down or left or right, we can use 4k or 5j, which will move cursor 4 lines up or 5 characters right respectively.
5. **0** cursor will be at begining of the line
6. **$** cursor will be at the end of a line
7. ** nG** moves to nth line of the file
8. **G** moves to last line of the file
9. **{ ** moves a paragraph back
10. **}** moves a paragraph forward
There are several other options that can be used to manage the cursor movement but these should get the work done for you.
### Editing files
Now we will learn the options that can be used in command mode to change our mode to Insert mode for editing the files
1. **i** Inserts text before the current cursor location
2. **I** Inserts text at the beginning of the current line
3. ** a ** Inserts text after the current cursor location
4. **A ** Inserts text at the end of the current line
5. **o** Creates a new line for text entry below the cursor location
6. ** O** Creates a new line for text entry above the cursor location
### Deleting file text
All of these commands will be excuted from command mode, so if you are in Insert mode exit out to command mode using the 'Esc' key
1. **dd** will delete the complete line of the cursor, can use a number like 2dd to delete next 2 lines after the cursor
2. **d$** deletes from cursor position till end of the line
3. **d^** deletes from cursor position till beginning of line
4. **dw** deletes from cursor to next word
### Copy & paste commands
1. **yy** to yank or copy the current line. Can be used with a number to copy a number of lines
2. **p** paste the copied lines after cursor position
3. **P** paste the copied lines before the cursor postion
These were some the basic operations that we can use with VI or VIM editor. In our future tutorials, we leanrn to perform some advanced operations with VI/VIM editors. If having any queries or comments, please leave them in the comment box below.
--------------------------------------------------------------------------------
via: http://linuxtechlab.com/working-vi-editor-basics/
作者:[Shusain][a]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]:http://linuxtechlab.com/author/shsuain/

View File

@ -0,0 +1,153 @@
如何使用 VI 编辑器:基础篇
====
VI 是一个基于命令行,功能强大的文本编辑器,最早为 Unix 系统开发,后来也被移植到许多的 Unix 和 Linux 发行版上。
在 linux 上还存在着另一个VI编辑器的高阶版本 —— VIM也被称作 VI IMproved。VIM 在 VI 已经很强的功能上添加了更多的功能,这些功能有:
- 支持更多 Linux 发行版。
- 支持多种编程语言,包括 python,c++,perl 等语言的代码块折叠,语法高亮。
- 支持通过多种网络协议,包括 httpssh等支持在压缩格式下编辑文件
- 支持分屏同时编辑多个文件
接下里我们来讨论 VI/VIM 的命令以及选项。本文出于教学的目的,我们使用 VI 来举例,但所有的命令都可以被用于 VIM。首先我们先介绍 VI 编辑器的两种模式。
命令模式
----
命令模式下,我们可以保存文件,在 VI 内运行命令,复制/剪切/粘贴操作,以及查找/替换等任务。当我们处于输入模式时,我们可以按下 escapeEsc键返回命令模式
输入模式
----
在输入模式下,我们可以键入文件内容。在命令模式下按下 i 进入输入模式
我们可以通过下述命令建立一个文件(如果该文件存在,则编辑已有文件)
$ vi filename
一旦该文件被创立或者打开,我们首先进入命令模式,我们需要进入输入模式以在文件中输入内容。我们通过前文已经大致上了解这两种模式。
退出 Vi
-----
如果是想从输入模式中退出,我们首先需要按下 'ESC' 进入命令模式。接下来我们可以根据不同的需要分别使用两种命令退出 Vi
- 不保存退出 - 在命令模式中输入 :q!
- 保存并退出 - 在命令模式中输入 :wq
移动光标
----
下面我们来讨论下那些在命令模式中移动光标的命令和选项
- k 将光标上移一行
- j 将光标下移一行
- h 将光标左移一个字母
- i 将光标右移一个字母
注意:如果你想通过一个命令上移下移多行,或者左移右移多个字母,你可以使用 4k 或者 5j,这两条命令会分别上移 4 行或者右移 5 个字母。
- 0 将光标移动到该行行首
- $ 将光标移动到该行行尾
- nG 将光标移动到第 n 行
- G 将光标移动到文件的最后一行
- { 将光标移动到上一段
- } 将光标移动到下一段
除此之外还有一些命令可以用于控制光标的移动,但上述列出的这些命令应该就能应付日常工作所需。
编辑文本
----
这部分会列出一些用于命令模式的命令,可以进入插入模式来编辑当前文件
- i 在光标所在行的行首插入内容
- I 在光标所在行的行尾插入内容
- a 在当前光标之前插入内容
- A 在当前光标之后插入内容
- o 在当前光标所在行之前添加一行
- O 在当前光标所在行之后添加一行
删除文本
----
以下的这些命令都只能在命令模式下使用,所以首先需要按下 'ESC' 进入命令模式,如果你正处于插入模式
- dd 删除光标所在的整行内容,可以在 dd 前增加数字,比如 2dd可以删除从光标所在行开始的两行
- d$ 删除从光标所在行开始的所有行
- d^ 删除从文件开始直到光标所在行的所有行
- dw 删除从光标所在位置直到下一个词开始的所有内容
复制黏贴命令
------
- yy 复制当前行在yy前添加数字可以复制多行
- p 在光标之后粘贴复制行
- P 在光标之前粘贴复制行
上述就是本期教程教授的可以在 VI/VIM 编辑器上使用的一些基本命令。在未来的教程中还会继续教授一些更高级的命令。如果有任何疑问和建议,请在下方评论区留言。
via: http://linuxtechlab.com/working-vi-editor-basics/
作者Shusain 译者:[ljgibbslf][1] 校对校对者ID
本文由 LCTT 原创编译Linux中国 荣誉推出
[1]: https://github.com/ljgibbslf