Lecture 2: Vim's Giant Flaw Part I Recurring Themes Annoucements - - PowerPoint PPT Presentation

lecture 2 vim s giant flaw
SMART_READER_LITE
LIVE PREVIEW

Lecture 2: Vim's Giant Flaw Part I Recurring Themes Annoucements - - PowerPoint PPT Presentation

Lecture 2: Vim's Giant Flaw Part I Recurring Themes Annoucements Homework 1 will be out by tonight! Covers everything up to and including this lecture. No commands will be covered (as usual), we will get to that in the second part of


slide-1
SLIDE 1

Lecture 2: Vim's Giant Flaw

Part I Recurring Themes

slide-2
SLIDE 2

Annoucements

  • Homework 1 will be out by tonight!
  • Covers everything up to and including this

lecture.

  • No commands will be covered (as usual), we

will get to that in the second part of the course.

  • Due by the beginning of next lecture. Print.
slide-3
SLIDE 3

Last Time

  • The Vim Philosophy!
  • Why file explorers are usually bad.
  • Describe, don't Explain!
slide-4
SLIDE 4

Last Time

  • The Vim Philosophy!
  • Why file explorers are usually bad.
  • Describe, don't Explain!
  • What happens when we are dealing with

code?

slide-5
SLIDE 5
  • A few loose ends...
  • Ambiguity
  • Ways to resolve it
slide-6
SLIDE 6
  • A few loose ends...
  • Ambiguity
  • Ways to resolve it
slide-7
SLIDE 7

Relative Line Numbers ... are BAD

  • vimtutor
  • 10j
  • 4w
  • <number>command
  • Also referred to as counts.
slide-8
SLIDE 8

Relative Line Numbers ... are BAD

slide-9
SLIDE 9

Relative Line Numbers ... are BAD

slide-10
SLIDE 10

Relative Line Numbers ... are BAD

slide-11
SLIDE 11

Relative Line Numbers ... are BAD

Um...

slide-12
SLIDE 12

Relative Line Numbers ... are BAD

Um... 18 words?

slide-13
SLIDE 13

Relative Line Numbers ... are BAD

Um... 18 words?

18w

slide-14
SLIDE 14

Relative Line Numbers ... are BAD

Um... 18 words?

18w

slide-15
SLIDE 15

Relative Numbers ... are BAD

I want to locate the 'std' after namespace. Okay, so I'm at the top of the code, and the word 'std' is 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18 words away... Okay, so 18w. Vim: so I can count 1, 2, 3, .. 18, words away from now, so that's the 'std' after namespace, ohhhh.... Move cursor to the 'std' after 'namespace'...

slide-16
SLIDE 16

Relative Numbers ... are BAD

I want to locate the 'std' after namespace. Okay, so I'm at the top of the code, and the word 'std' is 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18 words away... Okay, so 18w. Vim: so I can count 1, 2, 3, .. 18, words away from now, so that's the 'std' after namespace, ohhhh.... Move cursor to the 'std' after 'namespace'...

slide-17
SLIDE 17

Relative Numbers ... are BAD

  • But hey, I do know how to locate to the

'std' after 'namespace'....

slide-18
SLIDE 18

Relative Numbers ... are BAD

  • But hey, I do know how to locate to the

'std' after 'namespace'....

  • goto 'std' after 'namespace'.
slide-19
SLIDE 19

Relative Numbers ... are BAD

  • But hey, I do know how to locate to the 'std'

after 'namespace'....

  • goto 'std' after 'namespace'.

/namespace<CR>/std<CR>

slide-20
SLIDE 20

Relative Numbers ... are BAD

  • But hey, I do know how to locate to the 'std'

after 'namespace'....

  • goto 'std' after 'namespace'.

/namespace<CR>/std<CR>

  • Moral of the story? Don't make life

unneccesarily hard for yourself! Describe, don't Explain!

slide-21
SLIDE 21

Relative Numbers ... are BAD

  • vimtutor is bad!
  • Rule of thumb for relative numbers:
slide-22
SLIDE 22

Relative Numbers ... are BAD

  • vimtutor is bad!
  • Rule of thumb for relative numbers:

DON'T

slide-23
SLIDE 23

Relative Numbers ... are BAD

  • vimtutor is bad!
  • Rule of thumb for relative numbers:

