Drafting Commands, Metaediting Part II: The Core Commands - - PowerPoint PPT Presentation

drafting commands metaediting
SMART_READER_LITE
LIVE PREVIEW

Drafting Commands, Metaediting Part II: The Core Commands - - PowerPoint PPT Presentation

Lecture 8: Visual Mode, /<CR>, Drafting Commands, Metaediting Part II: The Core Commands Announcements HW3... is postponed to next week... Too few things to test on Make sure to practice on commands discussed so far. Every


slide-1
SLIDE 1

Lecture 8: Visual Mode, /<CR>, Drafting Commands, Metaediting

Part II: The Core Commands

slide-2
SLIDE 2

Announcements

  • HW3... is postponed to next week...

– Too few things to test on – Make sure to practice on commands discussed so far. Every single command in this course is important! (Each has an equal chance of showing up on the exams)

slide-3
SLIDE 3

Last Time

  • Text Objects!
  • Motions....
slide-4
SLIDE 4

Last Time

  • Text Objects!
  • Motions....
  • Today: Visual Mode!
slide-5
SLIDE 5

Reminder...

  • As always, in regular text editing, there

is a certain order that we tend to prioritize: 1.Text objects: views objects as fundamental elements of text

  • 1. word/WORD objects
  • 2. line objects
  • 3. enclosed objects
slide-6
SLIDE 6

Reminder...

  • As always, in regular text editing, there

is a certain order that we tend to prioritize:

  • 2. Motions: deals with "chunks of text"

that cannot be expressed by simply stating what objects they are.

slide-7
SLIDE 7

Reminder...

  • As always, in regular text editing, there

is a certain order that we tend to prioritize:

  • 2. Motions: deals with "chunks of text"

that cannot be expressed by simply stating what objects they are.

– Usually complicated combinations of

  • bjects.

– Are straightforward, and uninteresting.

slide-8
SLIDE 8

Reminder...

  • As always, in regular text editing, there

is a certain order that we tend to prioritize:

  • 2. Motions: deals with "chunks of text"

that cannot be expressed by simply stating what objects they are.

– What could possibly go wrong?

slide-9
SLIDE 9
  • Visual Mode
  • 1 Character Editing Commands and

Drafting Commands

  • /<pattern><CR> and Metaediting.
slide-10
SLIDE 10
  • Visual Mode
  • 1 Character Editing Commands and

Drafting Commands

  • /<pattern><CR> and Metaediting.
slide-11
SLIDE 11

Issues with Motions

slide-12
SLIDE 12

Issues with Motions

Task: Change to "in C, we can use ``variableinstance.function'' to access its member variables."

slide-13
SLIDE 13

Issues with Motions

Task: Change to "in C, we can use ``variableinstance.function'' to access its member variables." Well?

slide-14
SLIDE 14

Issues with Motions

Task: Change to "in C, we can use ``variableinstance.function'' to access its member variables." Well? Obvious Solution: c/.<CR>yaddayadda<Esc>

slide-15
SLIDE 15

Issues with Motions

Task: Change to "in C, we can use ``variableinstance.function'' to access its member variables." Well? Obvious Solution: c/.<CR>yaddayadda<Esc>

Nonobvious Result: ...

slide-16
SLIDE 16

Issues with Motions

  • The gotcha:

c<object>

  • Command deals with only ONE object or

ONE motion!

slide-17
SLIDE 17

Issues with Motions

  • The gotcha:

c<object>

  • Command deals with only ONE object or

ONE motion!

  • How do we deal with complicated

descriptions? (relative, cheating, etc...)

slide-18
SLIDE 18

In the good ol' days of vi...

slide-19
SLIDE 19

In the good ol' days of vi...

slide-20
SLIDE 20

In the good ol' days of vi...

Can't do that...

slide-21
SLIDE 21

In the good ol' days of vi...

Can't do that...

(workaround: marks, regex tricks, more accurate regex matching)

slide-22
SLIDE 22

In the good ol' days of vi...

slide-23
SLIDE 23

Today: Vim

Editing Commands follow the format: <command><object/motion>

slide-24
SLIDE 24

Today: Vim

Editing Commands follow the format: <command><object/motion>

Issue: how do we know that

  • ur description has ended?
