Photo courtesy of Dominik Gwarek
You can perform automatic word completion in Vim using Ctrl-x in insert or append mode. By typing the first few characters of a word you can get the whole word either from a dictionary, or a thesaurus, or even the words that are already present on the file that you are editing.
This article explains how to perform following operations with examples and screen shots:
- Line completion
All the completions are related, you can use the same keys for navigating between several choices shown, and choose the appropriate one by pressing enter.
本文是正在进行中的一部分 Vi / Vim提示和技巧 series.
1.使用Ctrl-x Ctrl-n在Vi / Vim中完成单词/图案完成
You can use one of the following Vim shortcut keys to choose the existing words on the file.
- CTRL-x CTRL-n：单词补全– forward
- CTRL-x CTRL-p：单词补全– backward
Fig: 使用Ctrl-x Ctrl-n在Vi / Vim中完成关键字
- Complete a variable name or a function name on your program or shell-scripts.
If you want to insert an existing line, type the first few words/characters of the line, and then press the Vim shortcut keys “Ctrl+x Ctrl+l”, which will display all the lines matching that patten. Note: The second control character is a lowercase L.
Fig: Vim使用Ctrl-x Ctrl-l完成整行
- Automatically fill first line of the function definition, when the function is already declared elsewhere in the same file.
Insert the name of the file that resides anywhere on the Linux system using the short cut key “Ctrl+x Ctrl+f”.
Fig: Vim使用Ctrl-x Ctrl-f完成文件名
Typical usage: When you give the path of the file name as a reference inside a document.
Using this feature you can choose alternate words with the same meaning for the word that you’ve just typed. Our previous article explains about Vim编辑器的同义词库功能.
Enable the dictionary in Vim by adding the following line to ~/.vimrc.
$ cat ~/.vimrc set dictionary+=/usr/share/dict/words
This is a great feature when you stumble for the correct spelling for a word that you are typing. After typing the first few characters, Press the Vim short cut key Ctrl-x Ctrl-k to display the matching dictionary words.
- 验证是否在Vi / Vim中启用了autocmd –Execute :version from vi / vim. If autocommand feature is enabled, it will display +autocmd.
- 插入模式完成帮助 –Execute :help ins-completion from vi / vim, to get quick help on insert mode completion features.
Vim 101 Hacks，作者：Ramesh Natarajan. I’m a command-line junkie. So, naturally I’m a huge fan of Vi and Vim editors. Several years back, when I wrote lot of C code on Linux, I used to read all available Vim editor tips and tricks. Based on my Vim editor experience, I’ve written Vim 101 Hacks eBook that contains 101 practical examples on various advanced Vim features that will make you fast and productive in the Vim editor. Even if you’ve been using Vi and Vim Editors for several years and have not read this book, please do yourself a favor and read this book. You’ll be amazed with the capabilities of Vim editor.