Communicate via the ex mode with your editor

One of the difficult situations new vim users get into is exiting the program: whereas in most applications, we are used to

  • Alt+f4,
  • Ctrl+q
  • A simple press of an Escape key,

    or such other standardised way, in vim it is a matter of knowing the right command. Though the command is unique to vim, it follows a particular pattern: you issue commands after pressing a colon key.

Getting into the EX Mode

Vim is a product of generations of editors. In the previous post, we learnt that vim means ‘Vi improved’, which tells us that it is a direct descendant of the vi editor.

Besides, the obvious heritage of vi, there is also a direct lineage to the ex and the ed editors.

An ex editor is a line editor. If you are on a Unix-based platform, try getting into the terminal and type: ex

You will see that a line editor will come up:

  • Just move around by issuing simple commands.
  • Press a number to go to a given line, for example 1p to print the contents of the first line.
  • Press the dot sign for the current line.
  • Press the - or + to move to a line above or below the current one.
  • Press s to substitute characters on a line.
  • To quit the Ex editor, just press the q key.
  • If you’ve made any change in a file with ex, just revert to the original copy with e!
  • In fact, the exclamation mark (!) is used to force an action.

Using the ex in vim

The ex mode is the third mode, after the normal and Insert modes we discussed in the last post. To get to the ex mode and issue the commands we just discussed, in vim, press the colon (:) key, for example

::1

As a result, it follows that once you know the ex commands, you can apply them in vim after pressing the colon key.

Hence, the famous exit conundrum can be solved by simply pressing :q to quit.

If you’ve made any changes in a file, Vim will not let you exit until you confirm that indeed you want to ignore your changes. So remember the exclamation mark directive: just add the mark after the q like this: :q!

Other interesting file manipulation operations can be done with the ex commands like this:

Operation Command
Save :w <filename>
Force write :w!
Open new file :edit
Quit :q
Force quit :q!
   

Setting vim options in ex

One of the means by which ex is used for is to set editor options.

You need to know the option name, and type “set” in the ex window. For example, one of the annoying features of vim for many screen-readers is its announcement of the line and column as you type. You need to turn off the ruler setting. So you have to put the “no” like this:

:set noruler

To highlight code as you type a program, you have to turn on highlighting like this :set hl.

Tip:

To persist the changes you make with the “set” command, you have to enter them in your .vimrc file, which is stored in your home directory. Just remove the colon, and type “set <option>” in the ~/.vimrc file.

Conclusion

Switching from one mode to another is critical to vim productivity. In this entry, we saw how the traditional ex editor can be leveraged with vim to provide powerful commands.

In the next entry, we are going to look at yet another mode, the visual mode. We will be looking at vim operators.<