slide-25
SLIDE 25

Possible Workarounds

  • Some kind of termination signal to tell

Vim that we have finished our description...

– Very Problematic: no longer vi-compatible, extra keystrokes for EVERY single edit command, too many commands (keyboard fully mapped)

slide-26
SLIDE 26

Possible Workarounds

  • Some kind of termination signal to tell

Vim that we have finished our description...

– Very Problematic: no longer vi-compatible, extra keystrokes for EVERY single edit command, too many commands (keyboard fully mapped)

  • What Vim actually does: Visual Mode!
slide-27
SLIDE 27

Visual Mode: The Idea

  • Nearly all Vim commands have a clear

seperation of editing process:

  • 1. Locate
  • 2. Edit
  • 3. (Repeat)
  • These commands are hence disjoint!
slide-28
SLIDE 28

Visual Mode: The Idea

  • Instead of structuring our commands

like this: <command><object/motion>

  • We do this:

<object/motion><command>

slide-29
SLIDE 29

Visual Mode: The Idea

  • Instead of structuring our commands

like this: <command><object/motion>

  • We do this:

<object/motion><command>

  • How do we know that our motion has

terminated?

slide-30
SLIDE 30

Visual Mode: The Idea

  • Instead of structuring our commands like

this: <command><object/motion>

  • We do this:

<object/motion><command>

  • How do we know that our motion has

terminated? When we reach an edit command!

slide-31
SLIDE 31

Visual Mode

  • Three kinds, with radically different

use cases:

– Visual Mode (today) – Visual Line Mode – Visual Block Mode

slide-32
SLIDE 32

Visual Mode

  • Three kinds, with radically different

use cases:

– Visual Mode (today)

  • to allow complicated descriptions for locates

– Visual Line Mode

  • to allow "chuck" execution of ex commands or

normal mode commands

– Visual Block Mode

  • practically useless... very few contrived use

cases.

slide-33
SLIDE 33

Visual Mode

  • Descriptions are highlighted.
  • Enter/Exit with the v command.
  • Demo.
  • Use case: nearly always paired with use
  • f f and t commands! Deals with any

kind of code that is even slightly complicated.

slide-34
SLIDE 34

Visual Mode

  • Commands:
  • Nearly all normal mode commands work

as expected, only in reverse order: v<object/motion><command>

  • v - enter/escape visual mode
  • V, <C-v> - switch to visual line/block

mode

slide-35
SLIDE 35

Visual Mode

  • Commands:
  • gv - reselect previous selection

– VERY useful for redoing typos

  • o - go to the other end of the selection

– Used in combination with gv for convenient continuting navigation.

slide-36
SLIDE 36

Visual Mode

  • Commands:
  • gv - reselect previous selection

– VERY useful for redoing typos

  • Hm.... How does gv work?

– where and how is previous selection stored? – what happens if we perform edits?

slide-37
SLIDE 37

Visual Mode

  • Commands:
  • gv - reselect previous selection

– VERY useful for redoing typos

  • Hm.... How does gv work?

– where and how is previous selection stored? – what happens if we perform edits?

  • Will be addressed in HW 3!
slide-38
SLIDE 38
  • Visual Mode
  • 1 Character Editing Commands and

Drafting Commands

  • /<pattern><CR> and Metaediting.
slide-39
SLIDE 39

Miscellaneous: 1-Character Commands

  • ... are used nearly exclusively to fix one

character typos.

  • Used surprisingly frequently: one

character typos happen often.

  • Optimized keystrokes.
  • Fixed Pattern:

/<line><CR>f<char><1-char-edit>

slide-40
SLIDE 40

Miscellaneous: 1-Character Commands

  • Commands:
  • r<char> - replace character with <char>
  • x - delete character
  • s - change character (go to insert mode)
  • vU/vu - trick to capitalize/lowercase one

character.

  • J - command to trim end of line and join two

lines together. (not quite 1 character)

slide-41
SLIDE 41

Drafting Commands

  • Drafting: appending/inserting huge

chuncks of text.

slide-42
SLIDE 42

Drafting Commands

  • Drafting: appending/inserting huge

chuncks of text.

  • Not much involved, remember a few

very common use case patterns:

slide-43
SLIDE 43

