Settings

Theme

Actually Using Ed (2012)

blog.sanctum.geek.nz

9 points by ditsuke 4 years ago · 7 comments

Reader

pcthrowaway 4 years ago

Ed is the standard text editor - https://www.gnu.org/fun/jokes/ed-msg.en.html

> Let's look at a typical novice's session with the mighty ed:

    golem$ ed

    ?
    help
    ?
    ?
    ?
    quit
    ?
    exit
    ?
    bye
    ?
    hello?
    ?
    eat flaming death
    ?
    ^C
    ?
    ^C
    ?
    ^D
    ?
  • u801e 4 years ago

    If they're lucky, they'll press Q. Otherwise, ^Z will work :)

seanhunter 4 years ago

There are actually practical uses for ed in shell scripting. For example recently I had to recover my entire website from the internet archive because covid reasons it's a long story.

So I went to the internet archive, downloaded almost all of it (all of the stuff that was there), put it through lynx, to text and from there created markdown.

So at some point I had 100-ish files and I wanted to put some yaml at the top of every markdown file. While playing around with how I wanted to do this at some point I needed to make some changes. I was faced with needing to do certain simple edits in the yaml front-matter of every one of a very large number of files.

Now you could just make a vim/emacs macro and run it on every file and that would be fine, or you could use ed in a shellscript. Something like this:

    find . -type f -name "*.md" | while read f ; do
        ed "${f}" <<__DONE
        1,^--g/^foo/d
        wq
    __DONE

    done
So what have we achieved? We deleted lines which start with "foo" but only from the top until the first line that starts with "--". (ie in the yaml). That range address wouldn't work in sed, or not as easily. We can also do things like insert a line just after something or whatever. These are tricks that are worth learning, and everyone who does unix shellscripting should have "ed with a heredoc" in their bag of tricks.
  • u801e 4 years ago

    I think the second line should start with

        1,/^--/ g...
    
    Unless forward slashes aren't required to indicate a regex based address.

    One thing I do to create my ed scripts is to actually read the output of ls with some file pattern and then use a vim macro to edit the output as follows:

        ed <<EOF
        e file1
        commands
        w
        e file2
        commands
        w
        ...
        wq
        EOF
    
    The gnu info page for ed is a very good reference to determine what commands are available and how addressing works.
jonjacky 4 years ago

(2012)

Keyboard Shortcuts

j
Next item
k
Previous item
o / Enter
Open selected item
?
Show this help
Esc
Close modal / clear selection