有很多种Unix和编辑文件对我来说最好的方法之一是使用面向屏幕的文本编辑器vi。这个编辑器让您编辑在上下文中的其他文件中的行线。
现在你会发现这就是所谓的VIM vi编辑器的改进版本。这里VIM代表ViIM被证明。
被普遍认为是在VI编辑器,因为在Unix事实上的标准:
-
它通常可在Unix系统中的使用。
-
它的实现是非常的全面。
-
它需要很少的资源。
-
这是比任何其他的编辑器,如ed或前更加用户友好。
可以使用vi编辑器来编辑现有的文件或创建一个新的文件从头。您也可以使用这个编辑器只读取一个文本文件。
启动vi编辑器:
有以下方式,你就可以开始使用vi编辑器:
| 命令 | 描述 |
|---|---|
| vi filename | Creates a new file if it already does not exist, otherwise opens existing file. |
| vi -R filename | Opens an existing file in read only mode. |
| view filename | Opens an existing file in read only mode. |
testfile将创建一个新的文件,如果它已经不存在于当前的工作目录下面的例子:
$vi testfile
因此,你会看到类似如下的画面:
| ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ "testfile" [New File]
光标之后你会发现在每行的符号(〜)。一个波浪线表示未使用的线路。如果行不以波浪号开始,似乎是空白,有空格,制表符,换行符,或其他一些存在nonviewable字符。
所以,现在你已经打开了一个文件开始。在继续之前,让我们了解一些微小但重要的概念解释如下。
操作模式:
用vi编辑器工作时,你会遇到以下两种模式:
-
命令模式:此模式可让您执行管理任务,如保存文件,执行命令,将光标移动,切割和粘贴线或字(唬弄),并寻找和更换。在这种模式下,无论你是作为一个命令解释。
-
插入模式:此模式使您可以插入到该文件中的文本。一切的在此模式下输入被解释为输入,最后,它被放置在该文件。
vi的总是开始在命令模式。要输入文字,您必须在插入模式。来插入模式,你只需键入i。要插入模式,按Esc键,就会把你背到命令模式。
提示:如果你不知道你是在哪个模式,按Esc键两次,那么你就可以在命令模式。您使用vi编辑器打开一个文件,并开始键入一些字符,然后在命令模式来理解上的差异。
退出vi:
退出vi的命令是 :q。一旦在命令模式下,键入冒号,和“q”,依次是返回。如果您的文件已经以任何方式修改,编辑器会提醒你这一点,而不是让你戒烟。要忽略此消息,不保存退出vi的命令是:q! 这可以让你退出VI不保存任何的变化。
保存编辑器内容的命令是 :w。您可以结合上面的命令quit命令,或者 :wq和返回。
保存更改并退出vi的最简单的方法是ZZ命令。当你在命令模式下:wq ,它会做相当于输入ZZ。
您可以指定一个不同的文件名保存到指定的名称:W之后。例如,如果你希望你工作,另一名为文件名的文件名保存该文件,您可以键入:w filename2中和返回。尝试一次。
移动在一个文件中:
要左右移动在一个文件中,而不会影响你的文字,您必须在命令模式下(按Esc键两次)。这里有一些你可以用它来走动一次一个字符的命令:
| 命令 | 描述 |
|---|---|
| k | Moves the cursor up one line. |
| j | Moves the cursor down one line. |
| h | Moves the cursor to the left one character position. |
| l | Moves the cursor to the right one character position. |
有以下两个重要点要注意:
-
vi是大小写敏感的,所以你需要使用命令时,要特别注意大小写。
-
大多数命令在vi中可以行动发生的时候,你想由数量开头。例如,2j移动光标两条线下来的光标位置。
还有很多其他的方法来移动在vi一个文件内。请记住,你必须在命令模式(按Esc键两次)。这里更多一些命令,你可以用它来走动文件:
| 命令 | 描述 |
|---|---|
| 0 or | | Positions cursor at beginning of line. |
| $ | Positions cursor at end of line. |
| w | Positions cursor to the next word. |
| b | Positions cursor to previous word. |
| ( | Positions cursor to beginning of current sentence. |
| ) | Positions cursor to beginning of next sentence. |
| E | Move to the end of Blank delimited word |
| { | Move a paragraph back |
| } | Move a paragraph forward |
| [[ | Move a section back |
| ]] | Move a section forward |
| n| | Moves to the column n in the current line |
| 1G | Move to the first line of the file |
| G | Move to the last line of the file |
| nG | Move to nth line of the file |
| :n | Move to nth line of the file |
| fc | Move forward to c |
| Fc | Move back to c |
| H | Move to top of screen |
| nH | Moves to nth line from the top of the screen |
| M | Move to middle of screen |
| L | Move to botton of screen |
| nL | Moves to nth line from the bottom of the screen |
| :x | Colon followed by a number would position the cursor on line number represented by x |
控制命令:
使用命令后,您可以使用控制键:
| 命令 | 描述 |
|---|---|
| CTRL+d | Move forward 1/2 screen |
| CTRL+d | Move forward 1/2 screen |
| CTRL+f | Move forward one full screen |
| CTRL+u | Move backward 1/2 screen |
| CTRL+b | Move backward one full screen |
| CTRL+e | Moves screen up one line |
| CTRL+y | Moves screen down one line |
| CTRL+u | Moves screen up 1/2 page |
| CTRL+d | Moves screen down 1/2 page |
| CTRL+b | Moves screen up one page |
| CTRL+f | Moves screen down one page |
| CTRL+I | Redraws screen |
编辑文件:
要编辑的文件,你需要在插入模式。有很多方法进入插入模式,命令模式:
| Command | 描述 |
|---|---|
| i | Inserts text before current cursor location. |
| I | Inserts text at beginning of current line. |
| a | Inserts text after current cursor location. |
| A | Inserts text at end of current line. |
| o | Creates a new line for text entry below cursor location. |
| O | Creates a new line for text entry above cursor location. |
删除字符:
这里是清单可用于在一个打开的文件中删除字符和线条的重要的命令:
| Command | 描述 |
|---|---|
| x | Deletes the character under the cursor location. |
| X | Deletes the character before the cursor location. |
| dw | Deletes from the current cursor location to the next word. |
| d^ | Deletes from current cursor position to the beginning of the line. |
| d$ | Deletes from current cursor position to the end of the line. |
| D | Deletes from the cursor position to the end of the current line. |
| dd | Deletes the line the cursor is on. |
正如上面所提到的,大多数命令可以在vi行动发生的时候,你想由数量开头。例如,2个删除光标下的位置和2dd删除光标在两行两个字符。
我会强烈建议所有上面的命令,然后再继续正常练习。
更改命令:
你也可以改变在vi中的字符,单词或行而不删除他们。以下是相关的命令:
| Command | 描述 |
|---|---|
| cc | Removes contents of the line, leaving you in insert mode. |
| cw | Changes the word the cursor is on from the cursor to the lowercase w end of the word. |
| r | Replaces the character under the cursor. vi returns to command mode after the replacement is entered. |
| R | Overwrites multiple characters beginning with the character currently under the cursor. You must use Esc to stop the overwriting. |
| s | Replaces the current character with the character you type. Afterward, you are left in insert mode. |
| S | Deletes the line the cursor is on and replaces with new text. After the new text is entered, vi remains in insert mode. |
“复制”和“粘贴”命令:
您可以复制线或从一个地方的话,那么你就可以过去,他们在另一个地方使用下面的命令:
| Command | 描述 |
|---|---|
| yy | Copies the current line. |
| yw | Copies the current word from the character the lowercase w cursor is on until the end of the word. |
| p | Puts the copied text after the cursor. |
| P | Puts the yanked text before the cursor. |
高级的命令:
有一些先进的简化现在的编辑器,以便更有效地利用vi的命令:
| Command | 描述 |
|---|---|
| J | Join the current line with the next one. A count joins that many lines. |
| << | Shifts the current line to the left by one shift width. |
| >> | Shifts the current line to the right by one shift width. |
| ~ | Switch the case of the character under the cursor. |
| ^G | Press CNTRL and G keys at the same time to show the current filename and the status. |
| U | Restore the current line to the state it was in before the cursor entered the line. |
| u | Undo the last change to the file. Typing 'u' again will re-do the change. |
| J | Join the current line with the next one. A count joins that many lines. |
| :f | Displays current position in the file in % and file name, total number of file. |
| :f filename | Renames current file to filename. |
| :w filename | Write to file filename. |
| :e filename | Opens another file with filename. |
| :cd dirname | Changes current working directory to dirname. |
| :e # | Use to toggle between two opened files. |
| :n | In case you open multiple files using vi, use :n to go to next file in the series. |
| :p | In case you open multiple files using vi, use :p to go to previous file in the series. |
| :N | In case you open multiple files using vi, use :N to go to previous file in the series. |
| :r file | Reads file and inserts it after current line |
| :nr file | Reads file and inserts it after line n. |
Word和字符搜索:
vi编辑器有两种类型的搜索字符串和字符。对于一个字符串搜索,/和?命令的使用。当您启动这些命令,刚刚键入的命令将显示在底线上,在那里你输入特定的字符串来寻找。
这两个命令的不同在搜索方向替换:
-
/命令向前搜索(向下)在文件中。
-
?命令向后搜索(向上)在文件中。
n和N命令重复以前的搜索命令,分别在相同或相反的方向。有些字符具有特殊的含义,同时使用搜索命令和前面的搜索表达式的一部分被列为一个反斜杠()。
| Character | 描述 |
|---|---|
| ^ | Search at the beginning of the line. (Use at the beginning of a search expression.) |
| . | Matches a single character. |
| * | Matches zero or more of the previous character. |
| $ | End of the line (Use at the end of the search expression.) |
| [ | Starts a set of matching, or non-matching expressions. |
| < | Put in an expression escaped with the backslash to find the ending or beginning of a word. |
| > | See the '<' character description above. |
同一行内的字符搜索搜索命令后,寻找输入的字符。f和F命令仅在当前行上的字符搜索。 f 向前搜索和F搜索向后光标移动到的位置找到的字符。
t和T命令搜索仅在当前行上的字符,但对于T,光标移动到该字符前的位置,和T行的字符之后的位置向后搜索。
set命令:
你可以改变它的外表和感觉使用以下屏VI:设置命令。要使用这些命令,你必须在命令模式下,然后键入 :set其次任何下列选项:
| 命令 | 描述 |
|---|---|
| :set ic | Ignores case when searching |
| :set ai | Sets autoindent |
| :set noai | To unset autoindent. |
| :set nu | Displays lines with line numbers on the left side. |
| :set sw | Sets the width of a software tabstop. For example you would set a shift width of 4 with this command: :set sw=4 |
| :set ws | If wrapscan is set, if the word is not found at the bottom of the file, it will try to search for it at the beginning. |
| :set wm | If this option has a value greater than zero, the editor will automatically "word wrap". For example, to set the wrap margin to two characters, you would type this: :set wm=2 |
| :set ro | Changes file type to "read only" |
| :set term | Prints terminal type |
| :set bf | Discards control characters from input |
运行命令:
vi有能力在编辑器中运行命令。要运行一个命令,你只需要进入命令模式输入 :! 命令。
例如,如果你要检查文件是否存在,然后再尝试保存您的文件,文件名,你可以键入:! ls在屏幕上,你会看到ls和ls的输出。
当你按任意键(或命令的转义序列),您将返回到您的vi会话。
替换文本:
替换命令(:s/) ,使您能够快速替换单词或组的话,在你的文件。下面是一个简单的语法:
:s/search/replace/g
g代表全局。这个命令的结果是,所有出现光标的上线改变。
重要提示:
这里用vi你成功的关键点是:
-
您必须在命令模式下使用命令。 (按Esc键两次在任何时候,以确保您在命令模式中。)
-
你必须小心正确使用所有命令。
-
您必须在插入模式下输入文字。
