Hot answers tagged

81

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 ...


61

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 ...


31

To edit file non-interactively using ex (vi is the visual mode for ex), you can use +{command} or -c {command} parameters which allows you to execute the vi commands after the first file has been read. The ex is a standard command-line editor (similar to ed). There is also vipe (a Vim command pipe editor) should be used which is part of moreutils package ...


18

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 ...


16

:insert will keep asking for lines until you type a line only containing .. For more help see :h :i.


14

Use the :global command for that: :3,$g/^#/d You can apply it to lines not matching a pattern: :3,$g!/^#/d You can use the full range mechanism with it (see :help :range): :.,/#define/+3g/^#/d And you can use it with any command: :3,$g/^#/s/foo/bar/g It's one of the most powerful commands in Vim, please see :help :global for details.


13

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 ...


13

The "open mode" of vi was useful for terminals that had a single line, such as hardcopy terminals. In open mode, vi had a "single line view" of the file. Moving the cursor around would redraw the entire line, and deleted characters printed differently. The "simulation" that vim does is simply supporting the command, making it act (as the documentation says)...


12

There is. For last line of a file, the address is $: :.,$s/old/new/ See :help {address} for a fill list of possible addresses.


10

Ex commands work on the buffer contents; for register (or variable) contents, you need to use a corresponding Vimscript function (if it exists). For :substitute, the equivalent is substitute() (that was easy, right?) So: :let @t = substitute(@t, '_', 'test', 'g')


9

Sure, just leave the pattern in the substitution empty: :%s//replacement/


9

To print buffer to shell standard output, vim needs to start in Ex mode, otherwise it'll open "normal" way with its own window and clear any output buffers on quit. Here is the simplest working example: $ echo foo | vim -e '+%print' '+q!' /dev/stdin foo or even shorter: $ echo foo | ex +%p -cq! /dev/stdin $ echo foo | ex +"%p|q!" /dev/stdin Note: The ...


9

If you have a sequence of keystrokes that you want to execute in normal mode from the command line, you can use the :normal command. However, by default the :normal command can't be followed by another command because as the help says: This command cannot be followed by another command, since any '|' is considered part of the command. So, if you ...


9

You can use pattern delimiter for this: :/first/,/second/norm dd You can use any search pattern around the ,. If you want to use only the inside of the matched patterns, use + and - like so: :/first/+1,/second/-1 norm dd


8

From An Introduction to Display Editing with Vi: If you are on a hardcopy terminal or a terminal which does not have a cursor which can move off the bottom line, you can still use the command set of vi, but in a different mode. When you give a vi command, the editor will tell you that it is using open mode. This name comes from the open command in ex, ...


8

This command does what you want: :let i = 1|g/^Do/s/^/\=i/|let i = i + 1 Explanation… let i = 1 initializes counter i, g/^Do/s/^/\=i/ prepends i to each line starting with Do, let i = i + 1 increments i. The trick is that the incrementation happens before the next substitution. --- edit --- If we used a single substitution, the counter would only be ...


8

The feasibility of deleting parts of lines is elusive and a better vimmer than I will have to explain that. If anyone cares to donate an explanation I'd be happy to add it to this answer. But it's quite possible to delete groups of rows that match a beginning pattern and an ending pattern. The most obvious way, I think, is to combine the :global and :delete ...


8

I'm not sure if I understand you correctly but is this what you're looking for? :4,11t 15 This will copy lines from 4 to 11 and paste it on line 15. See :h co for more.


8

Everything after ! will be a command, including |. So what you're running is: :redir @+ :w !node | redir END This makes sense, since using | is shell commands is common to pipe output. The easiest workaround is to use multiple lines: :redir @+ :w !node :redir END or you can use :execute to work around this: :redir @+ | :exe 'w !node' | redir END


7

There is a better way of accomplishing the task you showed in ex mode, which is by using the :delete command and a basic cmdline-range. To delete all lines between and including the beginning of the file and the search match, you can do this: 1,/dd/delete If you don't want to include the matched line in the deletion, you can do this 1,/dd/-1delete The ...


7

One way to do it: clear the register: :let @a='' append search results in it: :g/TODO/let @A = getpos('.')[1] . ' ' . getline('.') . "\n" Re: BONUS remove indentation in the global command: :g/TODO/let @A = getpos('.')[1] . ' ' . substitute(getline('.'), '^\s*', '', '') . "\n"


7

This is actually documented in a somewhat 'hidden' non-obvious way, from :help Ex-mode Q Switch to "Ex" mode. This is a bit like typing ":" commands one after another, except: - You don't have to keep pressing ":". - The screen doesn't get updated after each ...


7

Use the /c flag at the end of your substitution in order to confirm each substitution: :%s/foo/bar/gc See :help s_flags. Use & to repeat the last substitution without the flags. See :help &. Use @: to repeat the last ex command.


7

From :help todo: Substitute with hex/unicode number "\%xff" and "\%uabcd". Just like "\%uabcd" in search pattern. In other words, this isn't implemented yet. You can do it interactively, in Vim. See :help 24.8. Type :%s/ /, then type Ctrl-V, then x19, then press Enter.


7

&path has nothing to do with $PATH. Also $PATH is set and used. See :echo $PATH. You should also be able to change its value with :let $PATH = $PATH.':some/path' -- there are a few write only things, $PATH is not one of them IIRC


6

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.


6

If I understand your post correctly, you had this snippet of C code : switch (result) { case CASE_1: return report("..."); case CASE_2: return report("..."); } And you wanted to transform it to : switch (result) { case CASE_1: report("..."); break; case CASE_2: report("..."); break; } I don't know all the details of the ...


6

Type exit at the prompt and press Enter to terminate the current shell session and come back to Vim and your previous session. $ jobs and $ fg don't work because you don't have background jobs in the current shell session. Those commands would work if you used <C-z> instead of :sh, though. :sh suspends the current shell session and starts a new one ...


6

esc in command line mode traditionally meant the same as Enter. That means, it executes your entered command and Vim behaves still the same, when is mapped to another key. Several years ago, I even made a patch, to fix this, but it seems nobody cared too much about it. Back to your question, simply map F10 to ControlC should do what you want, e.g. abort ...


6

There are two ways to achieve this: One is to set ignorecase, then the pattern regex will ignore the case. Yet, this solution is poor if you are writing a script that may need to be reused by someone. A better solution is to use the \c (ignore case) \C (do not ignore case) modifiers in the regex. This command: g/\cpattern/z#.1|echo "=====================...


Only top voted, non community-wiki answers of a minimum length are eligible