Drafting Commands

  • Base Commands:
  • i - insert in front of cursor
  • a - append at end of cursor
  • I - insert in front of line
  • A - append at end of cursor
slide-44
SLIDE 44

Drafting Commands

  • VERY Common Patterns:
  • /<line><CR>I/A: insert in front
  • f/append at end of line containing

<line>.

  • /<word><CR>i: insert in front of <word>
  • /<word><CR>ea: append at end of

<word>

  • /<line><CR>f<char>i/a: insert in front
  • f/append at end of <char> from <line>
slide-45
SLIDE 45

Speaking of Which...

  • The /<pattern><CR> command...
  • ... is THE MOST IMPORTANT command

in Vim!

slide-46
SLIDE 46

Speaking of Which...

  • The /<pattern><CR> command...
  • ... is THE MOST IMPORTANT command

in Vim!

  • Which we will devote the rest of

today's lecture to!

slide-47
SLIDE 47
  • Visual Mode
  • 1 Character Editing Commands and

Drafting Commands

  • /<pattern><CR> and Metaediting.
slide-48
SLIDE 48

/<pattern><CR> ... again!

  • ... is very very versatile!
slide-49
SLIDE 49

/<pattern><CR> ... again!

  • ... is very very versatile!
  • But first, what's a <pattern>?
slide-50
SLIDE 50

vimregex

  • a <pattern> is a form of vim regular

expression.

slide-51
SLIDE 51

vimregex

  • a <pattern> is a form of vim regular

expression.

  • Depressing fact of life: vim has 4

different flavors of regular expressions!

slide-52
SLIDE 52

vimregex

  • a <pattern> is a form of vim regular

expression.

  • Depressing fact of life: vim has 4

different flavors of regular expressions!

– NONE of which is EQUIVALENT to regular expressions of programming languages. – Only SIMILAR, which means that you'll still have to "relearn" it.

slide-53
SLIDE 53

vimregex

  • 4 flavors:
  • verynomagic
  • nomagic
  • magic
  • verymagic
  • ... Each with their own rules!
  • ... and extremely questionable reasons

for having 4 different standards...

slide-54
SLIDE 54

vimregex

  • 4 flavors:
  • verynomagic
  • nomagic
  • magic
  • verymagic
  • ... Each with their own rules!
  • ... and extremely questionable reasons

for having 4 different standards...

slide-55
SLIDE 55

vimregex

  • Good news: you don't have to know any

