197

Vim has an Ex mode that can be entered by entering Q, and a command line mode that can be entered with q:. A common complaint amongst new vim users is that they enter these modes accidentally when trying to quit vim. As such, I disable these keys in my ~/.vimrc to stop myself hitting them accidentally (particularly q:):

map q: <Nop>
nnoremap Q <nop>

Although I've read the vim documentation on Ex, am a moderately experienced Vim user, and understand the basic idea behind it, I still struggle to find any use for it in my daily vim use. In general it seems less useful than just entering a standard vim command-line command prefixed with :, as changes are not echoed straight away.

Does Ex have any practical everyday use in modern Vim? Is there anything that's easier to do in Ex mode than with standard commands? What is the difference between command line mode and ex mode?

11
  • 19
    Q and q: are quite different. You're only referring to Q right?
    – jamessan
    Commented Feb 5, 2015 at 20:21
  • @jamessan, actually, it turns out that I wasn't sure ... you're right, they do seem to do different things, although I had assumed they were basically the same. If you answer this question, it might help to clarify there, as I'm obviously confused. Commented Feb 5, 2015 at 20:25
  • 1
    This question is being discussed on Meta. Commented Feb 5, 2015 at 23:39
  • 1
    @AndrewFerrier You asked about q: specifically, and mentioned it as, among the two key sequences, the one you have the most issue with. And two of the answers here may be useful to people who are searching for it by the correct term.
    – Random832
    Commented Mar 2, 2015 at 19:41
  • 7
    When you learn what q/ and q: do, you'll probably consider them some of the most useful commands in Vim. And yes, you already know how to use them, you just don't know yet what they do. Watch this screencast to find out. Commented Oct 17, 2015 at 6:10

9 Answers 9

145

Q is, as you found, ex mode. It's not entirely useful to use interactively, but it exists because Vim can be used to emulate the old ex binary. In fact, many systems provide the ex command by simply symlinking it to vim.

q:, or :<C-f>, instead provides a way to browse your command-line history and edit it like a normal buffer. This makes it easy to find a previous command you ran, edit it with normal Vim commands, and then run the modified command. The q/ and q? commands exist to provide the same functionality for the search history.

2
  • Was looking for a way to copy ex commands to create keybindings, and q: was the trick I needed.
    – Justin C
    Commented Mar 9, 2021 at 0:57
  • kenorb's answer is even more thorough. Glad y'all found this answer useful, though! Commented Apr 2 at 18:34
127

Vim in Ex mode is useful when:

  • You're in need of editing (multiple) files non-interactively (as part of the script).
  • Your connection is very slow or screen is not updated after your actions.
  • Mappings and abbreviations are disabled.
  • Common keys such as Escape or Control doesn't work properly.

Basically vi is the visual mode for ex therefore Vim Ex Mode is just emulation of ex (they still run the same code), so it is possible to get to the command mode of ex from within vi and vice-versa. There are actually two modes: Ex mode (vim -e) and improved Ex mode which allows for more advanced commands than the vi compatible Ex-mode (vim -E). See: What is the difference between Ex mode and improved Ex mode?

Ex is the root of a family of editors: edit, ex and vi. Ex is a super‐ set of ed, with the most notable extension being a display editing facility.ex(1)

Editing files non-interactively is the most common usage and people using it in similar way as sed and awk, however they're are more stream oriented - they only read the file forward from beginning to end (they're not designed to work with multiple lines) while vim is buffer oriented - you can move forward and backward in the file as you like which makes it so powerful.

Basically:

sed is a Stream EDitor, not a file editor.

Nevertheless, people seems to abuse it for trying to edit files and the truth is that it doesn't edit files. Secondly its options such as in-place (-i) are non-standard FreeBSD extensions and may not be available on other operating systems. So if you want to avoid unportable code, I/O overhead and bad side effects (such as destroying symlinks) you should use ex which is the standard UNIX command-based editor (along with ed)BashFAQ.


