ed

package module
v0.0.0-...-e88810d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 6, 2023 License: MIT Imports: 22 Imported by: 0

README

ed (the awesome UNIX line editor)

ed is a clone of the UNIX command-line tool by the same name ed a line editor that was nortorious for being and most unfriendly editor ever created.

This is a modern take on that editor writtein in the Go language for portability with all the basic ed commands, a modern readline line editor with vi bindings and friendly error messages when things go wrong.

Quick Start

Install ed with a valid Go environment using go get:

$ go install git.mills.io/prologic/ed@latest

Or install from published binaries for your platform from the Releases page.

And edit files to your heart's cntent :)

$ ed
> a
Hello World!
.
> ,p
Hello World!
> q
WARN[0025] buffer has modifications not written
> Q

For help on how to use ed in general please refer to this excellent guide:

Or the GNU ed manual

NOTE: This port/clone does not implement all ed commands, only a subset.

Example

Here is an example ed session:

$ ed
> a
Start typing a few lines.
Anything you want, really.
Just go right ahead.
When you're done, just type a period by itself.
.
> p
When you're done, just type a period by itself.
> n
4    When you're done, just type a period by itself.
> .n
4    When you're done, just type a period by itself.
> 3
Just go right ahead.
> .n
3    Just go right ahead.
> a
Entering another line.
.
> n
4    Entering another line.
> i
A line before the current line.
.
> n
4    A line before the current line.
> c
I decided I like this line better.
.
> n
4    I decided I like this line better.
>

Commands

This implementation supports the following commands: (not all commands from the original ed are supported nor the GNU ed)