DON'T

(BUT, you'll see it in one of the homeworks... There are actual uses for it.)

slide-24
SLIDE 24

"Arrow Keys"

  • Same principles apply!
slide-25
SLIDE 25

"Arrow Keys"

  • Same principles apply!
  • Myth #2: In Vim, you don't use the

arrow keys, you use the "hjkl" keys.

slide-26
SLIDE 26

"Arrow Keys"

  • Same principles apply!
  • Myth #2: In Vim, you don't use the

arrow keys, you use the "hjkl" keys.

slide-27
SLIDE 27

"Arrow Keys"

  • Same principles apply!
  • Myth #2: In Vim, you don't use the

arrow keys, you use the "hjkl" keys.

slide-28
SLIDE 28

"Arrow Keys"

  • Same principles apply!
  • Myth #2: In Vim, you don't use the

arrow keys, you use the "hjkl" keys.

  • They are no different from the arrow

keys!

slide-29
SLIDE 29

"Arrow Keys"

  • Same principles apply!
  • Myth #2: In Vim, you don't use the arrow

keys, you use the "hjkl" keys.

  • They are no different from the arrow keys!
  • In fact, in this course, we call them "arrow

keys"!

slide-30
SLIDE 30

More about File Explorers...

  • Again, suppose we want to navigate to:

~/vimcourse/demo/lecture1/puppies/chad .txt

  • In reality, you'll probably won't

remember the file location that accurately....

slide-31
SLIDE 31

More about File Explorers...

  • Ideally, we remember:
slide-32
SLIDE 32

More about File Explorers...

  • Ideally, we remember:
  • In reality:
slide-33
SLIDE 33

More about File Explorers...

  • So it's some file (hopefully) named

something memorable...

  • The path to it, on the other hand, may

not be that distinctive...

  • <something

something>/puppies/chad.txt

slide-34
SLIDE 34

More about File Explorers...

  • So it's some file (hopefully) named

something memorable...

  • The path to it, on the other hand, may

not be that distinctive...

  • **/*/puppies/chad.txt
slide-35
SLIDE 35

More about File Explorers...

  • Tab Completion!
  • something about a vimcourse...
slide-36
SLIDE 36

More about File Explorers...

  • Tab Completion!
  • something about a vimcourse...
  • cd
  • vim<Tab>
slide-37
SLIDE 37

More about File Explorers...

  • Tab Completion!
  • something about a vimcourse...
  • cd
  • vim<Tab>
  • OR, *course<Tab> (globbing)
slide-38
SLIDE 38

Key Placement

slide-39
SLIDE 39

Fuzzy is Good!

  • We don't have to even remember

everything to describe it!

  • A fuzzy description would do.
slide-40
SLIDE 40

Fuzzy is Good!

  • We don't have to even remember

everything to describe it!

  • A fuzzy description would do.
  • I want the filename that looks like this:
slide-41
SLIDE 41

Fuzzy is Good!

  • We don't have to even remember

everything to describe it!

  • A fuzzy description would do.
  • I don't have to describe everything in

the path! (foreshadowing 10ish lectures later...)

slide-42
SLIDE 42

Fuzzy is Good!

slide-43
SLIDE 43

Fuzzy is Good!

slide-44
SLIDE 44

Fuzzy is Good!

slide-45
SLIDE 45

Fuzzy is Good!

/cause<CR>

slide-46
SLIDE 46

Fuzzy is Good!

/ca<CR>

Sufficient Description to locate 'cause'!

slide-47
SLIDE 47

.vimrc

  • However, Vim default settings are not

very friendly...

  • To change this, we will have to modify
  • ur .vimrc file.
  • Located in: ~/.vimrc ($HOME/.vimrc)
  • (As of now, I recommend not using Vim

to modify it yet... Copy-Paste from course website)

slide-48
SLIDE 48

.vimrc

slide-49
SLIDE 49

.vimrc

  • Uses a language called "vimscript".
  • (Used to be called VimL).
slide-50
SLIDE 50

.vimrc

  • Uses a language called "vimscript".
  • (Used to be called VimL).
  • Terrible Terrible Language... But for

the purposes of the course, we'll focus

  • n the less terrible parts...
slide-51
SLIDE 51

.vimrc

  • In this course, we'll treat vimscript and

ex commands as the same thing...

  • What this means.. (demo)
slide-52
SLIDE 52

.vimrc

  • Setting options (the skinny)
  • set <option>
  • Ex.
  • set linebreak
  • set hlsearch
slide-53
SLIDE 53

.vimrc

  • Setting options
  • set <option name>
  • <option name> is a boolean option (we'll

see other options in the future)...

  • sets <option name> to True/Yes
slide-54
SLIDE 54

.vimrc

  • Setting (Enabling) options
  • set <option name>
  • Ex.
  • set ignorecase
  • " Ignores case
slide-55
SLIDE 55

.vimrc

  • Setting (Disabling) options
  • set no<option name>
  • (simply prepend 'no' in front of option

name)

  • Ex.
  • set noignorecase
  • " Does not ignore case
slide-56
SLIDE 56

.vimrc

  • Setting (Querying) options
  • set <option name>?
  • (simply append '?' in front of option

name)

  • Ex.
  • set ignorecase?
  • " If it ignores case
slide-57
SLIDE 57

.vimrc

  • Setting (Flipping) options (uncommonly used)
  • set <option name>!
  • (simply append '!' in front of option name)
  • Ex.
  • set ignorecase!
  • " True -> False, False -> True
slide-58
SLIDE 58

.vimrc

  • There is reference for what options do,

within vim itself!

  • Vim Help:
  • :h '<option name>'<CR>
  • Vim Docs.
slide-59
SLIDE 59

.vimrc

slide-60
SLIDE 60

.vimrc

slide-61
SLIDE 61

.vimrc

slide-62
SLIDE 62

.vimrc

  • nocompatible
  • Removes compatibility for vi.
  • (Because we are using Vim, vi defaults

are pretty bad, and it lacks many very important features.)

slide-63
SLIDE 63

.vimrc

  • nohlsearch
  • Disables search higlighting.
  • Thus, it won't show every single match

when we use the /<pattern><CR> command, as it gets very annoying.

slide-64
SLIDE 64

.vimrc

  • incsearch
  • Incremental Search.
  • Visual feedback for locating.
slide-65
SLIDE 65

.vimrc

  • incsearch
  • Incremental Search.
  • Visual feedback for locating.
  • This is how you know that your fuzzy

description is sufficient!

slide-66
SLIDE 66

.vimrc

  • ignorecase
  • Ignores case when searching...
  • Don't want to bother with annoying

caps.

slide-67
SLIDE 67

.vimrc

  • smartcase
  • With 'ignorecase', it also enables casing

when you type caps (hence when you really care about casing).

slide-68
SLIDE 68

Back to our regularly scheduled Vimming...

slide-69
SLIDE 69

Fuzzy is Good!

/ca<CR>

Sufficient Description to locate 'cause'!

slide-70
SLIDE 70

Fuzzy is Good!

slide-71
SLIDE 71

Fuzzy is Good!

  • Hence, simply sufficient details would

be enough to locate!

  • Faster and more convenient.

/ca<CR> Keycount: 4

slide-72
SLIDE 72

Fuzzy is Good!

  • Hence, simply sufficient details would

be enough to locate!

  • Faster and more convenient.

/ca<CR> Keycount: 4

  • Could we run into trouble?
slide-73
SLIDE 73
  • A few loose ends...
  • Ambiguity
  • Ways to resolve it
slide-74
SLIDE 74

Fuzzy is Good?

/cau<CR>

Sufficient Description to locate 'cause'!

slide-75
SLIDE 75

Fuzzy is Good?

  • Note that this is NOT an ambiguous

command!

  • hlsearch.
  • /cau<CR> uniquely identifies 'cause' as

the word you want to edit.

slide-76
SLIDE 76

Fuzzy is Good?

  • But if we are lazy:

/ca<CR>

  • it would be ambiguous! (demo hlsearch)
  • Question: What could go wrong?
slide-77
SLIDE 77

Fuzzy is Good?

slide-78
SLIDE 78

Fuzzy is Good?

slide-79
SLIDE 79

Fuzzy is Good?

goto for loop

slide-80
SLIDE 80

Fuzzy is Good?

goto for loop /for<CR>

What could go wrong?

slide-81
SLIDE 81

Fuzzy is Good?

goto for loop /for<CR>

What could go wrong?

Code has repetition!

slide-82
SLIDE 82

Fuzzy is Good?

From Vim's Perspective

slide-83
SLIDE 83

Fuzzy is Good?

From Vim's Perspective

slide-84
SLIDE 84

Fuzzy is Good?

slide-85
SLIDE 85

Fuzzy is Good?

I want to locate that for loop

slide-86
SLIDE 86

Fuzzy is Good?

Which for loop????

slide-87
SLIDE 87

Fuzzy is Good?

Which for loop????

slide-88
SLIDE 88

Fuzzy is Good?

Which for loop????

Ambiguity!!

slide-89
SLIDE 89

Fuzzy is Good?

Which for loop????

Ambiguity!!

There is more than one way to interpret a description.

slide-90
SLIDE 90

Fuzzy is Good?

Which for loop????

Ambiguity!!

There is more than one way to interpret a description.

slide-91
SLIDE 91

Conventions (from last time)

  • We have three emphasis on commands

for this course:

  • 1. Clarity (no ambiguity!)
  • 2. Ergonomics
  • 3. Keystrokes
slide-92
SLIDE 92

Conventions (from last time)

  • We have three emphasis on commands

for this course:

  • 1. Clarity (no ambiguity!)
  • 2. Ergonomics
  • 3. Keystrokes

So how do we deal with it?

slide-93
SLIDE 93
  • A few loose ends...
  • Ambiguity
  • Ways to resolve it
slide-94
SLIDE 94

Resolving Ambiguity

(In decreasing use case):

  • 1. Cheating
  • 2. Distinctive features
  • 3. Relative jumps
slide-95
SLIDE 95

Cheating

Well, it's one of them...

slide-96
SLIDE 96

Cheating

Well, it's one of them...

Keep trying until you get to it!!

slide-97
SLIDE 97

Cheating

Emphasizing the idea that: "Well, my description is somewhat clear... So there wouldn't be that much ambiguity!"

slide-98
SLIDE 98

Cheating

  • By default, /<pattern><CR> command

finds the next closest match.

slide-99
SLIDE 99

Cheating

  • By default, /<pattern><CR> command

finds the next closest match. n

  • Finds the next match

N

  • Finds the previous match
slide-100
SLIDE 100

Cheating

  • Note: most of the time, we cheat

because Vim matches things that we don't expect/know. (or we are unfamilar with the language)

  • Demo (trouble.cpp)
slide-101
SLIDE 101

Cheating

  • Note: most of the time, we cheat

because Vim matches things that we don't expect/know.

  • Demo (trouble.cpp)
  • For beginners, try to match distinctive

features instead, until writing descriptions (in some language) become natural....

slide-102
SLIDE 102

Distinctive features

Okay, these are for loops

What makes it different?

slide-103
SLIDE 103

Distinctive features

slide-104
SLIDE 104

Distinctive features

We defined the variable 'j' as the loop variable!

slide-105
SLIDE 105

Distinctive features

We defined the variable 'j' as the loop variable! /int j<CR>

slide-106
SLIDE 106

Distinctive features

  • This is the recommended way to resolve

ambiguity!

  • The reason for this is that ambiguity

arises from imprecise or too-general descriptions.

slide-107
SLIDE 107

Distinctive features

  • This is the recommended way to resolve

ambiguity!

  • The reason for this is that ambiguity

arises from imprecise or too-general descriptions.

  • (There is probably more than one for

loop in a program!)

slide-108
SLIDE 108

Distinctive features

  • However, it is less commonly used than

cheating... (why?)

slide-109
SLIDE 109

Distinctive features

  • However, it is less commonly used than

cheating... (why?)

  • Reason: you need to cheat when you

screw up... sometimes you screw up, and it's not worth the time relocating!

slide-110
SLIDE 110

Distinctive features

  • However, it is less commonly used than

cheating... (why?)

  • Reason: you need to cheat when you

screw up... sometimes you screw up, and it's not worth the time relocating!

slide-111
SLIDE 111

Next Time

  • More ways to resolve ambiguity!
  • Modal Editing and its principles and policies.
  • Unix and Conflicting Principles.
  • Homework 1 is out! Due next week.
  • Think about ambiguity! It's probably the most

annoying part of editing.

  • Practice coming up with clear descriptions.
  • Review setting boolean options in vimscript.

We won't cover it again due to time constraints.