Linux Format

TERMINAL: Vim

While any vanilla text editor will suffice for routine tasks, Shashank Sharma only trusts Vim for more profession­al work.

- Shashank Sharma is a trial lawyer in New Delhi and avid Arch user. He’s always on the hunt for geeky memorabili­a.

While any vanilla text editor will suffice for routine tasks, Shashank Sharma only trusts Vim for more profession­al work. dd Emacs is quite good too…

Abundance of choice has plagued just about every applicatio­n category in the open source arena, including text editors. Apart from applicatio­ns, users also get the choice of interfaces: graphical or command line. The latter option includes

joe, nano, Emacs and the ever popular vim. Vim, a contractio­n of Vi Improved, was first released in November 1991 as a clone of the original vi text editor, with many additional features thrown in. Over the past 27 years, vim has continued to increase its user base and has been at the heart of one of the most hotly debated issues: that of its standing against Emacs, another fiercely popular and featureful editor.

Most modern desktop distributi­ons ship with vim out of the box, or carry it in their software repositori­es. You can install it by running sudo apt install vim or sudo dnf install vim , depending on your distro. There are a large number of keyboard functions that are universal when you’re working with text files, whether prose or code. You may need to replace words, move up a line, or to the end, and so on. Vim features a large number of keyboard shortcuts to help you accomplish all this and more.

Unlike most regular text editors, vim has enough features to require different modes to accommodat­e its many capabiliti­es. These are the Normal, Visual and the rather straightfo­rward Insert mode, which is used when you want to insert text into a file. The Normal mode is the default mode in which vim is launched. This mode is where you can type commands. You can invoke the Insert mode by pressing A or I keys. To enter the Normal mode, you must hit the Esc key. Refer to the box (left) for more details on how to work with the Visual mode.

If you’re already familiar with vim, but don’t understand what makes it special, this tutorial is just for you. We’ll go over some of the quick solutions you can employ to turn vim from a humble text editor into a versatile word processor.

Smart search

The ~/.vimrc file is where you can store user-specific configurat­ion options, whereas the global config options are placed in the ~/etc/vim/vimrc file. To begin, you

might want to enable the option to highlight search results for your files. Also, by default search looks for the entire keyword, but you can enable incrementa­l search, which means that vim will start looking for matching words as soon as you type the first character. With each new character, vim refines the search results. Both these settings can be enabled by adding the following text to your ~/.vimrc file:

set hlsearch set incsearch

You can also add set ignorecase so that upper-case matches are identified even when your search string is lower-case, or vice-versa. The set smartcase option can be used to inform vim to respect the cases if upper case is used. If the ~/vimrc file doesn’t yet exist on your installati­on, you can create it with the vim ~/.vimrc command. Next, enter Insert mode by pressing A, and type in the above two commands. Save the file by typing

:wq .

To search for matching words, type / followed by the keyword from the Normal mode and hit Enter. Vim will highlight all matching words and you’ll be able to move forward and backward through the matching words by pressing n or N respective­ly.

Auto-completion

Another useful feature is word completion. Whether it’s in a script that makes frequent references to a variable or function, or your next adventure novel with frequent mention of the same character or place, you can use the auto-completion feature to save valuable time. From the Insert mode, type in the first few letters or characters of the keyword you’re confident you’ve already used in the file, and then press Ctrl+p. Vim will find the previously used matching word and auto-complete the current word in your file.

You can similarly use Ctrl+n to scan ahead through the file to find matches. If the editor finds multiple matches, you’ll be presented with a scrollable list. Use the arrow keys to highlight the one you want to use and press Enter.

Similar to word completion, vim also lets you auto-complete matching lines. If your character has a catchphras­e (what are you writing?–ed) that is repeated multiple times, you can type in the first few characters, and then press Ctrl+x followed by Ctrl+l.

Check it out

The rules of the English language may seem bizarre, especially if you’re not a native speaker, or otherwise have a hard time rememberin­g the (mostly useless) mnemonic rules such as ‘I before E except after C’. You will appreciate the editor’s support for spell-checking, dictionary and thesaurus.

To enable a dictionary, you must point vim to a list of words. You can run the echo set dictionary+=/usr/ share/dict/words >> ~/.vimrc command to inform vim of the dictionary you want to use. If you want to use multiple dictionari­es, you can also do that by adding them to the ~/.vimrc file: set dictionary+=/usr/share/dict/words set dictionary+=/usr/share/dict/british-english set dictionary+=~/documents/legal-glossary

You can similarly add different dictionari­es for use with vim. When working with a file, type a few words and then press Ctrl+x then Ctrl+k. Vim will then produce a scrollable list of all matching words from the different dictionary files.

As well as dictionari­es, you can also configure vim to work with a thesaurus, such that it will provide synonyms for words. Add set thesaurus ~/.vim/ thesaurus/mthesaur.txt to the ~/.vimrc file. The command uses the thesaurus from Project Gutenberg (www.gutenberg.org/files/3202/files/mthesaur. txt). When you now press Ctrl+x Ctrl+t, vim provides a list of synonyms for use in place of the current word.

With a thesaurus in place, you might also want to add set complete+=s to the ~/vimrc file. This option will enable you to use the thesaurus for recommenda­tion on auto-completing words, instead of just matching words in the current text file.

Going further

With only a few tweaks, we’ve already turned the vanilla text editor into a powerful word processor. But there’s still something missing. Unlike text editors, word processors are quite adept at pointing out spelling mistakes. Just add set spell and set spelllang=en_gb in the ~/.vimrc file if you want on-the-fly spell-checking capability available.

Finally, the vim editor does a poor job of wrapping lines by default. Use the set wrap and the set linebreak options to configure vim to break lines at sensible places, and not just at the end of the screen, which often leads to a word being broken across two lines.

As useful as these configurat­ion options are, we’ve only scratched the surface of what’s possible with vim. Part of its appeal lies in the ability to provide additional functional­ity with the use of plug-ins. We’ll discuss this in a follow-up tutorial in a future issue.

 ??  ?? From within Vim, you can run the ‘:options’ command to get a list of all possible configurat­ion options.
From within Vim, you can run the ‘:options’ command to get a list of all possible configurat­ion options.
 ??  ??
 ??  ?? Move the cursor next to an incorrectl­y spelt word and type ‘z=’ in Normal mode. Vim presents a list of suggestion­s to correct the mistake.
Move the cursor next to an incorrectl­y spelt word and type ‘z=’ in Normal mode. Vim presents a list of suggestion­s to correct the mistake.

Newspapers in English

Newspapers from Australia