Command Description Notes
`` newline Null command. An address alone prints the addressed line. A alone is equivalent to '+1p'. The current address is set to the address of the printed line.
!shell exec shell Executes a shell command with sh(1).
= print index Prints the line number of the addressed line. The current address is unchanged.
a append text Appends text to the buffer after the addressed line. Text is entered in input mode. The current address is set to the address of the last line entered or, if there were none, to the addressed line.
c change lines Changes lines in the buffer. The addressed lines are deleted from the buffer, and text is inserted in their place. Text is entered in input mode. The current address is set to the address of the last line entered or, if there were none, to the new address of the line after the last line deleted; if the lines deleted were originally at the end of the buffer, the current address is set to the address of the new last line; if no lines remain in the buffer, the current address is set to zero.
d delete lines Deletes the addressed lines from the buffer. The current address is set to the new address of the line after the last line deleted; if the lines deleted were originally at the end of the buffer, the current address is set to the address of the new last line; if no lines remain in the buffer, the current address is set to zero.
e file edit file Edits file, and sets the default filename. If file is not specified, then the default filename is used. Any lines in the buffer are deleted before the new file is read. The current address is set to the address of the last line in the buffer.
E file force edit Edits file unconditionally. This is similar to the 'e' command, except that unwritten changes are discarded without warning.
f file set filename Sets the default filename to file. If file is not specified, then the default unescaped filename is printed.
`g /re/command global Global command. The global command executes command for every matching line matching the regular expression re.
i insert text Inserts text in the buffer before the addressed line. The address '0' (zero) is valid for this command; it places the entered text at the beginning of the buffer. Text is entered in input mode. The current address is set to the address of the last line entered or, if there were none, to the addressed line.
j . join lines . Joins the addressed lines, replacing them by a single line containing their joined text. If only one address is given, this command does nothing. If lines are joined, the current address is set to the address of the joined line. Else, the current address is unchanged.
m n move lines Moves lines in the buffer. The addressed lines are moved to after the right-hand destination address. The destination address '0' (zero) is valid for this command; it moves the addressed lines to the beginning of the buffer. It is an error if the destination address falls within the range of moved lines. The current address is set to the new address of the last line moved.
n numbered Number command. Prints the addressed lines, preceding each line by its line number and a . The current address is set to the address of the last line printed.
p print lines Prints the addressed lines. The current address is set to the address of the last line printed.
q quit Quits ed. A warning is printed if any changes have been made in the buffer since the last 'w' command that wrote the entire buffer to a file.
Q force quit Quits ed unconditionally. This is similar to the q command, except that unwritten changes are discarded without warning.
r file read file Reads file and appends it after the addressed line. If file is not specified, then the default filename is used. If there is no default filename prior to the command, then the default filename is set to file. Otherwise, the default filename is unchanged. The address '0' (zero) is valid for this command; it reads the file at the beginning of the buffer. The current address is set to the address of the last line read or, if there were none, to the addressed line. If file is prefixed with a bang (!), then it is interpreted as a shell command whose output is to be read, (see shell escape command '!' below). In this case the default filename is unchanged.
R replace line Puts the editor in replace mode putting the currently addressed line in the input buffer permitting modifications to the line and replacing it.
s /re/replacement search and replace Search and replace command. Replaces all occurances of re with replacement on the first matched line in the address range.
t n transfer text Copies (i.e., transfers) the addressed lines to after the right-hand destination address. If the destination address is '0' (zero), the lines are copied at the beginning of the buffer. The current address is set to the address of the last line copied.
v /re/command inverse global This is similar to the 'g' command except that it applies command-list to each of the addressed lines not matching the regular expression re.
w file write file Writes the addressed lines to file. Any previous contents of file is lost without warning. If there is no default filename, then the default filename is set to file, otherwise it is unchanged. If no filename is specified, then the default filename is used. The current address is unchanged.
wq file write & quit Writes the addressed lines to file, and then executes a 'q' command.
x put text Copies (puts) the contents of the cut buffer to after the addressed line. The current address is set to the address of the last line copied.
y yank text Copies (yanks) the addressed lines to the cut buffer. The cut buffer is overwritten by subsequent 'c', 'd', 'j', 's', or 'y' commands. The current address is unchanged.
z n scroll Scroll n lines of text from the addressed line.

Why?

If you've gotten this far and wondering "Why?":

  • For fun primarily :)
  • As a learning exercise
    • I wanted to learn how ed works

And just to prove this version of ed is actually a useful editor; the following commits were made with this ed:

Differences between GNU Ed

Some of the of the main differences between this implemtnation (written in Go) and GNU Ed or even the traditiaional UNIX Ed are is the way I've handled parsing. In tranditional UNIX / GNU Ed you can do things like:

  • Better error handling by default
  • Syntax highlighting
  • Not all ed commands are implemented
  • Being written in Go the binary is a bit fatter than the ones written in C :)
  • Probably other minor differences and quirks...

License

ed is licensed under the terms of the MIT License

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// Version release version
	Version = "0.0.1"

	// Commit will be overwritten automatically by the build system
	Commit = "HEAD"
)

Functions

func FileExists

func FileExists(name string) bool

func FullVersion

func FullVersion() string

FullVersion returns the full version and commit hash

func Main

func Main(stdout io.Writer, args []string) error

Types

type Address

type Address interface {
	fmt.Stringer

	IsUnspecified() bool
	Resolve(buf Buffer) error
	Size() int
	Start() int
	End() int
}

Address ...

type Buffer

type Buffer interface {
	io.Reader
	io.Writer
	io.ReaderFrom
	io.WriterTo

	Index() int
	Size() int
	Hash() uint64
	Clear()

	Append(line string)
	Insert(line string)

	Current() string
	SearchBackward(re string) int
	SearchForward(re string) int

	Delete(addr Address)
	ForAll(f BufferFunc) error
	ForEach(addr Address, f BufferFunc) error
	Goto(addr Address) error
	GotoN(n int) error
	Replace(n int, re, replacement string) error
	Select(addr Address) []string
	SelectN(n int) []string
}

Buffer ...

type BufferFunc

type BufferFunc func(i int, line string) error

type Command

type Command interface {
	fmt.Stringer

	Validate(buffer Buffer) error

	Addr() Address
	Arg(i int) string
	Args() []string
	RawArgs() string
	Cmd() string
}

Command ...

type Editor

type Editor interface {
	io.Writer
	io.ReaderFrom

	Stop()
	Run() error
	Eval(line string) error

	Settings() Settings
	Regexp() *regexp.Regexp
	SetRegexp(re *regexp.Regexp)
	BufferModified() bool
	SetLastHash(hash uint64)
	Clipboard() []string
	SetClipboard(lines []string)
	Filename() string
	SetFilename(filename string)
	SetMode(mode int)
	SetPrompt(prompt string)

	Handle(cmd string, handler Handler)
}

Editor ...

func NewEditor

func NewEditor() (Editor, error)

type Handler

type Handler func(e Editor, buf Buffer, cmd Command) error

Handler ...

type Input

type Input interface {
	Readline() (string, error)
	ReadlineWithDefault(what string) (string, error)
	SetPrompt(prompt string)
	HistoryDisable()
	HistoryEnable()
	Close() error
}

type Option

type Option struct {
	// contains filtered or unexported fields
}

func NewFlagSet

func NewFlagSet() (*flag.FlagSet, *Option)

type Settings

type Settings interface {
	Save() error
	Load() error
	Get(key string) string
	GetBool(key string) bool
	GetInt(key string) int
	Set(keu, value string)
}

Directories

Path Synopsis
cmd
ed

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL