≡ Menu

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

Vim Word Completion Tips and Tricks
Photo courtesy of Dominik Gwarek

This is a guest post written by 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. Word / pattern completion
  2. Line completion
  3. Dictionary word completion
  4. File name completion
  5. Filling up words with similar meaning (Thesaurus word 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.

This article is part of the ongoing Vi / Vim Tips and Tricks series.

1. Word / Pattern Completion in Vi / Vim Using Ctrl-x Ctrl-n

You can use one of the following Vim shortcut keys to choose the existing words on the file.

  • CTRL-x CTRL-n : Word completion – forward
  • CTRL-x CTRL-p : Word completion – backward
Vim Word Completion Using Control-X

Fig: Keyword completion in Vi / Vim using Ctrl-x Ctrl-n

Typical usage for programmers and administrators:

  • Complete an existing word in the file, without retyping it.
  • Complete a variable name or a function name on your program or shell-scripts.

2. Line Completion in Vim using Ctrl-x Ctrl-l

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.

Vim Line completion using Control-X

Fig: Vim Whole line completion using Ctrl-x Ctrl-l

Typical usage for a programmers and administrators:

  • Repeat an existing line quickly in your file.
  • Automatically fill first line of the function definition, when the function is already declared elsewhere in the same file.

3. File Name Completion Using 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”.

Vim Filename Completion Using Control X

Fig: Vim File name completion using Ctrl-x Ctrl-f

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

4. Thesaurus Word Completion

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 the thesaurus features for your Vim editor.

5. Dictionary Word Completion Using 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.

Vim Dictionary Word Completion Using Control X

Fig: Vim Dictionary word completion using Ctrl-x Ctrl-k

Final Note:

  • Verify whether autocmd is enabled in Vi / Vim – Execute :version from vi / vim. If autocommand feature is enabled, it will display +autocmd.
  • Insert Mode Completion help – Execute :help ins-completion from vi / vim, to get quick help on insert mode completion features.

Recommended Reading

Vim 101 Hacks, by 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.

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

{ 12 comments… add one }

  • Pushpraj Nimbalkar January 9, 2009, 11:11 pm

    WOW……………….Very Informative……

  • Chethan January 10, 2009, 3:29 am

    very useful tips… Thanks a lot

  • elleryq January 13, 2009, 9:52 am

    I learn another useful tip!!

  • Ryan Paul February 10, 2009, 1:58 am

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

  • Rbowlby January 6, 2010, 4:10 am

    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 April 26, 2010, 12:54 pm

    Thanks a lot for the all the useful tips.

  • Uma Suresh April 27, 2010, 9:03 am

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

  • Uma Suresh April 27, 2010, 2:04 pm

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

  • Celsius1414 May 5, 2010, 4:24 pm

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

  • Uma Suresh May 11, 2010, 1:32 pm

    Thanks Celsius1414 for the tip.

  • genick March 28, 2011, 11:09 am

    I can figure out why in the previous version ^P was working and doing autocompletion.
    Now, it is broken and doing ^x^p does not working.
    Actually none of the instructions provided above working.
    Is there a change that need to be done to make it work again.

  • hblan November 21, 2011, 8:10 am

    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