Other things which I find useful in Ex mode is to use it as a playground (similar to python console) where you can execute many commands in a row, working/debugging regular expressions, checking vim configuration, executing external commands or working with registers, etc.

For example:

let @d = '<td></td>'
let @r = '<tr>' . repeat(@d, 5) . '</tr>'
echo @r
let @t = '<table>' . repeat(@r, 5) . '</table>'
reg

which is more easier in Ex mode than in normal mode (where you can see only your last command).


Practical usages

I've the following aliases in my .bash_aliases:

alias trim="ex +'bufdo!%s/\s\+$//e' -scxa"
alias retab="ex +'set ts=2' +'bufdo retab' -scxa"

Note: Using bufdo is not ex POSIX-compliant method (as per manual), so then you can consider using it with find instead. The ! is used to force switching the buffers without saving (otherwise warning is generated).

The first one I'm using to trim the trailing spaces in all my source files, e.g.:

trim **/*.php

The second one converts all tabs into spaces (recursively), e.g.:

retab **/*.php

For me using retab is enough, but there are some downsides described in here. Add extra -V for increased verbosity output.

Note that above examples using zsh/bash4 globbing (**), so make sure your shell supports it and it's enabled.

For more practical examples (like parsing html files), check:

Also learn further about Ex-mode at:

7
  • 1
    Can you please post an example of "editing (multiple) files non-interactively" operation that is simpler to achieve with ex than it is with ed, sed, or awk? Commented Oct 17, 2015 at 6:20
  • 2
    @SatoKatsura Added two practical usages which deals with multiple files.
    – kenorb
    Commented Oct 17, 2015 at 11:00
  • I've been digging through the documentation to get a better grasp on ex mode. I don't get why you have a bang (!) in your trim alias bufdo command. Won't that just abandon all changes in all files except the last one? If not, why not?
    – Wildcard
    Commented Jan 7, 2016 at 9:11
  • @Wildcard Without ! basically won't work, as each time when buffer is switched, ex would complains about non-saved changes (and we're saving changes at the very end with xa). The warning can be disabled, but it's much easier to add !.
    – kenorb
    Commented Jan 7, 2016 at 9:54
  • 1
    What is -scxa for in your examples?
    – Jacktose
    Commented Nov 10, 2022 at 7:48
33

I rarely use ex-mode, but when I need it I'm grateful for its existance.

I sometimes access systems via ssh over VPN, and these connections can sometimes get slow. Making the problem worse, I sometimes need to edit a file on the remote side which is, in addition to being behind ssh and VPN, is over a slow serial connection (so, 9600 baud plus a lot of network latency).

