LFS101x: Chapter 11 - Text Editors

Basic Editors

There are two standard ways to create a file from the command line without using an editor and filling it with content

  • Using echo repeatedly
    • ex:

    $ echo line one > myfile

    $ echo line two >> myfile

    $ echo line three >> myfile

  • These commands produce a file with the following line in it:

    line one

    line two

    line three

  • A single greater-than sign (>) sends the output of a command to a file

  • Two greater-than signs (>>) append the new output to an existing file

  • Using cat combined with redirection

    • ex:

    $ cat << EOF > myfile

    > line one

    > line two

    > line three

    > EOF

    $

  • These commands produce a file with the following line in it:

    line one

    line two

    line three

nano

nano is a text terminal-based editor

  • Use the nano command followed by a file name as an argument

    • Syntax: nano
    • If the file doesn't exist, it'll be created
  • nano provides a two-line shortcut bar at the bottom of the screen that lists the available commands, such as

    • CTRL-G - Displays the help screen

    • CTRL-O - Writes to a file

    • CTRL-X - Exits a file

    • CTRL-R - Inserts contents from another file into the current buffer

    • CTRL-C - Shows cursor position

gedit and kwrite

gedit is a graphical text editor that's part of the GNOME desktop system, while kwrite is associated with KDE

  • They're both very easy to use, capable, and very configurable

  • To open a new file you can either find the program in your desktop's menu system or you can type gedit in the command line

    • If the file doesn't exist, it'll be created

vi

vi, also known as vim (Vi Improved), is a standard tool installed on virtually all Linux distributions

  • GNOME extends vi with a very graphical interface known as gvim, while KDE offers kvim

  • When using vi, all commands are entered through the keyboard

vimtutor
  • Typing vimtutor launches a short but very comprehensive tutorial for basic vi commands
Three Modes in vi
  • Command

    • by default, vi starts in Command mode
    • Each key is an editor command
    • Keyboard strokes are interpreted as commands that can modify the contents
  • Insert

    • Type i to switch to Insert mode from Command mode

    • Insert mode is used to enter/insert text into a file

    • Insert mode is indicated by an "? INSERT ?" indicator at the bottom of the screen

    • Press Esc to exit Insert mode and return to Command mode

  • Line

    • Type : to switch to the Line mode from Command mode

    • Each key is an external command, including operations such as writing the file contents to disk or exiting

    • Uses line editing commands inherited from older line editors

    • Most of these commands are actually no longer used

    • Press Esc to exit Line mode and return to Command mode

Working with files in vi
  • The ENTER key needs to be pressed after all of these commands

    • vi myfile (Starts the editor and edits myfile)

    • vi -r myfile (Starts and edits myfile in recovery mode from a system crash)

    • :r file2 (Reads in file2 and insert at current position)

    • :w (Writes to the file)

    • :w myfile (Writes out to myfile)

    • :w! file2 (Overwrites file2)

    • :x or :wq (Exits and write out the modified file)

    • :q (Quit)

    • :q! (Quit even though modifications have not been saved)

Changing cursor positions in vi
  • Line mode commands (those following colon L ) require the ENTER key to be pressed after the command is typed

    • Arrows keys (Moves up, down, left and right)

    • j or (Moves one line down)

    • k (Moves one line up)

    • h or Backspace (Moves on character left)

    • l or Space (Moves on character right)

    • 0 (Moves to the beginning of the line)

    • $ (Moves to the end of the line)

    • w (Moves to the beginning of the next word)

    • :0 or 1G (Moves to the beginning of the file)

    • :n or nG (Moves to line n)

    • :$ or G (Moves to the last line in the file)

    • CTRL-F or Page Down (Moves forward one page)

    • CTRL-B or Page Up (Moves backwards one page)

    • ^l (Refreshes and centers the screen)

