dreftymac
dreftymac

Reputation: 32370

vim - process the output from the "read" command as a range in Ex mode

BACKGROUND:

In vim (Ex mode) it is possible to run an external command and have the output from that command automatically inserted into the current buffer.

In Example 001, we see how to cd to the current directory, get a listing of the files there and auto insert that into the current buffer

EXAMPLE 001:

    :cd . | :r ! dir /w/s/b

QUESTIONS:

1) Is it possible to automatically specify or capture the Vim {range} to reflect the lines that were recently inserted into the file ?

2) Using the range obtained in question 1) is it possible to chain Ex mode commands to automatically process the lines that were inserted into the file ?

3) If it is not possible to do 1) or 2) above, is there an alternate way for Vim to recognize lines recently inserted into the buffer and run arbitrary commands on them ?

4) What is a relevant :help cross reference that can be used for this purpose ?

GOAL:

The goal is to be able to chain multiple Ex mode commands together to easily run process recently added lines to a file, without having to expressly identify the line number or manually select them using Visual mode or something similar.

The goal is do something similar to the (psuedo-code) in Example 002

Example 002:

    :cd . | :r ! dir /w/s/b | :{auto-range}s/^/    /

Upvotes: 2

Views: 381

Answers (2)

Ingo Karkat
Ingo Karkat

Reputation: 172590

Vim sets the change marks '[ and '] to the inserted range; you can use these to define a range for subsequent Ex commands:

:cd . | execute 'r ! dir /w/s/b' | '[,']s/^/    /

You need :execute because otherwise the | is interpreted to belong to the :r command.

Upvotes: 2

romainl
romainl

Reputation: 196576

What about processing those lines before inserting them in Vim?

:r!dir /w/s/b | sed -e "s/^/    /"

Upvotes: 1

Related Questions