≡ Menu

Vi and Vim Editor: 5 Awesome Examples For Automatic Word Completion Using Ctrl-X Magic

Vim Word完成提示和技巧
Photo courtesy of Dominik Gwarek

这是SathiyaMoorthy撰写的来宾帖子

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:

  1. 单词/图案完成
  2. Line completion
  3. 字典单词补全
  4. 文件名补全
  5. 填充具有相似含义的词(同义词库词完成)。


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
使用Control-X的Vim Word完成

Fig: 使用Ctrl-x Ctrl-n在Vi / Vim中完成关键字


程序员和管理员的典型用法:

  • 完成文件中的现有单词,而无需重新键入。
  • Complete a variable name or a function name on your program or shell-scripts.

2.使用Ctrl-x Ctrl-l在Vim中完成行

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.

使用Control-X完成Vim行

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.

3.使用Ctrl-x Ctrl-f完成文件名

Insert the name of the file that resides anywhere on the Linux system using the short cut key “Ctrl+x Ctrl+f”.

使用Control X完成Vim文件名

Fig: Vim使用Ctrl-x Ctrl-f完成文件名

Typical usage: When you give the path of the file name as a reference inside a document.

4.词库词完成

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编辑器的同义词库功能.

5.使用Ctrl-x Ctrl-k完成字典单词

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.

使用Control X的Vim词典单词完成

Fig: 使用Ctrl-x Ctrl-k的Vim词典单词完成

Final Note:

  • 验证是否在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.

Recommended Reading

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.

如果您喜欢这篇文章,您可能还会喜欢..

  1. 50个Linux Sysadmin教程
  2. 50个最常用的Linux命令(包括示例)
  3. 排名前25位的最佳Linux性能监视和调试工具
  4. 妈妈,我找到了! – 15个实用的Linux Find命令示例
  5. Linux 101 Hacks第二版电子书 Linux 101 Hacks Book

Bash 101 Hacks Book Sed和Awk 101黑客手册 Nagios Core 3 Book Vim 101 Hacks Book

{ 12 comments… add one }

  • Pushpraj Nimbalkar 2009年1月9日,晚上11:11

    哇……………….Very Informative……

  • Chethan 2009年1月10日,上午3:29

    非常有用的提示… Thanks a lot

  • elleryq 2009年1月13日,上午9:52

    great!!
    我学到另一个有用的提示!

  • Ryan Paul 2009年2月10日,上午1:58

    Wow, I didn’t know about c-x c-f. That’s really useful. Thanks for sharing!

  • Rbowlby 2010年1月6日,上午4:10

    You broke down the types of VIM completion perfectly. What would complement this post well is a post on setting up language specific dictionaries for the dictionary completion. Thanks for saving ~30min of man page sifting. -

  • Uma Suresh 2010年4月26日,下午12:54

    非常感谢所有有用的提示。

  • Uma Suresh 2010年4月27日,上午9:03

    Hi Gurus,
    Is there a way to view the file list in the current directory while still in vim. Something like‘ls’?

  • Uma Suresh 2010年4月27日,下午2:04

    I found this very useful command for exploring files in the current directory in a horizontal while in Vim.
    :Sexplore

  • Celsius1414 2010年5月5日,下午4:24

    @Uma you can use the built-in file explorer to view the current working directory by entering“:e .” (colon e period)

  • Uma Suresh 2010年5月11日,下午1:32

    感谢Celsius1414的提示。

  • genick 2011年3月28日,上午11:09

    I can figure out why in the previous version ^P was working and doing autocompletion.
    现在,它已损坏,无法执行^ x ^ p。
    实际上,以上提供的说明均无效。
    是否需要进行更改才能使其再次运行。

  • hblan 2011年11月21日,上午8:10

    this makes me remember the SuperTab . it is using the ctrl-x ctrl-n in it`s script functions . woo . more clear in understanding SuperTab .

Leave a Comment