It is times like this that having visual feedback and screen updates becomes more of a hindrance because what I see is delayed (the effect is kind of like talking into a mic but with speakers far away, like in a sports stadium. One's actions become choppy and sometimes confused due to the delayed feedback).

In this case, having the changes not echoed back is a useful advantage, since I can get more done in less time when I'm not waiting for the screen to update.

When I'm done making the edits, I go back to visual mode for a one-time screen update to review my work. Then I can go back to ex-mode or save because I'm done.

1
  • 5
    I would like to think those slow situations are rare, but I've encountered them myself enough times at my job that I am grateful that vi(m) supports the "least common denominator" like this :) Commented Oct 27, 2019 at 14:31
19

The command-line window is useful for writing out long complicated commands. Since the command history opens as a window, you can use any vim navigation or editing command/mapping.

Say you want to edit a long substitution command that you ran once, but had made a mistake:

:%165,177s:here is a whole bunch of text I wnat to replace:here is the replacement:c

In order to change the search string, you cannot use vim motions like b in the Ex line to navigate (although I believe that there are key mappings for this navigation). Instead, most people would hit the left-arrow key a bunch of times.

A better way: hit q: to go into the command-line window, k to move up to the last command, and navigate with normal motions to the part of the command you want to change. Want to change an entire word, no problem: ciw.

7
  • OK, thanks. Are you sure q: is the Ex command-line? The help only uses the phrase "Ex" for he mode brought up by Q, but that doesn't support the standard vim editing you are referring to - only q: does, which the help simply calls 'Command line mode'. I know I confused the two at first, but we should probably make sure we are clear about the distinction here. Commented Feb 5, 2015 at 20:40
  • 1
    I searched :help q: before I decided to use that phrase, but I might be confused about the terminology.
    – bsmith89
    Commented Feb 5, 2015 at 20:42
  • 1
    I've been confused about this terminology too. I usually talk about typing an Ex command as anything following :, but I could be using that wrong.
    – bsmith89
    Commented Feb 5, 2015 at 20:45
  • 3
    The content of your answer is all correct, but the way you're using the terminology is (to me) a bit confusing. I think you're interpreting the documentation slightly wrongly. q: doesn't open an Ex command-line. It opens the command history window, in which the current and previous Ex commands can be edited. The Ex command line is what you get when you press : in Normal mode.
    – Rich
    Commented Feb 8, 2015 at 9:05
  • 3
    FWIW I think the Vim documentation is a bit confused too, as in places it refers to all : commands as being 'Ex' commands. IMHO really only the commands that are actually available in Ex should be considered to be Ex commands.
    – Rich
    Commented Feb 8, 2015 at 9:08
17

I was told by a person with partial sight who is going blind that he is switching to ex, so that's one use of it. I myself am considerably older than vi, and I switched from ed to ex a long time ago (yes, I know, "ed is the standard editor"). The only thing I do in vi mode is %-bouncing to match parens when writing Lisp code.

2
  • 1
    Reminds me of this interview. I don't remember if ex specifically was mentioned though. Commented Jul 1, 2017 at 20:32
  • 2
    Excellent insight, I would never have thought of this. Thanks very much, great answer. Commented Jul 3, 2017 at 13:29
7

Apart from the usages already mentioned here, ex is also a way to edit files on the linux console if that happens to be on the S/390 platform and the console happens to be line-oriented instead of screen-oriented.

3

Does Ex mode have any practical use?

Probably not. However, while entering Ex mode isn't really useful, learning Ex commands still is useful, particularly because you use ex editing commands with :g/ global commands to operate on matching lines.

3

As a vim user, I appreciate the speed of using normal mode keys to move around the text. I prefer using h and l to left arrow and right arrow so my hands do not need to move to reach those keys. When I use other computer keyboards, arrow keys move, but normal mode keys still at their expected place.

When I type :%s/oldtext/newtext/gc to make a search and replace operation, I no longer can use normal mode keys to move around this line.

I prefer typing q: and going to a command editor in normal mode to edit my command the way I edit the rest of the file.

By the way, typing q: lets me view the history of commands.

I have inoremap q; q:i in my .vimrc to use q; instead of q:

-1

The original "ex" was the upgraded version of the "ed" text editor in Unix. In 2BSD a visual mode was added, and the new editor was called "vi".

If one wants to use the ex command line, he will switch to command mode with the ":" key. So in my best understanding ex is actually not there for nostalgy, it is the part to provide command mode.

You can also enter its insert mode by

 :i 

then hit "enter and type your text. Exit with ^c. Or type it all on one command line, like

 :i my multiline text \with a second line.^c
2
  • 1
    Given that Ex-mode is not quite the same thing as Command-line-mode, this doesn't seem to answer the question. And as an additional side note: command-mode is an unfortunate synonym for Normal mode, not command-line mode.
    – 8bittree
    Commented Oct 28, 2017 at 17:34
  • "Command mode" point taken. Only excuse, when we changed to vi the new addition we were intoruduced as "visual" (not the present vim visual mode) as opposed to "command" - now ex. On the other hand command-line seems to invoke the same do_cmdline() as ex. Not that it suggests any "practical use" I admit.
    – if2
    Commented Oct 30, 2017 at 16:08

Not the answer you're looking for? Browse other questions tagged or ask your own question.