Boost Your Coding Fu With VSCode and Vim
Chapter 13.Table of ContentsCheatsheet

Control VSCode With Command-line Mode

Command-line mode is yet another mode in Vim. Its defining feature is the ability to run Ex commands (commands that start with :) and search patterns (which start with / and ?). We’ve already discussed searching in earlier chapters so we’ll focus on Ex-commands in this one.

Ex commands are very useful and diverse: Some let you configure Vim (f.i. :colorscheme), others allow you perform system-wide operations (like creating a new file with :edit), some others access external shell commands (:!), yet others let you quickly edit multiple lines within a document with the wave of a hand (like :delete).

VSCodeVim has support for only a very limited number of Ex commands but they can be greatly enhanced by integrating VSCodeVim with Neovim. This chapter will introduce Command-line mode, and we will leave Neovim for towards the end of the book when you’ve become a more seasoned Vim practitioner. Let’s dive in!

Typing an Ex Command

A common Ex command is :edit. It lets you open or creating a file by typing:

:edit {relative-path-to-file}

Notice the : before this command? Unlike Normal mode commands, Ex commands all start with a colon. It is typing this colon what triggers Command-line mode and sets you up to enter a command.

To type an Ex command you literally type : followed by the name of the command (e.g. :edit). When you type a colon and a command, the command will be displayed in the lower-left part of the screen on the VSCode status bar. Let’s try it!

Create a new file by typing:

:edit helloworld.js

You should see how, as you type a colon (:) and the name of the command, a cursor and letters materialize at the bottom of the screen inside the status bar.

Typing an ex command in command line mode

When you finish typing the command and press <Enter>, a new file helloworld.js will be created and VSCode will open it so that you can start coding. If the filename that you specify belongs to an existing file, then you’ll just open that file.

As usual, Vim always tries to save you work and this is not different in Command-line mode: Every Ex command has a shorthand version that allows you to trigger a command with just a few letters. For instance, the shorthand version of :edit is :e. Try the shorthand with our previous example and you’ll see how you get the same results.

Saving and Closing Files

A couple of nifty Ex commands let you to save and close files quite rapidly:

  • Use :write (shorthand :w) to save a file
  • Use :quit (shorthand :q) to close a file

These two commands are what I like to call soft commands. They’ll attempt to perform the action they describe but, under some conditions, they’ll fail and prompt you to take further action. For instance, :write will save a file but will fail if the file hasn’t changed or if it is readonly. Likewise :quit will close a file but will fail if the file has unsaved changes.

If you want to ignore any complains and perform an action at all costs, you can combine the previous commands with a !. You can think of the ! as a way to force a commandforce and thus:

  • Use :write! (shorthand :w!) to save a file even if it’s been saved already or if it is readonly
  • Use :quit! (shorthand :q!) to close a file without saving.

You can combine these commands to perform multiple actions:

  • Use :wq to save and close a file

Or apply them to all the opened files at once:

  • Use :wall (shorthand :wa) to save all files
  • Use :qall (shorthand :qa) to close all files
  • Use :wqall (shorthand :wqa) to save and close all files
  • Use :qall! (shorthand :qa!) to close all files without saving

Deleting Multiple Lines At Once

In earlier chapters you learned that Vim has a bunch of operators that allow you to perform changes in text: d to delete, c to change, y to yank, etc.

Vim also provides a series of Ex commands that perform equivalent actions to the Normal mode operators but with a different use case in mind: Operating on multiple lines at once.

These text-editing Ex commands take the following shape:


Where range defines a range of lines to which to apply the command and options vary depending on the command itself. For instance, in the case of :delete we have:

:[range]d [register]

Where register represents a register in which to cut whatever it is we delete. For instance:

:10,12d a

Deletes the lines 10, 11 and 12 and puts them inside the a register. As you can appreciate above, ranges are generally defined by their extremes: An initial line and an ending line. These extremes can, in turn, be expressed in different ways:

  • Using numbers (e.g. :10,12d to delete lines 10, 11 and 12)
  • Using offsets (e.g. :10,+2d to delete lines 10, 11 and 12)
  • Using the current line represented by . (e.g. :.,+2d to delete the current line and the next two ones)
  • Using % to represent the whole file (e.g. :%d to delete the whole file)
  • Using 0 to represent the beginning of the file (e.g. :0,+10d to delete the first 10 lines)
  • Using $ to represent the end of the file (e.g. :.,$d to delete from the current line to the end of the file)
  • If you use Visual mode to make a text selection and then type : your command line area will be pre-populated with the following gobbledygook: :'<,'> which is a special range that represents the current visual text selection. (e.g. :'<,'>d means delete the current text selection)

Why use Ex commands instead of the normal mode commands we learned in previous chapters? Ex commands are useful because they allow you to apply a command over a range of lines without needing to move the cursor to that location first. Whenever you need to apply a change over multiple lines, consider using Ex commands. It may be a faster approach than Normal mode.

Other useful Ex command alternatives to Normal mode commands are :yank, :put, :copy and :move but they’re not supported in VSCodeVim unless we enable the integration with Neovim get.

Repeating Ex Commands

Just like you can repeat Normal mode commands with the dot operator, Ex commands also have a repeater command. Type @: and you will repeat the last ex command, from then on you can repeat it again with @@.

Substituting Text

Another useful Ex command is :substitute. It allows you to substitute arbitrary bits of text for others of your choosing. The shape of this command is:



  • range defines the range in which we’ll apply the substitution
  • pattern is a search pattern that describes the text we want to change. Like /{pattern} it supports regular expressions.
  • substitute is the text we want to substitute
  • flags let us set options that configure the substitution

For example, the following command:


transmutes the first occurrence of led in the current line into gold.

If we want to change all occurrences in the current line then we need to add the g flag or global flag like so:


And if we want to change all occurrences for a whole file we just need to specify the entire file as a range with % like this:


In addition to the g flag we can use:

  • i for case insensitive searches
  • c to confirm each and every substitution

The fact that we can use regular expressions with the :s command opens the door to a lot of creative use cases. For instance, we could change the indentation level of all titles in a markdown file by using the following :s command:


Which can be translated to:

  • % for the whole file
  • s substitute
  • ^# any # at the beginning of a line (i.e. a header in markdown)
  • // for an empty character

  1. If you have trouble remembering to use ! just picture yourself shouting at your editor: Close that file!!!! Unpleasant but effective.

  2. Worry not! We’ll get there soon enough.

Jaime González García

Written by Jaime González García , Dad, Husband, Front-end software engineer, UX designer, amateur pixel artist, tinkerer and master of the arcane arts. You should follow him on Twitter where he shares useful stuff! (and is funny too).Jaime González García