i0HHj

ed tricks:

  n  -- print with numbers
  s  -- search and replace by regexp
  a  -- append, enter insert mode,
        the only way to insert at the top
  i  -- insert line,  enter insert mode
  c  -- change line, enter insert mode
  .  -- exit insert mode
  d  -- delete line
  p  -- make line current and print it
  w  -- save
  q  -- quit
  !  -- execute shell command
  u  -- undo
  Q  -- quit without save

  scope by example

    top line     -- 1
    bottom line  -- $
    current line -- default

    3n  -- print line number 3 with line number
    n   -- print current line number with line number
    0a  -- append on top of the file
    5,7 -- print lines from 5 to 7
    1,p -- print from top to current line
    ,$p -- print from current line to the end of file
    ,n  -- print whole file, enumerate lines
           ( 1,$n 'long' way or %n 'modern' (1980+) way)

Annex A. ED line editor -- tutorial
-----------------------------------

  http://linuxclues.blogspot.com.ee/2012/09
  /ed-tutorial-line-editor-unix.html

Ed is a line-oriented text editor. It can be used interactively or in shell scripts. Ed is the original text editor for Unix. It was written by Ken Thompson in 1971. Ed is a small editor with few dependencies that can help you edit some files in situations where no other editor is usable.

RUNNING ED EDITOR

$ ed file_name # opens file_name for editing. Changes are not stored until w command is introduced.

COMMAND STRUCTURE

Ed works by accepting commands to edit parts of the file. Commands usually have this structure:

  [ADDRESS [,ADDRESS]]COMMAND[PARAMETERS]

Commands are one letter long. Address means to which lines command is applied to. Parameters are passed to command.

QUIT ED EDITOR

When we need to finish running the editor we introduce q command:

  q : quit ed editor.
  Q : quit without save.

SHELL COMMANDS

execute a shell command

  !
  !ls

  !date
  Sat Sep 22 01:32:33 CEST 2012

UNDO COMMAND

If you perform a mistake, you can undo the command:

  u : undoes last command

MANIPULATING FILES

Edit a new or another file:

  e foo.txt : edit foo.txt file

Save current changes into a file:

  w : Writes back changes into current file.
  w foo.txt : Writes foo.txt file

Edits output from a shell command:

  e !date : edits answer from date shell command.

MOVING THROUGH THE FILE

  1 : go to first line (first line becomes current editing line)
  $ : go to last line
  . : current line
  3 : move to line number three.
  -2 : moves two lines backwards.
  +4 : moves four lines forward.

  /regex/ : move to next line where that regex matches
  /^foo/ : moves to next line which starts with "foo".

  ?regex? : move to previous line that matches that regex
  ?foo$? : moves to previous line which finishes with "foo".

PRINT LINES

  p : prints current line
  .p : prints current line

  1p : prints first line

  1,$p : prints every line in the file
  ,p : prints every line in the file

  -1p : prints line before current one.

  .,$ : prints line from current one until last one.

If we want to show numbers of lines:

  1,$n : print every line showing its line number.
  ,n : idem

  n : print current line showing its line number.

SUBSTITUTE A REGULAR EXPRESSION

  (.,.)s /RE/REPLACEMENT/
  (.,.)s /RE/REPLACEMENT/g
  (.,.)s /RE/REPLACEMENT/N

Replaces text in the addressed lines matching a regular expression RE with REPLACEMENT.

By default, only the first match in each line is replaced.

If the g (global) suffix is given, then every match is replaced.

The N suffix, where N is a postive number, causes only the Nth match to be replaced.

E.g:

  2s/foo/bar/ : Change in line 2 "foo" into "bar"
  $-1/foo/bar/ : Change "foo" into "bar" in line before last line.
  1,$s/foo/bar : Change first "foo" into "bar" in every line
  1,$s/\//slash : Change "/" into "slash" in every line
  s/foo/bar/g : Change every "foo" into "bar" in current line.

INPUT MODE VS COMMAND MODE

Some commands enter the editor in input mode (e.g: c, i, a), enabling you to introduce data instead of commands.

  . : a single period in a line returns to command mode.

CHANGE COMMAND

  3c : Changes line number three. (Enters in input mode)
  This is line number three
  .

Line number three now contains "This is line number three" text, old content is deleted.

APPEND COMMAND

  2a : appends after second line. (Enters in input mode)

  foo bar baz # this will be new third line
  . # returns to command mode

Former third line now is placed in fourth position, new "foo bar baz" line appears in third place.

INSERT COMMAND

  3i : inserts new line number three, moving everyone afterwards one position.

  hello
  .

Former third line is moved to fourth position. "hello" is new third line.

DELETE A LINE

  3d : deletes line number three.

MOVE A LINE

  1m$ : move first line after the end of the file.
  2m0 : move second line after the beginning of the file.
  3m1 : move third line after first line.

EXECUTE A COMMAND LIST

g/Regexp/command_list : Finds lines matching Regexp and executes a command list on them.

E.g:

  # finds every line containing "foo",
  # changes "oo" into "bar", and print that line.
  g/foo/s/oo/bar/p