ex is a line editor which serves as the foundation of vi. ex commands work on the current line or on a range of lines in a file. ex is invoked using the colon syntax within vi, and can be invoked on its own from the shell.
Questions tagged [ex]
75 questions
39
votes
2 answers
Vim command for "edit a copy of this file"
A pattern I seem to be following right now is:
edit a file until I like it
:w another-file to use it as a starting point for another-file
:e another-file to polish up another-file
Is there an existing ex command to do the latter two steps at once?…

rampion
- 87,131
- 49
- 199
- 315
11
votes
1 answer
How to execute multiple global commands in Vim sequentially? (Error: "Cannot do :global recursive")
I want to sort the paragraphs in my text according to their initials with the following global command:
g/_p/,/^$/mo$
This works alone. But when I use several global commands at once, then an error occurs:
g/_p/,/^$/mo$|g/_w/,/^$/mo$
This gives…

Mert Nuhoglu
- 9,695
- 16
- 79
- 117
6
votes
9 answers
In Vim, how to remove all lines that are duplicate somewhere
I have a file that contains lines as follows:
one one
one one
two two two
one one
three three
one one
three three
four
I want to remove all occurrences of the duplicate lines from the file and leave only the non-duplicate lines. So, in the example…

Tem Pora
- 2,043
- 2
- 24
- 30
5
votes
2 answers
vim: Run multiple commands based off of one :global command
Apologies if this has been posted already, for I cannot find an answer, even on the vim wiki.
Is there a way I can run multiple commands in vim command-line mode off of a single :g search?
For example,
:%g/foo/ s/bar/\=@a/g | exe "norm…

Miles
- 780
- 7
- 19
5
votes
2 answers
How to copy command string in the ex mode into clipboard?
How to copy command string in the ex mode into clipboard?

showkey
- 482
- 42
- 140
- 295
5
votes
3 answers
Redirect ex command to STDOUT in vim
I'm trying to craft a command to dump vim's highlighting information to STDOUT. I can write successfully to a file like this:
vim +'redir >outfile' +'hi' +'redir END' +'q'
After this, outfile contains exactly what I want. But I'd rather output to …

Kenan Banks
- 207,056
- 34
- 155
- 173
4
votes
1 answer
Differences in running vim via command line vs. running it in the vim editor
I am trying to process a series of files. I have noticed that there are discrepancies in running a particular command from the command line (i.e. ex mode). E.g.
$cat poo.txt
big
red
dog
small
black
cat
$vim -c "2,$g/^dog/d|wq" poo.txt
$cat…

irritable_phd_syndrome
- 4,631
- 3
- 32
- 60
4
votes
2 answers
Append to line in vim matching regex
I have a json file that I ran a find and replace in using vim, however I forgot a , at the end of the line.
...
"id":41483
"someName":"someValue",
...
Using vim, how can I append a , to every line matching \"id\"\:[0-9].*$?

Matt Clark
- 27,671
- 19
- 68
- 123
4
votes
1 answer
Vim: Call an ex command (set) from function?
Drawing a blank on this, and google was not helpful.
Want to make a function like this:
function JakPaste()
let tmp = :set paste?
if tmp == "paste"
set nopaste
else
set paste
…

sixtyfootersdude
- 25,859
- 43
- 145
- 213
3
votes
4 answers
How can I select part of a line in Vim
I have a line of text that looks like this
foo bar http://www.example.com -> baz
I want yank the url part using ex. Anyone have any ideas on how to do this?
To clarify. I want to do something like this :y/http:.*\.com/ from the command line. But…

Matt Peck
- 43
- 4
3
votes
3 answers
shell script replace variables in file - error with Sed's -i option for in-place updating
Here is my test.env
RABBITMQ_HOST=127.0.0.1
RABBITMQ_PASS=1234
And I want to use test.sh to replace the value in test.env to :
RABBITMQ_HOST=rabbitmq1
RABBITMQ_PASS=12345
here is my test.sh
#!/bin/bash
echo "hello…

user2492364
- 6,543
- 22
- 77
- 147
3
votes
1 answer
Vim ex knowing number typed before
I'm making a shortcut that places a # at the front of each line, in the next x lines. x is a number I type before entering the shortcut, like typing 11dd deletes the next eleven lines.
The command is .,+10 s/^/#/g. Here the number ten should really…

john-jones
- 7,490
- 18
- 53
- 86
3
votes
3 answers
vi (vim) to read an exrc file other than ~/.exrc
We would like to be able to start vi (or vim) up with an alternative ex initialisation file. By default this is ~/.exrc.
Unfortunately vim(1), which is also brought up for "man ex" on this system, doesn't mention .exrc.
Any workarounds to provide…

projix
- 211
- 1
- 4
2
votes
1 answer
How to read a excel file with merged cells in php?
I wrote a php script that allows me to read an uploaded excel file and insert all the images contained in a folder by renaming them with the cell values "style" and "color" to have style_color.jpg. The script works fine but if I upload an xlsx file…

Patrick62
- 71
- 8
2
votes
2 answers
Select relative range, ignoring invalid line numbers
I was wondering if there was a syntax for specifying a range of relative lines in vim/ex that does not give 'invalid range' and instead gets as many lines as it can.

0x777C
- 993
- 7
- 21