开发者

From vi editor, open a file of arbitrary type with an arbitrary program

开发者 https://www.devze.com 2023-03-24 05:34 出处:网络
The vi editor has a very useful command gf, which allows one to open — in a new vi window — the file whose path is situated beneath the cursor in the vi editor. I am attempting to generalize this fe

The vi editor has a very useful command gf, which allows one to open — in a new vi window — the file whose path is situated beneath the cursor in the vi editor. I am attempting to generalize this feature such that a file of any type can be open开发者_如何学Pythoned from within the vi editor by filtering the file name through a shell command such as open.

However, based on trial and error and perusal of the man page for the *nix open command, I believe it is not possible to open a file whose name/path is transmitted via pipe/redirection. As an example, the following very simple command fails because open cannot accept piped input as an argument.

echo file_name.txt | open

Perhaps there are other functions besides open that can be used in this situation?

Here is a selection of vi commands I have tried to use to open a file whose path was selected within vi using the vi visual mode:

:'<,'> ! open

:'<,'> ! echo | open

:'<,'> ! cat | open

Note: the :'<,'> jargon simply refers to the text that has been highlighted via vi visual mode. The ! signifies that the following text should be executed as a shell command using the default shell. Thus, these lines attempt to filter highlighted text through various shell commands, to no avail.

The three above examples attempting to filter the file name through the open command fail to work. If anyone has any suggestions on how to accomplish the task I have described, please share. Solutions to either the general case (opening a file whose path is received via a pipe/redirect) or the particular case (opening a file whose path is contained in a text file by using the vi command line) would be greatly appreciated.

As a bonus, I would like to be able to open the file from within vi by simply positioning the cursor over the file name (as is the case for the gf command), rather than by highlighting the path using the vi visual mode.


You want xargs(1).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号