Translated 20120301 The Beginner's Guide to Start Using Vim - part 1 (#5034)

This commit is contained in:
Yeyin Hu 2017-01-22 05:29:39 -06:00 committed by Ezio
parent 27159a1fa5
commit 9e93d47d65
2 changed files with 123 additions and 125 deletions

View File

@ -1,125 +0,0 @@
Tarnslating by Yinr
The Beginners Guide to Start Using Vim
============================================================
![](https://maketecheasier-2d0f.kxcdn.com/assets/uploads/2012/03/vim-beginner-guide-featured.jpg "The Beginner's Guide to Start Using Vims")
This article is part of the [VIM User Guide][12] series:
* The Beginners Guide to Start Using Vim
* [Vim Keyboard Shortcuts Cheatsheet][3]
* [5 Vim Tips and Tricks for Experienced Users][4]
* [3 Useful VIM Editor Tips and Tricks for Advanced Users][5]
Choosing a text editor is a very important decision for a programmer. This is partly because of the plethora of variables: graphical/non-graphical interfaces, different shortcuts, language specializations, plugins, customizations, etc. My advice is not to try to search for the best one. Instead, choose the one that corresponds best to your habits and your tasks. If you want to work in a group, its generally best to select the same editor as your co-worker. That way, if you have a problem, you will be able to find some help.
It is exactly for that reason that I started using Vim a few years ago. Traditionally, Vim is placed in conflict with the legendary Emacs. I confess that I know very little about Emacs, but what you have to know about these two text editors is that they can both be fully customized, and very confusing at first. This tutorial will not explain everything about Vim but will try to give you the basics to use it correctly in the first place, and then present a few tips that will (I hope) allow you to learn on your own.
Vim comes from “VI iMproved”. Vi is a non-graphical text editor widely distributed in Unix systems. It comes by default with Linux. Vim is an enhancement of this original editor. However, unlke Vi, Vim is not installed by default on every distribution.
### Installation
To install Vim on Ubuntu, use the command:
```
sudo apt-get install vim
```
If you are already interested in some plugins, use the command:
```
sudo apt-cache search vim
```
This will give you a long list of packages related to Vim. Among them are some for various programming languages, addon managers, etc.
For this tutorial, I will be using the latest version of Vim (7.3.154) on Ubuntu. You can use any other version though.
### Warming Up
Type the command `vim` in a terminal. You should see a nice welcome screen.
![vim-welcome](https://maketecheasier-2d0f.kxcdn.com/assets/uploads/2012/02/vim-welcome.jpg "vim-welcome")
And if youve never used Vi or Vim before, it is very likely that you dont even know how to exit… Yes, its true. **None of the shortcuts you normally use will work in Vim**.
First of all, to use any menu-type function like save or exit, your command should begin with a colon (:). Saving is `:w` and quitting is `:q`. If you want to quit a file without saving, use the force quit command `:q!`. A cool thing with Vim is that you dont have to type commands separately. In other words, if you want to save and then quit, you can directly use `:wq`.
So for now, quit Vim and open it on a sample text file. Simply add the name of the text file that you want to edit after the command:
```
vim [text file name]
```
![vim-file](https://maketecheasier-2d0f.kxcdn.com/assets/uploads/2012/02/vim-file.jpg "vim-file")
By default, when you open a text file, you are in visual mode. It is quite specific to Vim and confusing at the beginning. Vim is composed mainly of two modes: visual and editing. The visual mode is for viewing a text and using some commands. To go into editing mode, just press `i` to insert and `a` to add some text. To go back into the visual mode and access all the menu-type functions, press the “Escape” key. The difference between insertion and addition is simply whether you want the text you type to appear before or after the cursor in visual mode. To understand this fully, you should really try it yourself. My advice is: add at the end of lines, and insert in other cases.
To move the cursor within a text, whether you are in visual or editing mode, you can generally use the keyboard arrows. A real purist would tell you to use the keys _h_ for left, _j_for down, _k_ for up, and _l_ for right.
Now that you are warmed up and know how to control Vim at a basic level, lets go to the core.
### A few basic commands
Now that you master the transformation from visual to editing mode, here are a few commands that you can use in visual mode:
* _x_: to delete a character
* _u_: to undo an action (the equivalent of Ctrl+z)
* _dd_: to delete a line
* _dw_: to delete a word
* _yy_: to copy a line
* _yw_: to copy a word
* _p_: to paste the previously deleted or copied line or word
* _e _: to move to the next word (faster than just moving with the arrow keys)
* _r_: to replace a letter (press _r_, then the new letter)
And of course, there are more, but this is enough for now. If you master all of them, you will already be very fluent with Vim.
As a side note for those who always want more, you can type a number before any of these commands and the command will be executed that number of times. For example, _5x_ will delete five characters in a row, while _3p_ will paste three times.
### Advanced Commands
Finally, as a bonus and an appetizer for your own research, here are a few advanced and very useful commands:
* _/searched_word _: to search for a word within the text
* _:sp name_of_a_text_file_: will split the screen in half horizontally, showing the new text file in the other half. To shift the focus from the right to the left window, use the shortcut Ctrl+w
![vim-sp](https://maketecheasier-2d0f.kxcdn.com/assets/uploads/2012/02/vim-sp.jpg "vim-sp")
* _:vsp name_of_a_text_file_: same as before, but splits the screen vertically
* Ctrl+Shift+C and Ctrl+Shift+V: to copy and paste text in a terminal
* _:! name_of_a_command_: to launch a command external to Vim, directly into your shell. For example, `:! ls` will display the files within the directory you are currently working in, without quitting the editor
![vim-ls](https://maketecheasier-2d0f.kxcdn.com/assets/uploads/2012/02/vim-ls.jpg "vim-ls")
### Conclusion
I think you now have every tool you need to start using Vim. You can go even further by installing the various plugins, editing the _.vimrc_ file, or even using the interactive tutor by typing the command _vimtutor_.
If you have any other commands that you would like to share about Vim, please let us know in the comments.
--------------------------------------------------------------------------------
via: https://www.maketecheasier.com/start-with-vim-linux/
作者:[Himanshu Arora][a]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]:https://www.maketecheasier.com/author/himanshu/
[1]:https://www.maketecheasier.com/author/adrienbrochard/
[2]:https://www.maketecheasier.com/start-with-vim-linux/#comments
[3]:https://www.maketecheasier.com/vim-keyboard-shortcuts-cheatsheet/
[4]:https://www.maketecheasier.com/vim-tips-tricks-for-experienced-users/
[5]:https://www.maketecheasier.com/vim-tips-tricks-advanced-users/
[6]:https://www.maketecheasier.com/category/linux-tips/
[7]:http://www.facebook.com/sharer.php?u=https%3A%2F%2Fwww.maketecheasier.com%2Fstart-with-vim-linux%2F
[8]:http://twitter.com/share?url=https%3A%2F%2Fwww.maketecheasier.com%2Fstart-with-vim-linux%2F&text=The+Beginner%26%238217%3Bs+Guide+to+Start+Using+Vim
[9]:mailto:?subject=The%20Beginner%E2%80%99s%20Guide%20to%20Start%20Using%20Vim&body=https%3A%2F%2Fwww.maketecheasier.com%2Fstart-with-vim-linux%2F
[10]:https://www.maketecheasier.com/turn-dropbox-into-a-blogging-tool-with-scriptogram/
[11]:https://www.maketecheasier.com/4-sms-back-up-applications-to-keep-your-messages-safe-android/
[12]:https://www.maketecheasier.com/series/vim-user-guide/
[13]:https://support.google.com/adsense/troubleshooter/1631343

View File

@ -0,0 +1,123 @@
Vim 初学者入门指南
============================================================
![](https://maketecheasier-2d0f.kxcdn.com/assets/uploads/2012/03/vim-beginner-guide-featured.jpg "Vim 初学者入门指南")
这篇文章是 [VIM 用户指南][12] 系列文章中的一篇:
* Vim 初学者入门指南
* [Vim 快捷键速查表][3]
* [5 个针对有经验用户的 Vim 技巧][4]
* [3 个针对高级用户的 Vim 编辑器有用技巧][5]
对一个程序员来说,选择一个文本编辑器是一件非常重要的事。因为不同编辑器之间有着不少的差异:图形界面或者非图形界面、不同的快捷键、不同的语言支持、不同的插件以及自定义设置等等。我的建议不是去搜索最棒的编辑器,而是去选择最适合你的习惯且最适应你的任务的那一个。假如你打算在一个团体中工作,那么最好和你的共事者选择一样的编辑器。这样的话,一旦你在使用中遇到问题,你就可以去向他们寻求帮助。
这正是我在几年之前开始使用 Vim 的原因。通常来说Vim 会被置于传说中的 Emacs 的对立面。我承认我对 Emacs 知之甚少,但是对于他俩,你需要知道的是他们都可以被深度定制,并且在初学时也都非常令人困惑。这个教程并不会介绍有关 Vim 的所有内容,而是将介绍一些基础以使你在最初就能正确使用它,随后还会展示一些小技巧,借此(希望能)让你有能力自己去探索学习。
Vim 一词来源于 “VI iMproved”。Vi 是一个被广泛安装于 Unix 系统的非图形界面文本编辑器,并且它也被默认安装在了 Linux 系统中。Vim 是这个原始编辑器的增强版,但是不同于 Vi它并未被默认安装在所有发行版中。
### 安装
在 Ubuntu 中可以使用如下命令来安装 Vim
```
sudo apt-get install vim
```
如果你已经对某些插件有了兴趣,使用以下命令:
```
sudo apt-cache search vim
```
这命令将给你输出一个很长的和 Vim 有关的包列表。在这之中,有针对不同编程语言的工具,有插件管理器,等等。
在这系列教程中,我将会使用 Ubuntu 上的最新版7.3.154)的 Vim译注现在最新版为 8.0)。当然你也可以使用其他任何版本。
### 热身
在终端输入 `vim` 命令,你将会看到一个非常棒的欢迎界面。
![vim-welcome](https://maketecheasier-2d0f.kxcdn.com/assets/uploads/2012/02/vim-welcome.jpg "vim-welcome")
如果你之前从未使用过 Vi 或者 Vim那么你很可能甚至不知道该怎么退出它... 是的,这是事实。**任何你常用的快捷键在 Vim 中都将失去原有的效果**。
首先要使用任何命令式的功能像保存save或者退出exit你都先得输入一个冒号:)。保存是 `:w` 而退出是 `:q`。如果你想不保存地退出,那么就要使用强制退出命令 `:q!`。Vim 中非常棒的一点是你不需要分开输入各个命令,换言之,如果你想保存然后退出,你就可以直接使用 `:wq`
现在,我们退出 Vim 再打开一个文本文件。为此,你只需把想要编辑的文件名加在命令后面即可:
```
vim [文本文件名]
```
![vim-file](https://maketecheasier-2d0f.kxcdn.com/assets/uploads/2012/02/vim-file.jpg "vim-file")
一般而言,当你打开一个文本文件,你将会处在普通模式(译注:原文误,应为 normal mode。这使得 Vim 与众不同并且最初会让人感到困惑。Vim 主要由两种模式构成:普通模式(译注:原文误,应为 normal mode下同和插入模式译注原文误应为 insert mode下同。普通模式用于查看内容并且使用一些命令。想要进入插入模式只需按 `i` 键进行插入insert或者 `a` 键进行添加add。想要返回到普通模式或者进行命令式功能的操作按“Escape”键即可。插入insert和添加add的差异仅仅在于你是想在光标位置之前还是在光标之后进入插入模式并进行文字输入。要想彻底地明白你应该亲自去尝试一下。我的建议是仅在行尾使用添加add而在其他时候使用插入insert
要想在文本之中移动光标,你通常可以使用键盘上的方向键,它们无论是在普通模式还是在插入模式都可以生效。不过,一个真正的纯粹主义者将会告诉你使用按键 _h_ 向左_j_ 向下_k_ 向上_l_ 向右来进行移动。
现在你已经明白了如何和简单地控制 Vim我们再来更加深入一些。
### 一些简单命令
现在你已经熟悉了在普通模式和插入模式之间进行切换,下面是一些可以在普通模式中使用的命令:
* _x_:删除一个字符
* _u_:撤销一个操作(相当与 Ctrl+z
* _dd_:删除一行内容
* _dw_:删除一个单词
* _yy_:复制一行内容
* _yw_:复制一个单词
* _p_:粘贴一个之前删除或复制的行或者单词
* _e_:跳到下个单词(译注:词尾)(比单纯用方向键更快)
* _r_:替换一个字母(按 _r_,松开,然后再按新字母)
当然不止这些,不过这些对现在来说已经足够了。如果你掌握了上面的全部,你将能你很顺溜地使用 Vim 了。
对于那些还想知道更多的人我再多提一下。你可以在任何这些命令之前加上一个数值那么这个命令将被重复执行相应的次数。例如_5x_ 将在当前行连续删除 5 个字母,而 _3p_ 将会粘贴 3 次。
### 高级命令
最后,作为对你自己继续探索的鼓励和示例,这里给出几个高级且常用的命令:
* _/搜索内容_:在文中搜索特定内容
* _:sp 文本文件名_:将屏幕水平分割成两半,并将文件展示在另一半。想要在两侧切换焦点,可以使用 Ctrl+w 快捷键
![vim-sp](https://maketecheasier-2d0f.kxcdn.com/assets/uploads/2012/02/vim-sp.jpg "vim-sp")
* _:vsp 文本文件名_:同上,但是是垂直分割屏幕
* Ctrl+Shift+C 和 Ctrl+Shift+V在终端中复制和粘贴文本
* _:! 命令名_:在 Vim 中直接运行终端命令。例如,`:! ls` 将在不退出编辑器的同时,显示你当前目录内的文件
![vim-ls](https://maketecheasier-2d0f.kxcdn.com/assets/uploads/2012/02/vim-ls.jpg "vim-ls")
### 结论
我觉得你现在应该已经有了足够的工具来开始使用 Vim。你还可以通过安装各种插件编辑 _.vimrc_ 文件,或者输入 _vimtutor_ 命令来使用交互式教程以学到更多。
如果你有任何你想分享的关于 Vim 的其他命令,请在评论中告知我们。
--------------------------------------------------------------------------------
via: https://www.maketecheasier.com/start-with-vim-linux/
作者:[Himanshu Arora][a]
译者:[Yinr](https://github.com/Yinr)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]:https://www.maketecheasier.com/author/himanshu/
[1]:https://www.maketecheasier.com/author/adrienbrochard/
[2]:https://www.maketecheasier.com/start-with-vim-linux/#comments
[3]:https://www.maketecheasier.com/vim-keyboard-shortcuts-cheatsheet/
[4]:https://www.maketecheasier.com/vim-tips-tricks-for-experienced-users/
[5]:https://www.maketecheasier.com/vim-tips-tricks-advanced-users/
[6]:https://www.maketecheasier.com/category/linux-tips/
[7]:http://www.facebook.com/sharer.php?u=https%3A%2F%2Fwww.maketecheasier.com%2Fstart-with-vim-linux%2F
[8]:http://twitter.com/share?url=https%3A%2F%2Fwww.maketecheasier.com%2Fstart-with-vim-linux%2F&text=The+Beginner%26%238217%3Bs+Guide+to+Start+Using+Vim
[9]:mailto:?subject=The%20Beginner%E2%80%99s%20Guide%20to%20Start%20Using%20Vim&body=https%3A%2F%2Fwww.maketecheasier.com%2Fstart-with-vim-linux%2F
[10]:https://www.maketecheasier.com/turn-dropbox-into-a-blogging-tool-with-scriptogram/
[11]:https://www.maketecheasier.com/4-sms-back-up-applications-to-keep-your-messages-safe-android/
[12]:https://www.maketecheasier.com/series/vim-user-guide/
[13]:https://support.google.com/adsense/troubleshooter/1631343