mirror of
https://github.com/LCTT/TranslateProject.git
synced 2025-01-25 23:11:02 +08:00
translated by MZqk
This commit is contained in:
parent
38b66b6c98
commit
9f4e835019
@ -1,100 +0,0 @@
|
||||
translating by MZqk
|
||||
|
||||
11 awesome vi tips and tricks
|
||||
======
|
||||
|
||||
![](https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/keyboaord_enter_writing_documentation.jpg?itok=kKrnXc5h)
|
||||
|
||||
The [vi editor][1] is one of the most popular text editors on Unix and Unix-like systems, such as Linux. Whether you're new to vi or just looking for a refresher, these 11 tips will enhance how you use it.
|
||||
|
||||
### Editing
|
||||
|
||||
Editing a long script can be tedious, especially when you need to edit a line so far down that it would take hours to scroll to it. Here's a faster way.
|
||||
|
||||
1. The command `:set number` numbers each line down the left side.
|
||||
|
||||
![](https://opensource.com/sites/default/files/styles/panopoly_image_original/public/u128651/setnum.png?itok=sFVA97mG)
|
||||
|
||||
You can directly reach line number 26 by opening the file and entering this command on the CLI: `vi +26 sample.txt`. To edit line 26 (for example), the command `:26` will take you directly to it.
|
||||
|
||||
![](https://opensource.com/sites/default/files/styles/panopoly_image_original/public/u128651/number.png?itok=d7FE0LL3)
|
||||
|
||||
### Fast navigation
|
||||
|
||||
2. `i` changes your mode from "command" to "insert" and starts inserting text at the current cursor position.
|
||||
3. `a` does the same, except it starts just after the current cursor position.
|
||||
4. `o` starts the cursor position from the line below the current cursor position.
|
||||
|
||||
|
||||
|
||||
### Delete
|
||||
|
||||
If you notice an error or typo, being able to make a quick fix is important. Good thing vi has it all figured out.
|
||||
|
||||
Understanding vi's delete function so you don't accidentally press a key and permanently remove a line, paragraph, or more, is critical.
|
||||
|
||||
5. `x` deletes the character under the cursor.
|
||||
6. `dd` deletes the current line. (Yes, the whole line!)
|
||||
|
||||
|
||||
|
||||
Here's the scary part: `30dd` would delete 30 lines starting with the current line! Proceed with caution when using this command.
|
||||
|
||||
### Search
|
||||
|
||||
You can search for keywords from the "command" mode rather than manually navigating and looking for a specific word in a plethora of text.
|
||||
|
||||
7. `:/<keyword>` searches for the word mentioned in the `< >` space and takes your cursor to the first match.
|
||||
8. To navigate to the next instance of that word, type `n`, and keep pressing it until you get to the match you're looking for.
|
||||
|
||||
|
||||
|
||||
For example, in the image below I searched for `ssh`, and vi highlighted the beginning of the first result.
|
||||
|
||||
![](https://opensource.com/sites/default/files/styles/panopoly_image_original/public/u128651/ssh-search.png?itok=tJ-7FujH)
|
||||
|
||||
After I pressed `n`, vi highlighted the next instance.
|
||||
|
||||
![](https://opensource.com/sites/default/files/styles/panopoly_image_original/public/u128651/n-search.png?itok=wU-u3LiI)
|
||||
|
||||
### Save and exit
|
||||
|
||||
Developers (and others) will probably find this next command useful.
|
||||
|
||||
9. `:x` saves your work and exits vi.
|
||||
|
||||
![](https://opensource.com/sites/default/files/styles/panopoly_image_original/public/u128651/x.png?itok=kfoHx84m)
|
||||
|
||||
10. If you think every nanosecond is worth saving, here's a faster way to shift to terminal mode in vi. Instead of pressing `Shift+:` on the keyboard, you can press `Shift+q` (or Q, in caps) to access [Ex mode][2], but this doesn't really make any difference if you just want to save and quit by typing `x` (as shown above).
|
||||
|
||||
|
||||
|
||||
### Substitution
|
||||
|
||||
Here is a neat trick if you want to substitute every occurrence of one word with another. For example, if you want to substitute "desktop" with "laptop" in a large file, it would be monotonous and waste time to search for each occurrence of "desktop," delete it, and type "laptop."
|
||||
|
||||
11. The command `:%s/desktop/laptop/g` would replace each occurrence of "desktop" with "laptop" throughout the file; it works just like the Linux `sed` command.
|
||||
|
||||
|
||||
|
||||
In this example, I replaced "root" with "user":
|
||||
|
||||
![](https://opensource.com/sites/default/files/styles/panopoly_image_original/public/u128651/subs-command.png?itok=M8MN72sp)
|
||||
|
||||
![](https://opensource.com/sites/default/files/styles/panopoly_image_original/public/u128651/subs-result.png?itok=34zzVdUt)
|
||||
|
||||
These tricks should help anyone get started using vi. Are there other neat tips I missed? Share them in the comments.
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://opensource.com/article/18/1/top-11-vi-tips-and-tricks
|
||||
|
||||
作者:[Archit Modi][a]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]:https://opensource.com/users/architmodi
|
||||
[1]:http://ex-vi.sourceforge.net/
|
||||
[2]:https://en.wikibooks.org/wiki/Learning_the_vi_Editor/Vim/Modes#Ex-mode
|
95
translated/talk/20180214 11 awesome vi tips and tricks.md
Normal file
95
translated/talk/20180214 11 awesome vi tips and tricks.md
Normal file
@ -0,0 +1,95 @@
|
||||
11 个超棒的 vi 技巧和窍门
|
||||
======
|
||||
![](https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/keyboaord_enter_writing_documentation.jpg?itok=kKrnXc5h)
|
||||
|
||||
[vi][1] 编辑器是 Linux 最流行的编辑器之一,默认安装在 Unix 和类 Uinux 系统中。无论您是 vi 新手还是想进修,这些技巧都对您有用。
|
||||
|
||||
### 编辑
|
||||
|
||||
编辑长文本时可能很难受,特别是编辑其中某一行时,需要移动许久才能到这行。这有个很快的方法:
|
||||
|
||||
1. `:set number` 这个命令可是在编辑器左边显示行号。
|
||||
|
||||
![](https://opensource.com/sites/default/files/styles/panopoly_image_original/public/u128651/setnum.png?itok=sFVA97mG)
|
||||
|
||||
您可以在命令行中输入 `vi +26 samp.txt` 命令直接打开文件到达 26 行,在 vi 编辑器中也可以输入 `:26` 跳转到 26 行。
|
||||
|
||||
![](https://opensource.com/sites/default/files/styles/panopoly_image_original/public/u128651/number.png?itok=d7FE0LL3)
|
||||
|
||||
### 快速导航
|
||||
|
||||
2. `i` 将工作方式从“命令模式”更改为“输入模式”,并在当前光标位置开始插入内容。
|
||||
3. `a` 除了是光标之后开始插入年内容,与上面的效果是一样的。
|
||||
4. `o` 在光标的下一行位置开始插入内容。
|
||||
|
||||
|
||||
|
||||
### 删除
|
||||
|
||||
如果您发现错误或错别字,能快速的修正是很重要的。好在 vi 都事先想好了。
|
||||
|
||||
了解 vi 的删除功能,保证你不会意外按下某个键并永久删除一行或多段内容,这点至关重要。
|
||||
|
||||
5. `x` 删除当前光标的字符。
|
||||
6. `dd` 删除当前行 (是的,整行内容!)
|
||||
|
||||
下面看可怕的部分:`30dd` 从当前行开始删除以下 30 行!使用此命令请慎重。
|
||||
|
||||
### 搜索
|
||||
|
||||
您可以在“命令模式”搜索关键字,而不用在大量文本内容中手动导航查找特定的单词或内容。
|
||||
|
||||
7. `:/<keyword>` 搜索 `< >` 中的单词并将光标移动到第一个匹配项。
|
||||
8. 导航到该单词的下一个匹配项,请输入 `n` 并继续按下, 直到找到您要找的内容。
|
||||
|
||||
|
||||
|
||||
例如,在这个图像中我要搜索包含 `ssh` 的内容, vi 光标就会突出第一个结果的开始位置。
|
||||
|
||||
![](https://opensource.com/sites/default/files/styles/panopoly_image_original/public/u128651/ssh-search.png?itok=tJ-7FujH)
|
||||
|
||||
按下 n 之后, vi 光标就会突出下一个匹配项。
|
||||
|
||||
![](https://opensource.com/sites/default/files/styles/panopoly_image_original/public/u128651/n-search.png?itok=wU-u3LiI)
|
||||
|
||||
### 保存并退出
|
||||
|
||||
开发人员 (或其他人) 可能会发现这个命令很有用。
|
||||
|
||||
9. `:x` 保存您的工作空间并退出 vi 。
|
||||
|
||||
![](https://opensource.com/sites/default/files/styles/panopoly_image_original/public/u128651/x.png?itok=kfoHx84m)
|
||||
|
||||
10. 如果你想实时保存,那么这有个更快的方法。你可以按下 `Shift+q` (或者大写字母 Q ) 来进入 [Ex 模式][2] , 而不是在键盘上按 `Shift+:` 。但是如果你只是想按下 `x` 来保存退出,那就没有什么区别(如上所示)。
|
||||
|
||||
|
||||
|
||||
### 替换
|
||||
|
||||
如果您想将文中的某个单词全部替换为一个单词,这有个很巧妙的招式。例如,如果您想在一个大文件中将 "desktop" 替换为 "laptop" ,那么单调的搜索每个出现的 "desktop" 将其删掉,然后再输入 "laotop" ,是很浪费时间的。
|
||||
|
||||
11. `:%s/desktop/laptop/g` 这个命令将在整个文件中的 "desktop" 用 "laptop" 替换,他就像 Linux 的 `sed` 命令一样。
|
||||
|
||||
|
||||
|
||||
这个例子中我用 "user" 替换了 "root" :
|
||||
|
||||
![](https://opensource.com/sites/default/files/styles/panopoly_image_original/public/u128651/subs-command.png?itok=M8MN72sp)
|
||||
|
||||
![](https://opensource.com/sites/default/files/styles/panopoly_image_original/public/u128651/subs-result.png?itok=34zzVdUt)
|
||||
|
||||
这些技巧应该能帮组任何想开始学 vi 的人。我有遗漏其他巧妙的提示吗?请在评论中分享他们。
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://opensource.com/article/18/1/top-11-vi-tips-and-tricks
|
||||
|
||||
作者:[Archit Modi][a]
|
||||
译者:[MZqk](https://github.com/MZqk)
|
||||
校对:[校对者 ID](https://github.com/ 校对者 ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux 中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]:https://opensource.com/users/architmodi
|
||||
[1]:http://ex-vi.sourceforge.net/
|
||||
[2]:https://en.wikibooks.org/wiki/Learning_the_vi_Editor/Vim/Modes#Ex-mode
|
Loading…
Reference in New Issue
Block a user