Advanced Editing - PowerPoint PPT Presentation

1 / 31
About This Presentation
Title:

Advanced Editing

Description:

wrapscan (ws) - wraps around the beginning or end of edit buffer during searches ... Sometimes you need to insert non-printable characters or characters that are not ... – PowerPoint PPT presentation

Number of Views:34
Avg rating:3.0/5.0
Slides: 32
Provided by: Kenneth4
Category:
Tags: advanced | editing

less

Transcript and Presenter's Notes

Title: Advanced Editing


1
Advanced Editing
  • ex/vi are powerful editors
  • There are many capabilities which allow vi to be
    your every day editor
  • Editing multiple files
  • Writing part of a buffer to a file
  • Inserting a file into the current buffer
  • Executing and inserting the results of shell
    commands into the buffer
  • Using shell commands to perform operations on the
    current buffer
  • To exit vi and invoke ex, enter the command Q
  • vi then returns you to vi

2
Cursor Movement
  • By Character
  • h, j, k, l (?, ? ,?,?)
  • By Text Objects
  • w or W Forward a word
  • b or B Backwards a word
  • e or E End of word
  • ) and ( Beginning of next, previous sentence

3
Movement by Lines
  • 0, Beginning, end of current line
  • First character of current line (ignore
    spaces)
  • , - Next, previous line
  • n Column n of current line
  • H Top line of screen
  • M Middle line of screen
  • L Bottom line of screen
  • nH n (number) of lines after top line
  • nL n (number) of lines before bottom

4
Command Multipliers
  • Almost any command can be executed multiple times
    by using a command multiplier
  • Examples
  • 4j moves down 4 lines
  • 6w or 6W moves forward 6 words
  • 10dd deletes 10 lines, starting with the current
    line
  • 5x deletes 5 characters
  • y4w copies 4 words to the anonymous buffer

5
Screen Movement
  • CTRL-F , CTRL-B Scroll forward/backwards one
    screen
  • CTRL-D , CTRL-U Scroll down/up one-half
    screen
  • CTRL-E , CTRL-Y Show one more line at
    bottom/top of window
  • z RETURN Reposition current line to top of
    screen
  • z. Reposition current line to middle of
    screen
  • z- Reposition current line to bottom of
    screen

6
Searches
  • The search syntax in vi is similar to ed but only
    uses one search indicator
  • /expr searches forward for expr
  • ?expr searches backwards for expr
  • n, N repeat last search in same, opposite
    direction
  • /, ? repeat previous search forward,
    backward
  • fx search forward for x on current line
  • Fx search backwards for x on current line
  • repeat previous current-line search
  • , repeat previous current line search in
    opposite direction

7
Switching Files In Current Session
  • The command e lets you load in a new file to
    edit
  • The current file should be written (saved) first
  • Two symbols "remember" the last two file names
    you have used
  • refers to the current file
  • refers to the previous file
  • e will switch back to the previous file
  • If you haven't saved the current file before
    issuing an e command, ex will tell you
  • e! filename will force the current edits to be
    discarded and the new file to be loaded
  • e! by itself will discard your current edits and
    return to the last saved version

8
  • is mainly useful when you want to save the
    current buffer under a new name
  • w .new will save the current buffer (containing
    file filename) as filename.new

9
Writing
  • w Write (save) file
  • w! Write file (overriding file protection)
  • 3,9w newfile Write lines 3 through 9 to newfile
  • 3,9wgtgtfile Write lines 3 through 9 appending
    to file
  • w.new Append ".new" to the current file
    name and write file

10
Reading
  • r newfile Read newfile into current edit file
  • r !command Read the output of Unix command
    into current edit file
  • e! Return to version of current file at
    time of last write (discarding all
    changes since last write)
  • e file2 Edit file2 without leaving vi
  • e Edit alternate file

11
Executing Shell Commands
  • Often it is useful to be able to execute a shell
    command from within vi
  • There are several options to do this
  • Executing shell commands
  • Temporarily exiting to a subshell
  • Reading the results into your file
  • Filtering with shell commands

12
  • Executing shell commands from vi
  • ! cmd executes the shell command cmd
  • The results of cmd appear on your screen, press
    Enter to return to your editing session
  • To get the results of the shell command into your
    file, use the read (r) command
  • r !cmd will insert the results of cmd into your
    edit buffer
  • If you want to temporarily exit to a subshell,
    use sh
  • This places you in a subshell where you can
    perform anything you would at the command prompt
  • Control D returns you to vi

13
Filtering Text Through a Shell Cmd
  • Text is filtered through a command shell by
    entering an ! followed by any vi movement command
    that indicates a full line or more of text
    followed by the Unix command
  • This command sequence has some oddities
  • The ! doesnt appear until you type the text
    object reference, and then it appears at the
    bottom of the screen but the object reference
    doesnt display
  • The text object must be more than one line
  • There is a special text object available, !,
    which indicates the current line
  • To repeat the previous command, enter ! object !

14
Commonly Used Filters
  • sort - sort a file
  • tr - translate characters to different characters
  • spell - check the spelling of a list of words
  • wc - count the number of words, characters and
    lines in a file
  • grep - search files for a pattern
  • head and tail - list first/last part of a file
  • sed - stream editor
  • awk - patern-action pair programming language
  • nroff/troff - format text for printing

15
vi Buffers
  • vi is also based on memory buffers, like ed,
    changes are made to the buffer, not your file
    until a write is performed
  • vi has three kinds of buffers
  • the normal default buffer your edit file resides
    in
  • the anonymous buffer, a special buffer used for
    copying text
  • named
  • 1-9 hold the last nine deletions
  • a-z for storing copied text (like the Windows
    clipboard)

16
Recovering a Buffer
  • If the system goes down during an edit session,
    vi will attempt to save your current edit buffer
  • On your next login, enter vi -r and vi will list
    any edit buffers the system has saved
  • To recover any of these buffers, use vi -r
    filename to reload the saved buffer
  • You can force the system to preserve your current
    edit buffer by using pre although there usually
    isnt a good reason to do this

17
Using the Named Deletion Buffers
  • The last 9 deletions are stored in numbered
    buffers from 1-9
  • Only major deletions small deletions, such as
    characters or parts of lines can only be restored
    by using a p command immediately after the
    deletion
  • To recover a deletion, enter "np where n is the
    number of the deletion you want to recover
  • "2p would be the second-to-last deletion, "4p
    would be fourth from last, etc.

18
What if I Dont Know Which Buffer?
  • If you dont know which buffer contains the
    deletion you want, vi will cycle through them for
    you
  • "1pu.u.u etc
  • This inserts the last deletion, undoes the
    insert, and the . (period) advances to the next
    buffer and inserts it

19
Using Named Buffers for Copies
  • When copying text you want to use multiple times,
    named buffers can be used
  • There are 26 named buffers (a-z)
  • To use a named buffer, precede the yank or put
    command with a " and the buffer name
  • "xyy Copy current line into named buffer x
  • "xP Put text from named buffer x before
    cursor

20
Copying Text Between Files
  • vi file1
  • Copy desired text to a named buffer
  • "a6Y
  • e file2
  • Paste your text into the new file
  • "ap
  • w and save your changes
  • If you want, you can then switch back and forth
    between file1 and file2 using
  • e

21
Customizing vi Using Options
  • vi can be customized using set commands
  • Two kinds of set commands exist
  • Toggles, which are either On of Off
  • Options which take values or strings
  • Toggles are activated/deactivated by
  • set option to turn option On
  • set nooption to turn option Off
  • Value options are set using
  • set optionvalue or set optionstring

22
Viewing Current Options
  • set all will display a complete list of current
    options, both those set by you and those that are
    defaulted by vi
  • set option? displays the current value of option
  • set shows options that either you set during the
    edit session or were set via your .exrc file

23
Some Useful Options
  • wrapmargin (wm) - causes vi to automatically word
    wrap text as you type
  • value gives minimum distance from right margin
  • Three options that control searching
  • noignorecase (noic) - differentiates between
    upper and lower case
  • wrapscan (ws) - wraps around the beginning or end
    of edit buffer during searches
  • magic (magic) - recognizes wildcards (., . )
    in searches

24
Options Useful for Programmers
  • autoindent (ai) - in insert mode, indents each
    line to the same level as the one above it
  • showmatch (sm) - when ) or is entered, vi
    momentarily moves to matching ( or
  • tabstop (ts) - defines number of spaces that a
    TAB produces
  • shiftwidth (sw) - defines number of spaces in
    backwards TAB (Ctrl-D) when using autoindent
  • number (nu) - displays line numbers when editing
  • list (list) - displays TABs as I, marks end of
    lines with

25
.exrc File
  • When vi starts, it reads the .exrc file in your
    home directory, if it exists
  • Use this file to customize vi with any options,
    abbreviations or key-command maps you always want
    to use
  • An example .exrc file might look like this
  • set wm10
  • set exrc
  • ab nlcc North Lake Community College
  • set autowrite

26
Alternate Environments
  • vi will also read a .exrc file in your current
    directory
  • This is useful if you want vi to behave
    differently for specific projects, such as C or
    lisp programming
  • Example for C programming
  • set nu
  • set autoindent
  • set ts3
  • set shiftwidth3
  • set showmatch
  • You can also read a file of options at anytime
    using so filename

27
Abbreviations
  • You can define shortcuts, or abbreviations for
    commonly used phrases in vi
  • set ab abbreviation phrase
  • Example
  • set ab ibm International Business Machines
  • These can then be used in insert mode
  • Example
  • The ibm corporation
  • will expand to
  • The International Business Machines corporation

28
More Abbreviations
  • To disable a previous abbreviation use
  • unab abbreviation
  • To list currently defined abbreviations
  • ab
  • The characters that compose your abbreviation
    cannot appear at the end of your phrase
  • ab IBM the corporation known as IBM
  • This will cause a recursion error, creating an
    infinite loop
  • Using the characters of your abbreviation within
    your phrase may or may not produce an error but
    is strongly discouraged

29
Mapping Frequent Commands
  • To save frequently used command sequences to a
    shortcut, use
  • map x sequence
  • unmap x will disable the sequence
  • map will display all currently mapped characters
  • To use map, you need to know the characters that
    arent used by vi in command mode
  • g K q V v _ \ Ctrl-A Ctrl-K Ctrl-O Ctrl-T
    Ctrl-W Ctrl-X
  • Example key mappings
  • map g dwelp will delete the current word (dw),
    move to the end of the next word (e), move right
    one space (l) and put the deleted word there (p).
    In other words, the mapping for g will reverse
    any two words.

30
Inserting Special Characters
  • Sometimes you need to insert non-printable
    characters or characters that are not readily
    available from the keyboard
  • CTRL-V tells ex and vi to take whatever is typed
    next literally
  • For example, to enter a "bell" character into
    your file, type CTRL-V CTRL-G

31
Protecting Keys From ex
  • ENTER, ESC, BACKSPACE, DELETE, Ctrl-T, Ctrl-W,
    and Ctrl-X can only be used in a map or
    abbreviation if first "escaped" by Ctrl-V
  • Example
  • ab address 123 Floyd RdMPlano,TXM75243
  • M is what will display when you enter Ctrl-V
    ENTER
  • map T dwelp Ctrl-V Ctrl-T displays a T
Write a Comment
User Comments (0)
About PowerShow.com