particular flavor! (I can't memorize it all either).

  • Onwards, we will use the 'magic' regex

syntax:

– put this in vimrc:

  • set magic
slide-56
SLIDE 56

vimregex

  • Bottom Line:

– Vim has 4 flavors of regex. – Each regex has its own conventions. – Used for pattern matching. – vimregex is NOT regular! There are capture groups.

slide-57
SLIDE 57

vimregex

  • So what can we do?
slide-58
SLIDE 58

vimregex

  • So what can we do?
  • Useful facts:
  • . - matches any character.
slide-59
SLIDE 59

vimregex

  • So what can we do?
  • Useful facts:
  • . - matches any character.
  • Very useful for matching characters

that are annoying to type (\n) or have bad ergonomics (&) etc...

slide-60
SLIDE 60

vimregex

  • So what can we do?
  • Useful facts:
  • . - matches any character.
  • * - matches any number of previous

group.

slide-61
SLIDE 61

vimregex

  • So what can we do?
  • Useful facts:
  • . - matches any character.
  • * - matches any number of previous

group.

  • .* - matches any phrase that you

REALLY don't wanna type.

slide-62
SLIDE 62

Back to /<pattern><CR>

  • Like many commands in Vim, it can

either be a forward direction or a reverse direction.

slide-63
SLIDE 63

Back to /<pattern><CR>

  • Like many commands in Vim, it can

either be a forward direction or a reverse direction. /<pattern><CR>

  • Matches next pattern

?<pattern><CR>

  • Matches previous pattern
slide-64
SLIDE 64

Is there more to /pattern<CR>?

  • Yes! But first, the interface...
slide-65
SLIDE 65

Command Line Interface

  • The /<pattern><CR> command has

"nearly" the exact same interface as typing out ex commands.

  • Properties:

– One Line – Insert Mode Commands – History Recall

slide-66
SLIDE 66

Command Line Interface

  • The /<pattern><CR> command has "nearly" the exact

same interface as typing out ex commands.

  • Commands:
  • <C-n> - unix standard for next search history
  • <C-p> - unix standard for previous search history
  • <C-w> - unix standard for delete word (but follows

Vim's definition of word)

  • <C-u> - unix standard for delete line
  • <C-r> - insert from register
slide-67
SLIDE 67

Command Line Interface

  • Commands:
  • Important:
  • <C-r><C-w> - insert word under cursor!
  • <C-r><C-a> - insert WORD under

cursor!

  • Exceedingly useful! Saves a lot of

typing.

slide-68
SLIDE 68

Command Line Interface

  • Commands:
  • Important:
  • <C-r><C-w> - insert word under cursor!
  • <C-r><C-a> - insert WORD under

cursor!

  • Exceedingly useful! Saves a lot of

typing.

  • Can we do that from normal mode as

well? YES!

slide-69
SLIDE 69

Command Line Interface

  • Commands:
  • In normal mode:
  • * - searches word under cursor
  • # - reverse searches word under cursor
  • Saves going into command line

altogether!

slide-70
SLIDE 70

Command Line Interface

  • But, is there EVEN MORE?
slide-71
SLIDE 71

Command Line Interface

  • But, is there EVEN MORE?
slide-72
SLIDE 72

Command Line Interface

  • But, is there EVEN MORE?
  • It'll be sad if everything just ends here...
  • We would expect that if we made a

mistake in the command line interface, we should have equal power to fix it..

– same tools: locate, edit, repeat, etc...

slide-73
SLIDE 73

Command Line Interface

  • First, a detour:
  • Where, and how, does Vim stores its

search history?

slide-74
SLIDE 74

Command Line Interface

  • First, a detour:
  • Where, and how, does Vim stores its

search history?

– .viminfo file – loaded into a specialized memory segment.

slide-75
SLIDE 75

Command Line Interface

  • First, a detour:
  • Where, and how, does Vim stores its

search history?

– .viminfo file – loaded into a specialized memory segment.

slide-76
SLIDE 76

Command Line Interface

  • First, a detour:
  • Where, and how, does Vim stores its

search history?

– .viminfo file – loaded into a specialized memory segment.

  • What does that mean?
slide-77
SLIDE 77

Metaediting

  • Since we have stored our previous

searches, there should be a mechanism to quickly perform edits on previous searches for editing. (Hence, metaediting. Editing your edits).

slide-78
SLIDE 78

Simple Case: flags

  • A more general syntax of search:

/<pattern>/<flags><CR>

  • If <pattern> is left blank, automatically

repeats previous search!

slide-79
SLIDE 79

Simple Case: flags

  • A more general syntax of search:

/<pattern>/<flags><CR>

  • If <pattern> is left blank, automatically

repeats previous search!

  • Most important <flag>:
  • e - places cursor at end of match
slide-80
SLIDE 80

Simple Case: flags

  • A more general syntax of search:

/<pattern>/<flags><CR>

  • If <pattern> is left blank, automatically repeats

previous search!

  • Example:
  • /hello<CR> - matches "hello"
  • //e<CR> - matches "hello" and moves cursor to 'o'
  • /<C-r><C-w> there/e - matches "hello" there and moves

cursor to end of the "there".

slide-81
SLIDE 81

Even more powerful metaediting!

  • Command line history of searches!
  • Works exactly as expected (with modes, etc).
  • Commands:
  • q/ - open command line history
  • <C-f> - when typing out pattern, switch to

command line history

  • <C-c> - switch back from command line history

to command line.

  • demo
slide-82
SLIDE 82

Even more powerful metaediting!

  • Command line history of searches!
  • Works exactly as expected (with modes, etc).
  • Commands:
  • q/ - open command line history
  • <C-f> - when typing out pattern, switch to

command line history

  • <C-c> - switch back from command line history

to command line.

  • demo
slide-83
SLIDE 83

Next Time

  • What about registers and macros?
  • What can we do with visual line mode and ex

commands?

  • There are a ton of commands today! Practice

practice practice.

  • All of them are important, put them in finger

memory!