Searching for Text in vi
  • The ENTER key should be pressed after typing the search pattern

  • Search commands

    • /pattern (Searches forward for the pattern)

    • ?pattern (Searches backwards for the pattern)

  • Keystrokes used when searching for text in vi

    • n (Moves to the next occurrence of the search pattern)

    • N (Moves to the previous occurrence of the search pattern

Working with text in vi
  • Keystrokes used when changing, adding, and deleting text in vi

  • a (Appends the text after cursor; Stop upon Escape key)

    • A (Appends the text at the end of the current line; Stop upon Escape key)

    • i (Inserts text before the cursor; Stop upon Escape key)

    • I (Inserts text at the beginning of the current line; Stop upon Escape key)

    • o (Starts a new line below the current line, insert text there; Stop upon Escape key)

    • O (Starts a new line above the current line, insert text here; Stop upon Escape key)

    • r (Replaces character at the current position)

    • R (Replaces the text starting with the current position; Stop upon Escape key)

    • x (Deletes the character at the current position)

    • Nx (Deletes N characters, starting at the current position)

    • dw (Deletes the word at the current position)

    • D (Deletes the rest of the current line)

    • dd (Deletes the current line)

    • Ndd or dNd (Deletes N lines)

    • u (Undos the previous operation)

    • yy (Yanks/copies the current line and puts it in a/the buffer)

    • Nyy or yNy (Yanks/copies N lines and puts it in a/the buffer)

    • p (Pastes at the current position the yanked line or lines from the buffer)

Using external commands in vi
  • Typing the sh command opens an external command shell

    • When you exit the shell, you'll resume your editing session
  • Typing ! executes a command from within vi (The command follows the exclamation point)

    • This technique is best suited for non-interactive commands, such as : ! wc % (Typing this will run the wc (word count) command on the file; the % character represents the file that's currently being edited)

emacs

  • Unlike vi, it doesn't work with modes

  • emacs is highly customizable and includes a large number of features

    • It can be used for text editing, email, debugging, etc.
  • Rather than having different modes for command and insert, like vi, emacs uses the CTRL and Meta (Alt or Esc) keys for special commands

emacs commands
  • emacs myfile (Start emacs and edit myfile)

  • CTRL-x i (Inserts prompted for file at current position)

  • CTRL-x s (Saves all files)

  • CTRL-x CTRL-w (Writes to the file giving a new name when prompted)

  • CTRL-x CtRL-s (Saves the current file)

  • CTRL-x CTRL-c (Exits after being prompted to save any modified files)

  • CTRL-h t (The emacs tutorial)

Changing cursor position in emacs
  • Arrow keys (Moves up, down, left and right)

  • CTRL-n (Moves one line down)

  • CTRL-p (Moves one line up)

  • CTRL-f (Moves one character forward/right)

  • CTRL-b (Moves one character back/left)

  • CTRL-a (Moves to the beginning of the line)

  • CTRL-e (Moves to the end of the line)

  • Meta-f (Moves to the beginning of the next word)

  • Meta-b (Moves back to the beginning of the preceding word

  • Meta-< (Moves to the beginning of the file)

  • Meta-g-g-n or Esc-x Goto-line n' (Moves to line n)

  • Meta-> (Moves to the end of the file)

  • CTRL-v or Page Down (Moves forward one page)

  • Meta-V or Page Up (Moves backwards one page)

  • CTRL-l (Refreshes and centers the screen)

Searching for text in emacs
  • CTRL-s (Searches forward for the prompted pattern, or for the next pattern)

  • CTRL-r (Searched backwards for the prompted pattern, or for the next pattern)

Working with text in emacs
  • CTRL-o (Inserts a blank line)

  • CTRL-d (Deletes the character at the current position)

  • CTRL-k (Deletes the rest of the current line)

  • CTRL-_ (Undoes the previous operation)

  • CTRL-space or CTRL-@ (Marks the beginning of the selected region; The end will be at the cursor position)

  • CTRL-w (Deletes the current marked text and writes it to the buffer)

  • CTRL-y (Inserts at the current cursor location whatever was most recently deleted)