mirror of
https://github.com/LCTT/TranslateProject.git
synced 2024-12-29 21:41:00 +08:00
commit
7f41e3cd03
@ -1,131 +0,0 @@
|
|||||||
ucasFL translating
|
|
||||||
5 Vim Tips and Tricks for Experienced Users
|
|
||||||
============================================================
|
|
||||||
![](https://maketecheasier-2d0f.kxcdn.com/assets/uploads/2014/08/vim-tips-tricks-featured.jpg "5 Vim Tips and Tricks for Experienced Userss")
|
|
||||||
|
|
||||||
This article is part of the [VIM User Guide][12] series:
|
|
||||||
|
|
||||||
* [The Beginner’s Guide to Start Using Vim][3]
|
|
||||||
* [Vim Keyboard Shortcuts Cheatsheet][4]
|
|
||||||
* 5 Vim Tips and Tricks for Experienced Users
|
|
||||||
* [3 Useful VIM Editor Tips and Tricks for Advanced Users][5]
|
|
||||||
|
|
||||||
The Vim editor offers so many features that it’s very difficult to learn all of them. While, of course, spending more and more time on the command line editor always helps, there is no denying the fact that you learn new and productive things faster while interacting with fellow Vim users. Here are some Vim tips and tricks for you.
|
|
||||||
|
|
||||||
|
|
||||||
**Note** – To create the examples here, I used Vim version 7.4.52.
|
|
||||||
|
|
||||||
### 1\. Working with multiple files
|
|
||||||
|
|
||||||
If you are a software developer or someone who uses Vim as their primary editor, chances are that you have to work with multiple files simultaneously. Following are some useful tips that you can use while working with multiple files.
|
|
||||||
|
|
||||||
Instead of opening different files in different shell tabs, you can open multiple files in a single tab by passing their filenames as arguments to the vim command. For example:
|
|
||||||
|
|
||||||
```
|
|
||||||
vim file1 file2 file3
|
|
||||||
```
|
|
||||||
|
|
||||||
The first file (file1 in the example) will be the current file and read into the buffer.
|
|
||||||
|
|
||||||
Once inside the editor, use the `:next` or `:n` command to move to the next file, and the `:prev` or `:N` command to return to the previous one. To directly switch to the first or the last file, use `:bf` and `:bl` commands, respectively. To open and start editing another file, use the `:e` command with the filename as argument (use the complete path in case the file is not present in the current directory).
|
|
||||||
|
|
||||||
At any point if it is required to list down currently opened files, use the `:ls` command. See the screen shot shown below.
|
|
||||||
|
|
||||||
![vim-ls](https://maketecheasier-2d0f.kxcdn.com/assets/uploads/2014/08/vim-ls.png "vim-ls")
|
|
||||||
|
|
||||||
Note that “%a” represents the file in the current active window, while “#” represents the file in the previous active window.
|
|
||||||
|
|
||||||
### 2\. Save time with auto complete
|
|
||||||
|
|
||||||
Want to save time and improve accuracy? Use abbreviations. They come in handy while writing long, complex words that recur frequently throughout the file. The Vim command for abbreviations is `ab`. For example, after you run the command
|
|
||||||
|
|
||||||
```
|
|
||||||
:ab asap as soon as possible
|
|
||||||
```
|
|
||||||
|
|
||||||
each occurrence of the word “asap” will be automatically replaced by “as soon as possible”, as you type.
|
|
||||||
|
|
||||||
Similarly, you can also use abbreviations to correct common typing mistakes. For example, the command
|
|
||||||
|
|
||||||
```
|
|
||||||
:ab recieve receive
|
|
||||||
```
|
|
||||||
|
|
||||||
will automatically correct the spelling mistake as you type. If you want to prevent the expansion/correction from happening at a particular occurrence, just type “Ctrl + V” after the last character of the word and then press the space bar key.
|
|
||||||
|
|
||||||
If you want to save the abbreviation you’ve created so that it is available to you the next time you use the Vim editor, add the complete `ab` command (without the initial colon) to “/etc/vim/vimrc” file. To remove a particular abbreviation, you can use the `una`command. For example, `:una asap`.
|
|
||||||
|
|
||||||
### 3\. Split windows to easily copy/paste
|
|
||||||
|
|
||||||
There are times when you want to copy a piece of code or a portion of text from one file to another. While the process is easy when working with GUI editors, it gets a bit tedious and time-consuming while working with a command line editor. Fortunately, Vim provides a way to minimize the time and effort required to do this.
|
|
||||||
|
|
||||||
Open one of the two files and then split the Vim window to open the other file. This can be done by using the `split` command with the file name as argument. For example,
|
|
||||||
|
|
||||||
```
|
|
||||||
:split test.c
|
|
||||||
```
|
|
||||||
|
|
||||||
will split the window and open “test.c”.
|
|
||||||
|
|
||||||
![vim-split](https://maketecheasier-2d0f.kxcdn.com/assets/uploads/2014/08/vim-split.png "vim-split")
|
|
||||||
|
|
||||||
Observe that the command split the Vim window horizontally. In case you want to split the window vertically, you can do so using the `vsplit` command. Once both the files are opened, copy the stuff from one file, press “Ctrl + w” to switch the control to another file, and paste.
|
|
||||||
|
|
||||||
### 4\. Save a file you edited without the required permissions
|
|
||||||
|
|
||||||
There are times when you realize that a file is read-only only after making a bunch of changes to it.
|
|
||||||
|
|
||||||
![vim-sudo](https://maketecheasier-2d0f.kxcdn.com/assets/uploads/2014/08/vim-sudo.png "vim-sudo")
|
|
||||||
|
|
||||||
Although closing the file and reopening it with the required permissions is a way out, it’s a sheer waste of time if you’ve already made a lot of changes, as all of them will be lost during the process. Vim provides you a way to handle this situation by allowing you to change the file permissions from within the editor before you save it. The command for this is:
|
|
||||||
|
|
||||||
```
|
|
||||||
:w !sudo tee %
|
|
||||||
```
|
|
||||||
|
|
||||||
The command will ask you for the password, just like `sudo` does on the command line, and will then save the changes.
|
|
||||||
|
|
||||||
**A related tip**: To quickly access the command prompt while editing a file in Vim, run the `:sh` command from within the editor. This will place you in an interactive shell. Once you are done, run the `exit` command to quickly return to your Vim session.
|
|
||||||
|
|
||||||
### 5\. Preserve indentation during copy/paste
|
|
||||||
|
|
||||||
Most of the experienced programmers work on Vim with auto indentation enabled. Although it’s a time-saving practice, it creates a problem while pasting an already indented code. For example, this is what happened when I pasted an already indented code into a file opened in Vim editor with auto indent on.
|
|
||||||
|
|
||||||
![vim-indentation](https://maketecheasier-2d0f.kxcdn.com/assets/uploads/2014/08/vim-indentation.png "vim-indentation")
|
|
||||||
|
|
||||||
The solution to this problem is the `pastetoggle` option. Add the line
|
|
||||||
|
|
||||||
```
|
|
||||||
set pastetoggle=<F2>
|
|
||||||
```
|
|
||||||
|
|
||||||
to your vimrc file, and press F2 in insert mode just before pasting the code. This should preserve the original indentation. Note that you can replace F2 with any other key if it’s already mapped to some other functionality.
|
|
||||||
|
|
||||||
### Conclusion
|
|
||||||
|
|
||||||
The only way you can further improve your Vim editor skills is by using the command line editor for your day-to-day work. Just note down the actions that take time and then try to find out if there is an editor command that will do the actions more quickly.
|
|
||||||
|
|
||||||
--------------------------------------------------------------------------------
|
|
||||||
|
|
||||||
via: https://www.maketecheasier.com/vim-tips-tricks-for-experienced-users/
|
|
||||||
|
|
||||||
作者:[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/himanshu/
|
|
||||||
[2]:https://www.maketecheasier.com/vim-tips-tricks-for-experienced-users/#comments
|
|
||||||
[3]:https://www.maketecheasier.com/start-with-vim-linux/
|
|
||||||
[4]:https://www.maketecheasier.com/vim-keyboard-shortcuts-cheatsheet/
|
|
||||||
[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%2Fvim-tips-tricks-for-experienced-users%2F
|
|
||||||
[8]:http://twitter.com/share?url=https%3A%2F%2Fwww.maketecheasier.com%2Fvim-tips-tricks-for-experienced-users%2F&text=5+Vim+Tips+and+Tricks+for+Experienced+Users
|
|
||||||
[9]:mailto:?subject=5%20Vim%20Tips%20and%20Tricks%20for%20Experienced%20Users&body=https%3A%2F%2Fwww.maketecheasier.com%2Fvim-tips-tricks-for-experienced-users%2F
|
|
||||||
[10]:https://www.maketecheasier.com/enable-two-step-verification-apple-icloud-account/
|
|
||||||
[11]:https://www.maketecheasier.com/mistakes-wordpress-user-should-avoid/
|
|
||||||
[12]:https://www.maketecheasier.com/series/vim-user-guide/
|
|
||||||
[13]:https://support.google.com/adsense/troubleshooter/1631343
|
|
@ -1,101 +0,0 @@
|
|||||||
ucasFL translating
|
|
||||||
3 Useful VIM Editor Tips and Tricks for Advanced Users
|
|
||||||
============================================================
|
|
||||||
|
|
||||||
![](https://maketecheasier-2d0f.kxcdn.com/assets/uploads/2017/01/vim-featured.jpg "3 Useful VIM Editor Tips and Tricks for Advanced Userss")
|
|
||||||
|
|
||||||
This article is part of the [VIM User Guide][12] series:
|
|
||||||
|
|
||||||
* [The Beginner’s Guide to Start Using Vim][3]
|
|
||||||
* [Vim Keyboard Shortcuts Cheatsheet][4]
|
|
||||||
* [5 Vim Tips and Tricks for Experienced Users][5]
|
|
||||||
* 3 Useful VIM Editor Tips and Tricks for Advanced Users
|
|
||||||
|
|
||||||
Vim is undoubtedly a very powerful text editor. It offers a plethora of features which means that studying and remembering every Vim functionality isn’t practically possible. But what we can do at least is keep learning easier ways of doing things so that our experience with the editor keeps on getting better with time.
|
|
||||||
|
|
||||||
With that in mind, in this article we will discuss some Vim editor tips/tricks that are aimed at advanced users.
|
|
||||||
|
|
||||||
**Note**: If you are completely new to Vim, you can first go through our [getting started guide][14]. For those who’ve just started using the editor, I’m sure our [Vim keyboard shortcuts cheatsheet][15] will be extremely useful to you. And if you’re already an experienced user, you might also want to find out [some tips and tricks for experienced users][16].
|
|
||||||
|
|
||||||
Please note that all the tips mentioned in this article have been mostly explained using easy-to-understand coding situations, as they come in really handy while software development. But that does not mean normal users (who aren’t coders and use Vim for general text editing) can’t use them in their work.
|
|
||||||
|
|
||||||
### 1\. Set file specific variables
|
|
||||||
|
|
||||||
There may be times when – in a particular file – you would want any tab character that you type to get replaced by spaces. Or you may want a source code file to use two spaces for indentation even if the editor’s default indentation is set to four spaces.
|
|
||||||
|
|
||||||
Basically we’re talking about file-specific changes here. There’s a feature that Vim provides which allows you to change certain settings only for a particular file. That feature is called “Modeline.”
|
|
||||||
|
|
||||||
For example, to make sure that each tab you type gets replaced by spaces, all you have to do is to add the following modeline in the first or last few lines of the file in question:
|
|
||||||
|
|
||||||
```
|
|
||||||
# vim: set expandtab:
|
|
||||||
```
|
|
||||||
|
|
||||||
And to change the indentation from default (4) to 2, use the following modeline in the source file:
|
|
||||||
|
|
||||||
```
|
|
||||||
// vim: noai:ts=2:sw=2
|
|
||||||
```
|
|
||||||
|
|
||||||
Here are some important points that you need to keep in mind when dealing with modelines:
|
|
||||||
|
|
||||||
* Modelines should only be added in the first or last five lines of the file.
|
|
||||||
* The “modeline” option must be set (`:set modeline`) in the “.vimrc” file order to take advantage of this feature.
|
|
||||||
* The feature is off by default when editing as root.
|
|
||||||
|
|
||||||
For more information, head to the feature’s [official documentation][17].
|
|
||||||
|
|
||||||
### 2\. Keyword completion
|
|
||||||
|
|
||||||
As you start writing more and more complex code or start working on large source files, you deal with several variable names. Sometimes it’s not easy to remember all the names, so whenever you have to write a variable name you usually copy it from where it’s already used.
|
|
||||||
|
|
||||||
Thankfully, with Vim you can just write some initial letters of the variable. Without leaving the Insert mode, press “Ctrl + n” or “Ctrl + p” to get a list of matching keywords. While “Ctrl + n” is used to insert the next matching word, “Ctrl + p” gives you a list of previous matching words.
|
|
||||||
|
|
||||||
Here’s this feature in action.
|
|
||||||
|
|
||||||
![vim-keyword-completion1](https://maketecheasier-2d0f.kxcdn.com/assets/uploads/2017/01/vim-keyword-completion1.jpg "vim-keyword-completion1")
|
|
||||||
|
|
||||||
As is clear from the screenshot above, the list that pops up also contains words from other source files. For more information on this feature, head [here][18].
|
|
||||||
|
|
||||||
### 3\. Searching
|
|
||||||
|
|
||||||
Suppose you are debugging your code, and as part of that you need to quickly see all the occurrences of a variable in a file. A commonly used way to do this is to come out of the Insert mode, write `/[var-name]`, press Enter, and then go back and forth using the “n” and “p” keys.
|
|
||||||
|
|
||||||
There’s no problem with the aforementioned approach, per se, but there’s a slightly more easier and quicker way to do this kind of search. For that, first you have to make sure that you are out of Insert mode and that the cursor is under the word/variable you’re trying to search, which isn’t time consuming at all. And next, all you have to do is press “Shift + *.”
|
|
||||||
|
|
||||||
Do this repeatedly, and the editor will quickly take you to all the places where the word/variable is used in the file.
|
|
||||||
|
|
||||||
### Conclusion
|
|
||||||
|
|
||||||
Although aimed at advanced users, the tips/tricks discussed here aren’t difficult to understand and use. If your basics are clear, you can really benefit from them. Needless to say, as with any new feature or concept, you need to practice these tips to make them a habit.
|
|
||||||
|
|
||||||
|
|
||||||
--------------------------------------------------------------------------------
|
|
||||||
|
|
||||||
via: https://www.maketecheasier.com/vim-tips-tricks-advanced-users/
|
|
||||||
|
|
||||||
作者:[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/himanshu/
|
|
||||||
[2]:https://www.maketecheasier.com/vim-tips-tricks-advanced-users/#respond
|
|
||||||
[3]:https://www.maketecheasier.com/start-with-vim-linux/
|
|
||||||
[4]:https://www.maketecheasier.com/vim-keyboard-shortcuts-cheatsheet/
|
|
||||||
[5]:https://www.maketecheasier.com/vim-tips-tricks-for-experienced-users/
|
|
||||||
[6]:https://www.maketecheasier.com/category/linux-tips/
|
|
||||||
[7]:http://www.facebook.com/sharer.php?u=https%3A%2F%2Fwww.maketecheasier.com%2Fvim-tips-tricks-advanced-users%2F
|
|
||||||
[8]:http://twitter.com/share?url=https%3A%2F%2Fwww.maketecheasier.com%2Fvim-tips-tricks-advanced-users%2F&text=3+Useful+VIM+Editor+Tips+and+Tricks+for+Advanced+Users
|
|
||||||
[9]:mailto:?subject=3%20Useful%20VIM%20Editor%20Tips%20and%20Tricks%20for%20Advanced%20Users&body=https%3A%2F%2Fwww.maketecheasier.com%2Fvim-tips-tricks-advanced-users%2F
|
|
||||||
[10]:https://www.maketecheasier.com/opt-out-google-personalized-ads/
|
|
||||||
[11]:https://www.maketecheasier.com/wi-fi-vs-ethernet-vs-4g/
|
|
||||||
[12]:https://www.maketecheasier.com/series/vim-user-guide/
|
|
||||||
[13]:https://support.google.com/adsense/troubleshooter/1631343
|
|
||||||
[14]:https://www.maketecheasier.com/start-with-vim-linux/
|
|
||||||
[15]:https://www.maketecheasier.com/vim-keyboard-shortcuts-cheatsheet/
|
|
||||||
[16]:https://www.maketecheasier.com/vim-tips-tricks-for-experienced-users/
|
|
||||||
[17]:http://vim.wikia.com/wiki/Modeline_magic
|
|
||||||
[18]:http://vim.wikia.com/wiki/Any_word_completion
|
|
@ -0,0 +1,132 @@
|
|||||||
|
5 个针对有经验用户的 Vim 实用技巧
|
||||||
|
============================================================
|
||||||
|
![](https://maketecheasier-2d0f.kxcdn.com/assets/uploads/2014/08/vim-tips-tricks-featured.jpg "5 Vim Tips and Tricks for Experienced Userss")
|
||||||
|
|
||||||
|
这篇文章是 [Vim 用户指南][12]系列文章中的一篇:
|
||||||
|
|
||||||
|
* [Vim 初学者入门指南][3]
|
||||||
|
* [Vim 快捷键速查表][4]
|
||||||
|
* [3 个针对高级用户的 Vim 编辑器实用技巧][5]
|
||||||
|
|
||||||
|
Vim 编辑器提供了很多的特性,要想全部掌握它们很困难。然而,花费更多的时间在命令行编辑器上总是有帮助的。毫无疑问,和 Vim 用户们进行交流能够让你更快地学习新颖有创造性的东西。
|
||||||
|
|
||||||
|
|
||||||
|
**注** - 本文中用到的例子,使用的 Vim 版本是 7.4.52 。
|
||||||
|
|
||||||
|
### 1\. 同时编辑多个文件
|
||||||
|
|
||||||
|
如果你是一名软件开发者或者把 Vim 作为主要的编辑器,那么可能很多时候你需要同时编辑多个文件。“紧跟(following)”是在同时编辑多个文件时可用的实用技巧。
|
||||||
|
|
||||||
|
不需要在多个 shell 界面中打开多个文件,你可以通过把多个文件的文件名作为 Vim 命令的参数从而在一个 shell 界面中打开多个文件。比如:
|
||||||
|
|
||||||
|
```
|
||||||
|
vim 文件1 文件2 文件3
|
||||||
|
```
|
||||||
|
|
||||||
|
第一个文件(例子中的文件1)将成为当前文件并被读入缓冲区。
|
||||||
|
|
||||||
|
在编辑器中,使用 ‘:next’ 或 ’:n’ 命令来移动到下一个文件,使用 ‘:prev’ 或 ‘:N’ 命令返回上一个文件。如果想直接切换到第一个文件或最后一个文件,使用 ‘:bf’ 和 ‘:bl’ 命令。特别地,如果想打开另外的文件并编辑,使用 ‘:e’ 命令并把文件名作为参数(如果该文件不在当前目录中则需要完整路径做为参数)。
|
||||||
|
|
||||||
|
任何时候如果需要列出当前打开的所有文件,使用 ‘:ls’ 命令。看下面展示的屏幕截图。
|
||||||
|
|
||||||
|
![vim-ls](https://maketecheasier-2d0f.kxcdn.com/assets/uploads/2014/08/vim-ls.png "vim-ls")
|
||||||
|
|
||||||
|
注意 ”%a” 表示文件在当前活动窗口,而 “#” 表示文件在上一个活动窗口。
|
||||||
|
|
||||||
|
### 2\. 通过自动补全节约时间
|
||||||
|
|
||||||
|
想节约时间并提高效率吗?使用 ‘abbreviations(缩写)’吧。使用它们能够快速写出文件中多次出现、复杂冗长的词。在 Vim 命令中 ‘abbreviations(缩写)’的缩写就是 ‘ab’ 。
|
||||||
|
比如,当你运行下面的命令以后:
|
||||||
|
|
||||||
|
```
|
||||||
|
:ab asap as soon as possible
|
||||||
|
```
|
||||||
|
|
||||||
|
文件中出现的每一个 "asap" 都会被自动替换为 “as soon as possible” ,就像你自己输入的一样。
|
||||||
|
|
||||||
|
类似地,你可以使用缩写来更正常见的输入错误。比如,下面的命令
|
||||||
|
|
||||||
|
```
|
||||||
|
:ab recieve receive
|
||||||
|
```
|
||||||
|
|
||||||
|
将会自动更正拼写错误,就像你自己输入的一样。如果在一次特殊情况下你想阻止扩展/更正发生,那么你只需要在输入一个单词的最后一个字母以后按 “Ctrl + V” ,然后按空格键。
|
||||||
|
|
||||||
|
如果你想把刚才使用的缩写保存下来,从而当你下次使用 Vim 编辑器的时候可以再次使用,那么只需将完整的 ‘ab’ 命令(没有起始的冒号)添加到 ”/etc/vim/vimrc“ 文件中。如果想删除某个缩写,你可以使用 “una” 命令。比如: ‘una asap’ 。
|
||||||
|
|
||||||
|
|
||||||
|
### 3\. 分离窗口来进行‘复制/粘贴’
|
||||||
|
|
||||||
|
有时,你需要将一段代码或文本的一部分从一个文件复制到另一个。当使用 GUI(图形界面)编辑器的时候,这很容易实现,但是当使用一个命令行编辑器的时候,这就变得比较困难并且很费时间。幸运的是, Vim 提供了一种高效、节约时间的方式来完成这件事。
|
||||||
|
|
||||||
|
打开两个文件中的一个然后分离 Vim 窗口来打开另一个文件。可以通过使用 ‘split’ 命令并以文件名作为参数来完成这件事。比如:
|
||||||
|
|
||||||
|
```
|
||||||
|
:split test.c
|
||||||
|
```
|
||||||
|
|
||||||
|
上面的命令将分离窗口并打开文件 “test.c”
|
||||||
|
|
||||||
|
![vim-split](https://maketecheasier-2d0f.kxcdn.com/assets/uploads/2014/08/vim-split.png "vim-split")
|
||||||
|
|
||||||
|
注意到 ‘split’ 命令水平分离 Vim 窗口。如果你想垂直分离窗口,那么你可以使用 ‘vsplit’ 命令。当同时打开了两个文件并从一个文件中复制好内容以后,按 “Ctrl + W” 切换到另一个文件,然后’粘贴’。
|
||||||
|
|
||||||
|
### 4\. 保存一个没有权限的已编辑文件
|
||||||
|
|
||||||
|
有时候当你对一个文件做了大量更改以后才会意识到该文件仅有 ‘只读’ 权限。
|
||||||
|
|
||||||
|
![vim-sudo](https://maketecheasier-2d0f.kxcdn.com/assets/uploads/2014/08/vim-sudo.png "vim-sudo")
|
||||||
|
|
||||||
|
虽然把文件关闭,获取权限以后再重新打开是一种解决方法。但是如果你已经做了大量更改,这样做会很浪费时间,因为在这个过程中所有的更改都会丢失。 Vim 提供了一种方式来处理这种情况:你可以在编辑器中在保存文件前更改文件权限。命令是:
|
||||||
|
|
||||||
|
```
|
||||||
|
:w !sudo tee %
|
||||||
|
```
|
||||||
|
|
||||||
|
这个命令将会向你询问密码,就像在命令行中使用 ‘sudo’ 一样,然后就能保存更改。
|
||||||
|
|
||||||
|
**一个相关的技巧**:在 Vim 中编辑一个文件的时候,如果想快速进入命令行提示符,可以在编辑器中运行 ‘:sh’ 命令,从而你将进入一个交互的 shell 中。完成以后,运行 ‘exit’ 命令可以快速回到 Vim 模式中。
|
||||||
|
|
||||||
|
### 5\. 在复制/粘贴过程中保持缩进
|
||||||
|
|
||||||
|
大多数有经验的程序员在 Vim 上工作时都会启用自动缩进。虽然这是一个节约时间的做法,但是在粘贴一段已经缩进了的代码的时候会产生新的问题。比如,下图是我把一段已缩进代码粘贴到一个在自动缩进的 Vim 编辑器中打开的文件中时遇到的问题:
|
||||||
|
|
||||||
|
![vim-indentation](https://maketecheasier-2d0f.kxcdn.com/assets/uploads/2014/08/vim-indentation.png "vim-indentation")
|
||||||
|
|
||||||
|
这个问题的解决方法是 ‘pastetoggle’ 选项。
|
||||||
|
在 ‘/etc/vim/vimrc’ 文件中加入下面这行内容:
|
||||||
|
|
||||||
|
```
|
||||||
|
set pastetoggle=<F2>
|
||||||
|
```
|
||||||
|
|
||||||
|
然后当你在 ‘插入’ 模式中准备粘贴代码前先按 ‘F2’ 键,就不会再出现上图中的问题,这样会保留原始的缩进。注意,你可以用其他的任何键来代替 ‘F2’ 如果它已经映射到了别的功能上。
|
||||||
|
|
||||||
|
### 结论
|
||||||
|
|
||||||
|
更进一步的提高你的 Vim 编辑器技巧的唯一方法是,在你日复一日的工作中使用命令行编辑器。留意那些耗时多的操作,然后尝试去寻找是否有编辑器命令可以很快地完成这个操作。
|
||||||
|
|
||||||
|
--------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
via: https://www.maketecheasier.com/vim-tips-tricks-for-experienced-users/
|
||||||
|
|
||||||
|
作者:[Himanshu Arora][a]
|
||||||
|
译者:[ucasFL](https://github.com/ucasFL)
|
||||||
|
校对:[校对者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/himanshu/
|
||||||
|
[2]:https://www.maketecheasier.com/vim-tips-tricks-for-experienced-users/#comments
|
||||||
|
[3]:https://www.maketecheasier.com/start-with-vim-linux/
|
||||||
|
[4]:https://www.maketecheasier.com/vim-keyboard-shortcuts-cheatsheet/
|
||||||
|
[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%2Fvim-tips-tricks-for-experienced-users%2F
|
||||||
|
[8]:http://twitter.com/share?url=https%3A%2F%2Fwww.maketecheasier.com%2Fvim-tips-tricks-for-experienced-users%2F&text=5+Vim+Tips+and+Tricks+for+Experienced+Users
|
||||||
|
[9]:mailto:?subject=5%20Vim%20Tips%20and%20Tricks%20for%20Experienced%20Users&body=https%3A%2F%2Fwww.maketecheasier.com%2Fvim-tips-tricks-for-experienced-users%2F
|
||||||
|
[10]:https://www.maketecheasier.com/enable-two-step-verification-apple-icloud-account/
|
||||||
|
[11]:https://www.maketecheasier.com/mistakes-wordpress-user-should-avoid/
|
||||||
|
[12]:https://www.maketecheasier.com/series/vim-user-guide/
|
||||||
|
[13]:https://support.google.com/adsense/troubleshooter/1631343
|
@ -0,0 +1,101 @@
|
|||||||
|
3 个针对高级用户的 Vim 编辑器实用技巧
|
||||||
|
============================================================
|
||||||
|
|
||||||
|
![](https://maketecheasier-2d0f.kxcdn.com/assets/uploads/2017/01/vim-featured.jpg "3 Useful VIM Editor Tips and Tricks for Advanced Userss")
|
||||||
|
|
||||||
|
这篇文章是[ Vim 用户指南][12]系列文章中的一篇:
|
||||||
|
|
||||||
|
* [Vim 初学者入门指南][3]
|
||||||
|
* [Vim 快捷键速查表][4]
|
||||||
|
* [5 个针对有经验用户的 Vim 技巧][4]
|
||||||
|
* 3 个针对高级用户的 Vim 编辑器实用技巧
|
||||||
|
|
||||||
|
毫无疑问, Vim 是一个很强大的文本编辑器。它提供了大量的特性,这意味着学习并记住 Vim 的所有功能实际上是不可能的。但是我们至少可以
|
||||||
|
不断学习简单的方法来完成事情,从而随着时间的增长,我们使用编辑器的经验将会变得更好。
|
||||||
|
|
||||||
|
请记住,在这篇文章中我们将讨论的一些 Vim 编辑器技巧是针对高级用户的。
|
||||||
|
|
||||||
|
**注**:如果你是第一次接触 Vim,你可以首先阅读我们的[入门指南][14]。对于已经使用过 Vim 编辑器的用户,我确信[ Vim 快捷键速查表][15]将会对你很有帮助。如果你已经是一名有经验的用户,你可能对[一些针对有经验用户的技巧][16]比较感兴趣。
|
||||||
|
|
||||||
|
请注意文中提到的所有技巧绝大多数都是在简单、易于理解的代码环境中进行阐述的,因为它们在软件开发中确实很实用。但这并不意味着普通用户(非程序员、没有把 Vim 作为一般的文本编辑器)在他们的工作中用不到。
|
||||||
|
|
||||||
|
### 1\.为文件设置特殊变量
|
||||||
|
|
||||||
|
有时候,在一个特殊文件中,你可能想把输入的某个字母用空格代替,或者想把一个源代码文件使用两个空格缩进,而编辑器的默认缩进是四个空格。
|
||||||
|
|
||||||
|
我们在这儿基本讨论对特殊文件进行的更改。 Vim 提供了一个特性允许你对一个特殊的文件更改特定的设置。这个特性叫做 “Modeline” 。
|
||||||
|
|
||||||
|
比如,如果你想把输入的每一个制表符 (Tab) 用空格代替,那么你需要做的就是考虑在文件的前几行或最后几行加入下面的 ‘modeline’ :
|
||||||
|
|
||||||
|
```
|
||||||
|
# vim: set expandtab:
|
||||||
|
```
|
||||||
|
|
||||||
|
如果想把默认缩进从 4 个空格变成 2 个空格,可以在源文件中添加下面的 ‘modeline’ :
|
||||||
|
|
||||||
|
```
|
||||||
|
// vim: noai:ts=2:sw=2
|
||||||
|
```
|
||||||
|
|
||||||
|
在使用 ‘modeline’ 时,请记住下面这几个重要的点:
|
||||||
|
|
||||||
|
* ‘Modeline’ 只能添加在文件中的前五行或者最后五行。
|
||||||
|
* 为了使用 “modeline” 选项这个特性,必须在 “.vimrc” 文件中添加 ‘:set modeline’ 。
|
||||||
|
* 在以 root 用户身份对文件进行编辑的时候该特性失效。
|
||||||
|
|
||||||
|
了解更多的信息,请阅读该特性的[官方文档][17]
|
||||||
|
|
||||||
|
### 2\. 关键字补全
|
||||||
|
|
||||||
|
当你开始写更多的复杂代码或者开始开发大量的源文件时,你需要设置一些变量名字。有时,要记住所有的变量名字不太容易,所以当需要输入变量名字的时候,你通常从已经使用过的地方复制过来。
|
||||||
|
|
||||||
|
幸运的是,使用 Vim 你只需要输入变量的几个起始字母即可。在’插入模式’中,按 “Ctrl + n” 或者 “Ctrl + p” 可以得到匹配关键词。 “Ctrl + n” 用来插入下一个匹配词; “Ctrl + p” 给出一系列过去的匹配词。
|
||||||
|
|
||||||
|
下图是该特性的一个展示:
|
||||||
|
|
||||||
|
![vim-keyword-completion1](https://maketecheasier-2d0f.kxcdn.com/assets/uploads/2017/01/vim-keyword-completion1.jpg "vim-keyword-completion1")
|
||||||
|
|
||||||
|
正如上面的屏幕截图清晰展示的那样,列表中也会出现其他源文件中包含的词。关于该特性的更多信息,请访问[这儿][18]。
|
||||||
|
|
||||||
|
### 3\. 搜索
|
||||||
|
|
||||||
|
假设你正在调试代码,其中一部分需要做的工作是快速查看一个变量在一个文件中所有出现的地方。一个常用的方法是退出‘插入模式’,输入 ‘/[变量名字]’ 命令,按 ‘Press’ ,然后返回‘插入模式’,使用 “n” 和 “p” 关键字。
|
||||||
|
|
||||||
|
上面讲到的方法是挺好的,但是还有一种更简单、更快捷的方法可以来完成这样的搜索。使用这种方法,首先你需要退出‘插入模式’,然后把光标移动到你想要搜索的词/变量下面,这并不费时。接下来,你只需要按 “Shift + .” 即可。
|
||||||
|
|
||||||
|
重复这样做,然后编辑器将会带你找到在文件中所有使用了这个词/变量的地方。
|
||||||
|
|
||||||
|
### 结论
|
||||||
|
|
||||||
|
尽管是针对高级用户,但文章中讨论的这些技巧并不难理解,也比较容易使用。如果你具有一定的基础,那么你能够从中获益很多。不必多说,无论是任何新特性或观念,你需要勤于练习这些技巧才能够把它们变成一种习惯。
|
||||||
|
|
||||||
|
|
||||||
|
--------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
via: https://www.maketecheasier.com/vim-tips-tricks-advanced-users/
|
||||||
|
|
||||||
|
作者:[Himanshu Arora][a]
|
||||||
|
译者:[ucasFL](https://github.com/ucasFL)
|
||||||
|
校对:[校对者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/himanshu/
|
||||||
|
[2]:https://www.maketecheasier.com/vim-tips-tricks-advanced-users/#respond
|
||||||
|
[3]:https://www.maketecheasier.com/start-with-vim-linux/
|
||||||
|
[4]:https://www.maketecheasier.com/vim-keyboard-shortcuts-cheatsheet/
|
||||||
|
[5]:https://www.maketecheasier.com/vim-tips-tricks-for-experienced-users/
|
||||||
|
[6]:https://www.maketecheasier.com/category/linux-tips/
|
||||||
|
[7]:http://www.facebook.com/sharer.php?u=https%3A%2F%2Fwww.maketecheasier.com%2Fvim-tips-tricks-advanced-users%2F
|
||||||
|
[8]:http://twitter.com/share?url=https%3A%2F%2Fwww.maketecheasier.com%2Fvim-tips-tricks-advanced-users%2F&text=3+Useful+VIM+Editor+Tips+and+Tricks+for+Advanced+Users
|
||||||
|
[9]:mailto:?subject=3%20Useful%20VIM%20Editor%20Tips%20and%20Tricks%20for%20Advanced%20Users&body=https%3A%2F%2Fwww.maketecheasier.com%2Fvim-tips-tricks-advanced-users%2F
|
||||||
|
[10]:https://www.maketecheasier.com/opt-out-google-personalized-ads/
|
||||||
|
[11]:https://www.maketecheasier.com/wi-fi-vs-ethernet-vs-4g/
|
||||||
|
[12]:https://www.maketecheasier.com/series/vim-user-guide/
|
||||||
|
[13]:https://support.google.com/adsense/troubleshooter/1631343
|
||||||
|
[14]:https://www.maketecheasier.com/start-with-vim-linux/
|
||||||
|
[15]:https://www.maketecheasier.com/vim-keyboard-shortcuts-cheatsheet/
|
||||||
|
[16]:https://www.maketecheasier.com/vim-tips-tricks-for-experienced-users/
|
||||||
|
[17]:http://vim.wikia.com/wiki/Modeline_magic
|
||||||
|
[18]:http://vim.wikia.com/wiki/Any_word_completion
|
Loading…
Reference in New Issue
Block a user