[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Viewer for html files



On Tue, Feb 11, 2020 at 12:26:38PM -0500, Thomas George wrote:
> 
> file:///home/user/directory/long file name.html
> 
> It would be nice to have an html viewer which opens a file in the current
> directory with auto-completion of the initial word of the filename.
> 
> Is there such a thing?

Not that I'm aware of, but you could write a shell function to do it
for you.  Exact details will depend on which browser you intend to
launch.

Let's say you want to call your function "hv", and you're in the directory
where the HTML file is.  You can type "hv ", and then the first part of
the filename, and then press Tab to have your shell (bash, I'm assuming)
tab-complete the filename for you.  You'll see something like this:

prompt:$ hv long\ file\ name.html

This will pass "long file name.html" as a single argument to the "hv"
command.  Now all we have to do is write that command.

Let's also assume that your browser wants you to convert the spaces to
%20 strings.  Some browsers may not require it, but bash can do it
easily enough, so we might as well add that.

hv() {
    mybrowser "file://$PWD/${1// /%20}"
}

Of course, you'll need to replace "mybrowser" with whatever command
and options are required to launch a browser, or to send a URL to an
already-running instance of a browser.

This particular function may be perfectly suited to your needs, but it
doesn't gracefully handle pathnames with slashes in them, e.g. if you
tried to do hv ../foobar.html.  To handle that, you'd need to
canonicalize the pathname with $PWD/ prefixed to it.  There are
shell commands that can do it, but I'd prefer not to pursue that
goal unless absolutely necessary.

There are also other URL-encoding steps that may be required if you
use special characters other than spaces in your filenames.  Again,
I'm choosing to let that goal go unpursued unless absolutely necessary.


Reply to: