Vim Recipes – A cookbook for the Vim text editor

Categories:

Recommended

Choosing the Right Mode

Problem

You know that Vim has multiple modes of operation but aren’t sure when to use which one.

Solution

For practical purposes there are four modes:

  • Insert mode:  Use only for typing; not moving around or editing. Stay in this mode for as short a time as possible.
  • Normal mode:  Use this for editing: moving around the file, changing text, and rearranging structure. Dip in and out of Insert mode when needed.
  • Visual mode:  Use this for visually selecting text so that you can cut, copy, or format it.
  • Command-Line mode:  Use this for entering commands, e.g. :set number

Discussion

Vim’s modal approach to editing can seem confusing, but it really is the key to understanding Vim.

It’s tempting to spend much of your time in Insert mode, and navigate with the arrow keys. However, this is slow and requires an awful lot of key presses.

Normal mode is the default mode because it makes it so easy to move around the file to either edit existing text or position the cursor where you want to insert text.

✪ Use <Ctrl>+o in Insert mode to switch to Normal mode for one command, then

return to Insert mode. For example, <Ctrl>+o gqas enters Normal mode, reformats the current sentence,1 then returns you to Insert mode.

If you create a new file, and just want to type, by all means go straight into Insert mode and do so. All other times, though, stay in Normal mode.

For example, you want to find a paragraph you’ve written previously, and reword it. In Normal mode you can either search for it (e.g. /Hobson argued), or simply page through the file (e.g. <Ctrl>+F to scroll downwards) to find it. Once there, you can move to the section you’re interested in using either the basic movement commands or text objects. You can now use text objects again to select something and change it. For example caw deletes the current word and puts you into Insert mode to change it. Once you have done so, hit <Esc> again to return to Normal mode.

Category:

Attribution

Run Paint Run Run (2009), Vim Recipes, URL: https://github.com/runpaint/vim-recipes

This work is licensed under Attribution-ShareAlike 3.0 Unported License:  (https://creativecommons.org/licenses/by-sa/3.0/).

VP Flipbook Maker

Display your work as a digital flipbook to increase engaging and interaction with your readers! VP Online Flipbook Maker is a powerful tool for you to convert your work to a flipbook, and also create from scratch. Try it now!