4 ms·
(pedantic note: When you run shell commands in vim, they are running in an actual shell.) vim can be thought of as a visual and interactive helper for your tex
by oftenwrong 10y ago
(pedantic note: When you run shell commands in vim, they are running in an actual shell.)
vim can be thought of as a visual and interactive helper for your text-processing utilities. The "intended scope" of vim is text editing, and while you're editing you may want to run commands on the text in your buffer, or just parts of it, and have the results apply immediately without writing and reloading the file, or exiting.
For example, in vim, you may be writing a list somewhere in your document, and you realize you want it sorted alphabetically. You can visually select the range of lines ('<,'>) that comprise the list and then sort them by calling the sort utility [1]:
:'<,'>!sort
And then you can continue adding to the list.
What would your workflow be for the above? Some like this...?
1. write the file you are working on in your EDITOR
2. make a mental note of the absolute line numbers for the list in the document
3. open new terminal
4. call an awk command that uses `sort`, or some other script, passing in your line numbers, and the file name
5. switch back to EDITOR and reload file
----
[1] Of course vim has its own `sort` command, so you can leave out the '!' in that example to do the same.
vi did not have its own `sort`, so people used the external `sort`. vi also didn't have the visual selection, so people could pick lines using ranges:
:.,+10!sort
i.e. sort from the current line to the line 10 lines down, inclusive
- u801e 10y agoIt's interesting to read up on the relationship between ed, sed, ex, and vi. In fact, all 4 of those editors have similar ways of addressing line ranges (1 for the first line, $ for the last line, etc). One interesting feature many people may not know about is that it's possible to address lines using a regular expression. For instance, if you had the following text in a vim buffer: one two three four five and you wanted to delete the lines between "two" and "four" inclusive, you could run the following command in vim: :/two/,/four/d You could also do the same thing in sed sed '/two/,/four/d' filename