Showing posts with label terminal. Show all posts
Showing posts with label terminal. Show all posts

Thursday, August 25, 2016

Sh-Utils

I wrote a set of simple shell commands a little while back.  I actually ended up using them pretty regularly (quick and dirty source control, manual merging of configuration files, editing large compressed and delimited files in place, etc...).  Since they save some non-trivial keystrokes, I cleaned up the code and pushed it to github.  Likewise, I built a python package and published it via PyPI.

The package is called sh-utils.  The commands are...

pm - move p to p'
cpm - copy p to p'
upm - undo p to p'
sw - swap two paths
pt - pivot file over a command
pts - pivot file over a command (stdin)

There are descriptions on the github and PyPI pages, but I'll try to go into a bit more detail.

The three pm commands are mostly meant to work with path (file, directory, pipe, socket, etc...) backups.  For example, it's fairly reasonable to move or copy a file to a .bak (.old, .orig, etc...).  Vice versa, it's fairly reasonable to want to undo it.  So, pm takes a list of paths and moves them safely to a backup by prefixing (-p) or suffixing (-s) a value to the path name.  Likewise, cpm does the same, but copies the path instead of moving it.  upm safely undoes both of them.

For example, making a backup before editing a file...

~ cpm foo

To recover it...

~ upm foo\'

They're additive and subtractive as well, so it's easy to keep a stack of path(s) backups.  Unfortunately, that can make things a bit messy and possibly painful.  So, instead of having to run upm a bunch of times to unwind an stack of backups, upm has a flag (-a) that conflates the latest and earliest paths in a single command.

The next command is sw, which swaps two paths.  Normally swapping two paths takes a few commands and a pivot file (move a path to a pivot, move second to first, move the pivot to the second).  Instead, sw does everything in one command and as close to atomically as possible.

For example, swapping a file with its backup...

~ sw foo foo\'

or...

~ sw foo* 

Last are two pivot commands.  Similar to sw, doing something in place on a file normally needs more than one step and a pivot file.  Instead, pt and pts do everything in one command and as close to atomically as possible.

Normally I would sort a file...

~ sort foo > foo\'
~ mv foo\' foo

With pt...

~ pt sort foo

Finally, pts is similar to pt except it passes the file on stdin, which can be useful for pipelines.

For example, if you wanted to uncompress a delimited file, sort it, parse it, square a column, and re-compress it, normally it would be...

~ xz -dc foo.xz | sort | awk '{ print \$1, \$2 * \$2 }' | xz > foo.xz\'
~ mv foo.xz\' foo.xz

Instead, with pts...

~ pts sh -c "xz -d | sort | awk '{ print \$1, \$2 * \$2 }' | xz" foo.xz

If anyone ends up using these and you have any questions, feel free to ask.

Tuesday, June 3, 2014

Hypertext Browsing

I tend to browse the web using Firefox with some basic plugins, which probably is true for most people (Chrome, Safari, etc...).  It's overkill for a lot of things though.  A lot of the time, all I really need is a way to enter a URL, render some text, search for some text, follow links, and possibly enter text into forms.  All of these can be done with a slightly simpler browser, w3m.

If you are resource constrained, you prefer working strictly with text, or you don't have access to a graphics environment, w3m can come in handy.  The footprint is a few orders of magnitude less than the graphical variants, and it's meant for keyboard navigation.

The motions are probably familiar to anyone who's used Vi/Vim before.

<h>- left
<j>- down
<k>- up
<l>- right
<w>- next word
<^>- line start
<$>- line end

Directional keys can also be used.  Navigating links and input fields is generally the same as the graphical browsers.

<tab>- next field
<s-tab>- previous field

Instead of the mouse, w3m gives a menu for links, which can be slightly faster than using a mouse (see Fitts' Law).

<esc-m>- link list (move)
<esc-l>- link list (follow)

Another easy way to navigate is searching for text.  Again, Vi/Vim users will probably recognize the default key bindings.

</>- search forward
<?>- search backward

Last, to open new URLs and tabs...

<U>- open URL
<T>- new tab

There are more complex combinations, but the above should be a good start.  The full set of navigation and configuration options can be found in the man page as well as the help screen (<H>).

Putting it all together...


One of the things I've found w3m is good for is as a basic interface for things that have a form of HTTP/HTML API.


It's admittedly limited, depending on the protocol or features sites you try to browse rely on.  Support for more recent protocols (SPDY, HTML5, AJAX, etc...) is decidedly lacking.

Although, on the development side, it's also useful for a number of things like navigating doxygen/javadoc, parsing jhat output, and even browsing wikipedia.


It's also useful to work with from the command line.

~ watch -n 10 w3m http://www.wrh.noaa.gov/sew -dump

I combine it with other things like tmux, mutt, vim and emacs which makes it a little more useful, but the above are some of the basics.

Downloads, documentation, etc... can be found on w3m's homepage for anyone curious.