Step 1 of 6

18. Ex Commands Addresses And Ranges

Basic Ex commands

You issue Ex commands using the Vim command line, accessed through : in normal mode. Many of the commands that you use on a day-to-day basis might be inherited Ex commands.

To show you how ubiquitous they are: if you know how to write a buffer to a file in Vim (hint: :w), congratulations! You’ve been using Ex commands all along. To be precise, :w is an Ex command that is usually used to write the contents of the buffer to a file.

There are lots of Ex commands — far too many to enumerate here — but here are a few that are useful time and time again:

  • :print, or just :p for short, is used to print a line to Vim’s output
  • :move, or just :m for short, is used to move a line/range from a source to a target
  • :copy, or just :t for short, is used to copy a line/range from a source to a target
  • :delete, or just :d for short, is used to delete a line/range
  • :yank, or just :y for short, is used to yank a line/range to a register
  • :put is used to put the contents of a register

Give this a try in the editor. Work through the checklist below: for each of the labeled lines, put your cursor on it and run the command it asks for.

  1. Print a line with :p Put your cursor on the PRINTING line, then type :p and hit Enter — Vim will echo the line below the editor.
  2. Move a line to the top with :m Line 0 means "above the first line" — put your cursor on the MOVING line, then type :m 0 and hit Enter.
  3. Copy a line to the top with :t Put your cursor on the COPYING line, then type :t 0 and hit Enter — the original will stay put.
  4. Delete a line with :d Put your cursor on the DELETING line, then type :d and hit Enter.
  5. Yank a line with :y Put your cursor on the YANKING line, then type :y and hit Enter — the line will be stored away, with no visible change yet.
  6. Put the yanked line with :put Type :put and hit Enter — the yanked line will appear below the cursor.
Print a line with :p Put your cursor on the PRINTING line, then type :p and hit Enter — Vim will echo the line below the editor.

Next: Move a line to the top with :m

Loading editor…