How to Turn Vim into a Word Processor

Person typing at laptop computer

Believe it or not, I use Vim as my every-day word processor. I like its simple layout and its modal design that favors keyboard commands above mouse clicks.

Although you probably know Vim as a text editor more suited for coding than for prose, with a few tweaks, you, too, can write documents like you’re in Word or LibreOffice while staying within the comfortable confines of a terminal.

Also read: How to Use Vim Shortcuts to Navigate Your Web Browser

Getting Started

If you haven’t already familiarized yourself with the basics of Vim, check out this article, which is the first in a series of four. Make sure you learn how to open a text file, enter and exit the program, and move around the screen with your keyboard before jumping further into this piece.

Installation

Your Linux distro probably comes with Vim in its package repository, so you can easily install it with your Software Manager or package manager. When you edit Vim’s config file later, you can do that as a normal user.

Arch Linux:

sudo pacman -S vim

Fedora:

sudo dnf install vim

Ubuntu:

sudo apt install vim

Everyone else can grab Vim from its Github repository:

git clone https://github.com/vim/vim.git
cd ./vim/src/
make
sudo make install

The Vim Configuration File

An installation of Vim may create a .vimrc file in your home directory. If it doesn’t, create one:

touch ~/.vimrc

You can open it with Vim itself:

cd
vim .vimrc

Our Word Processor

Now you’re probably looking at an empty file. Let’s change that.

We can create a function called “WordProcessor” that you can call up at any time. It will look like this:

func! WordProcessor()
  " movement changes
  map j gj
  map k gk
  " formatting text
  setlocal formatoptions=1
  setlocal noexpandtab
  setlocal wrap
  setlocal linebreak
  " spelling and thesaurus
  setlocal spell spelllang=en_us
  set thesaurus+=/home/test/.vim/thesaurus/mthesaur.txt
  " complete+=s makes autocompletion search the thesaurus
  set complete+=s
endfu
com! WP call WordProcessor()

Now that you see the function as a whole, we can break down its parts. Follow along by looking at the sections of our function commented out with a single quotation mark, the individual lines in gray.

1. Movement changes with map j gj and map k gk

What you see here is a change in how cursor movement works inside Vim. If you read the previous Vim articles mentioned at the beginning of this article, you will know that j moves your cursor down and k moves your cursor up. Similarly, gj and gk move you down and up, but they move the cursor by line on the screen rather than by line in the file.

Our mapping of j/k to gj/gk gives your cursor the ability to move up and down through wrapped lines on your screen. This helps when you’re typing long paragraphs and need to move your cursor easily to the middle of those paragraphs.

2. Formatting text with the “setlocal” commands

First, see setlocal formatoptions=1. “Formatoptions” lets you pick from a number of text-formatting modes. In the case of option “1,” Vim will refrain from breaking lines after a one-letter word. It will try, when possible, to break lines before the one-letter word instead.

Next, setlocal noexpandtab lets you keep Vim from changing tabs into spaces. As an example, the “expandtab” setting will tell Vim to change your press of the Tab key into a number of spaces that fits the same length. “Noexpandtab” does the opposite; it preserves your Tab press as a single character.

setlocal wrap tells Vim to wrap your text when it reaches the end of your screen. This is the behavior you would expect from a word processor.

Finally, setlocal linebreak gets Vim to break your lines at sensible places. It keeps your lines from being wrapped at the last character that fits on the screen. This is why, in part, you will see lines of text reach various points on the screen before they wrap to the beginning. Check out what it looks like with an unfinished copy of this article in the screenshot below

Vim line break example

3. Spelling and an offline thesaurus

Vim comes with a built-in spellcheck ability. You can make your “WordProcessor” function use that ability by using the command setlocal spell spelllang=en_us. You can define multiple languages here in a comma-separated list.

Find more language files at this Vim FTP site. Place your selected files (both .spl and .sug files for each language) in “$HOME/.vim/spell/.” Change your function’s line to set spelllang=en_us,nl,medical, for example, to check for English, Dutch, and medical words.

Misspelled words will show up as underlined and bold in your text.

Vim spellcheck

You can search for replacements by entering :z= in Normal mode. (Use the Escape key to get to Normal mode.) That command will produce a screen like the following image.

Vim misspelled word suggestions

You can use Vim’s thesaurus feature by telling it your chosen thesaurus directory with set thesaurus+=/home/<user>/.vim/thesaurus/mthesaur.txt or similar. You can find the “mthesaur.txt” file at Project Gutenberg. Place your downloaded text file into the directory you define with that command.

Search for words similar to your highlighted word by entering Insert mode (press i when in Normal mode), and typing Ctrl + x and then Ctrl + t. It will show you a list like the following image that you can scroll through to find a new word.

Vim thesaurus suggestions

Sometimes the suggestions aren’t the best.

4. Autocomplete words with the “complete” option

Look at set complete+=s for our final consideration. This option tells Vim to search your thesaurus for a word you want to autocomplete.

Normally, you can autocomplete words by having Vim look at previous words you’ve typed, such as “missspelled” and “mistake” in the following screenshot. You just enter Insert mode and type Ctrl + n or Ctrl + p to look for a word that, for instance, the half-spelled “mis” matches.

Vim autocomplete suggestions

See how the first two selections aren’t from the thesaurus? Those two lines are the only options you’d see here if our function had not used “complete+=s.” You can omit or comment out that line for quicker autocompletions.

Starting Your Word Processor

The final line of your .vimrc that makes the WordProcessor function work is com! WP call WordProcessor(). It lets you type :WP in Normal mode to set Vim’s word processing abilities.

You can edit the “WP” in this line to read anything you like – just make sure the text is contiguous. Whatever text you choose will then change the command to :<your-text>.

Conclusion

The functionality I’ve discussed here only lasts for a single Vim session. Therefore, when you close and reopen Vim, the default settings will take hold once again. This will happen even if you open the same file a second time.

You could set these options outside the function to have them work all the time, but they might not do justice to many of the programming tasks Vim was made to handle. It’s not hard to type a couple characters anyway. Just enjoy your new settings and the fact that you’ve turned your terminal into a powerful paragraph editor.

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox

Casey Houser

I have worked as a professional writer since 2011. I like to compose my articles in Vim, which I also use for hobbyist C and Ruby projects. When I'm not in front of a text editor, I run, bike, and play tennis until I'm too tired to move.