Motions and Visual Mode Part II Core Commands The World as Everyone - - PowerPoint PPT Presentation

motions and visual mode
SMART_READER_LITE
LIVE PREVIEW

Motions and Visual Mode Part II Core Commands The World as Everyone - - PowerPoint PPT Presentation

Lecture 7: Text Objects, Motions and Visual Mode Part II Core Commands The World as Everyone Else Sees it The World as Everyone Else Sees it The World as Everyone Else Sees it The World as Everyone Else Sees it Other editors see text as a


slide-1
SLIDE 1

Lecture 7: Text Objects, Motions and Visual Mode

Part II Core Commands

slide-2
SLIDE 2

The World as Everyone Else Sees it

slide-3
SLIDE 3

The World as Everyone Else Sees it

slide-4
SLIDE 4

The World as Everyone Else Sees it

slide-5
SLIDE 5

The World as Everyone Else Sees it

Other editors see text as a string of characters

slide-6
SLIDE 6

The World as Everyone Else Sees it

Other editors see text as a string of characters

slide-7
SLIDE 7

The World as Everyone Else Sees it

Other "editors" see text as a string of characters

slide-8
SLIDE 8

The World as Vim Sees It

slide-9
SLIDE 9

The World as Vim Sees It

slide-10
SLIDE 10

The World as Vim Sees It

slide-11
SLIDE 11

The World as Vim Sees It

Vim (and you) see text as a sequence of objects.

slide-12
SLIDE 12
  • Editing Commands
  • Text Objects
  • Motions
  • Visual Mode
slide-13
SLIDE 13
  • Editing Commands
  • Text Objects
  • Motions
  • Visual Mode
slide-14
SLIDE 14

If you still recall....

  • Editing...
slide-15
SLIDE 15

If you still recall....

  • Editing...

– is the process of making changes to text.

  • Hence, we have editing commands.
slide-16
SLIDE 16

If you still recall....

  • Editing...

– is the process of making changes to text.

  • Hence, we have editing commands.
  • Focus of today: the c and d commands

(and the like)!

slide-17
SLIDE 17

If you still recall....

  • Editing...

– is the process of making changes to text.

  • Hence, we have editing commands.
  • Focus of today: the c and d commands (and

the like)!

  • Focus of next lecture: the i and a commands

for inserting/drafting, along with a rehash of the /<pattern><CR>, and f<char> commands.

slide-18
SLIDE 18

Editing Commands

c<object>

  • Change <object> under current cursor

to... (goes to insert mode). d<object>

  • Delete <object> under cursor.
  • Main focus of today!
slide-19
SLIDE 19

Other editing commands

  • Are still important.
  • Follow pretty much the same

syntax/principles.

  • Every editing command can be

repeated with the dot command!

slide-20
SLIDE 20

Other editing commands

y<object>

  • Yanks <object> under current cursor to

register. =<object>

  • Reformats code for <objects> under

current cursor.

slide-21
SLIDE 21

Other editing commands

gu/gU<object>

  • Decapitalize/Capitalize <object> under

current cursor. >/<<object>

  • Indent/Dedent for <objects> under

current cursor.

slide-22
SLIDE 22

Other editing commands (from plugins)

gc<object>

  • Comment <object> under current cursor.
  • From vim-commentary by Tim Pope.

cs/ds<object>

  • Change/Delete surrounding for

<objects> under current cursor.

  • From vim-surround by Tim Pope.
slide-23
SLIDE 23

And then there are others

  • Other commands that do not follow this

format:

  • Single Character Commands.
  • Special Purpose Commands.
  • And others that don't even categorize

to these...

  • Not the focus of today!
slide-24
SLIDE 24

Today: Editing Commands

c<object>

  • Change <object> under current cursor

to... (goes to insert mode). d<object>

  • Delete <object> under cursor.
slide-25
SLIDE 25
  • Editing Commands
  • Text Objects
  • Motions
  • Visual Mode
slide-26
SLIDE 26

Text Objects

  • Vim sees the world as inherently

composed of objects.

slide-27
SLIDE 27

Text Objects

  • Vim sees the world as inherently

composed of objects.

  • They are:

– The most important part of Vim's commands that you NEED TO KNOW on top

  • f your head.
slide-28
SLIDE 28

Text Objects

  • Vim sees the world as inherently

composed of objects.

  • They are:

– The most important part of Vim's commands that you NEED TO KNOW on top

  • f your head.

– Most used part of Vim's commands.

slide-29
SLIDE 29

Text Objects

  • Vim sees the world as inherently

composed of objects.

  • They are:

– The most important part of Vim's commands that you NEED TO KNOW on top

  • f your head.

– Most used part of Vim's commands. – You should be using them almost all of the time, unless you are not describing an

  • bject.
slide-30
SLIDE 30

Fundamental Objects: #1 word and WORD

slide-31
SLIDE 31

Fundamental Objects: #1 word and WORD

  • Probably one of the most confusing

parts of Vim.

slide-32
SLIDE 32

Fundamental Objects: #1 word and WORD

  • Probably one of the most confusing

parts of Vim.

  • A word is... an almost C-Style variable

name.

slide-33
SLIDE 33

Fundamental Objects: #1 word and WORD

  • Probably one of the most confusing

parts of Vim.

  • A word is... an almost C-style variable

name.

– A consecutive string of characters containing only of letters, digits and underscores.

slide-34
SLIDE 34

Fundamental Objects: #1 word and WORD

  • Probably one of the most confusing parts of

Vim.

  • A word is... an almost C-style variable name.

– A consecutive string of characters containing only

  • f letters, digits and underscores.

– Can be modified through the 'iskeyword' option, if you are working with a language that has slightly different specification of variable names! – They are meant to indicate variable name and values!

slide-35
SLIDE 35

Fundamental Objects: #1 word and WORD

slide-36
SLIDE 36

Fundamental Objects: #1 word and WORD

l_list is a word! (variable name)

slide-37
SLIDE 37

Fundamental Objects: #1 word and WORD

NULL is a word! (variable value)

slide-38
SLIDE 38

Fundamental Objects: #1 word and WORD

  • Why word?
  • Do the following: change variable name,

function name, change values, fix variable/function name typos...

slide-39
SLIDE 39

Fundamental Objects: #1 word and WORD

  • Why word?
  • Do the following: change variable name,

function name, change values, fix variable/function name typos...

  • Without having to retype everything in

that WORD! (convenience)

slide-40
SLIDE 40

Fundamental Objects: #1 word and WORD

  • Probably one of the most confusing

parts of Vim.

  • A word is... an almost C-Style variable

name.

  • A WORD is... what unix standard thinks

is a word (lol).

slide-41
SLIDE 41

Fundamental Objects: #1 word and WORD

  • Probably one of the most confusing

parts of Vim.

  • A word is... an almost C-Style variable

name.

  • A WORD is... what unix standard thinks

is a word (lol).

– A sequence of characters delimited by any kind of whitespace characters.

slide-42
SLIDE 42

Fundamental Objects: #1 word and WORD

l_list->next is a WORD!

slide-43
SLIDE 43

Fundamental Objects: #1 word and WORD

  • Why WORD?
  • Do the following: making phrasal

changes such as email addresses (with @'s and .'s), fixing completely wrong variables or structures...

  • Entirely different purpose.
slide-44
SLIDE 44

Fundamental Objects #1 word and WORD

  • So how to express a word or WORD
  • bject?
slide-45
SLIDE 45

Fundamental Objects #1 word and WORD

  • So how to express a word or WORD
  • bject?
  • Before that:
  • Text objects (generally) come in two

different flavors!

slide-46
SLIDE 46

Two flavors of text objects

  • Two flavors:

1.i - also known as "inside" 2.a - also known as "around"

slide-47
SLIDE 47

Fundamental Objects #1 word and WORD

  • What does it mean to be "inside" a

word/WORD, or "around" a word/WORD?

slide-48
SLIDE 48

Fundamental Objects #1 word and WORD

  • What does it mean to be "inside" a

word/WORD, or "around" a word/WORD?

"inside" a word means the word itself. iw

slide-49
SLIDE 49

Fundamental Objects #1 word and WORD

  • What does it mean to be "inside" a

word/WORD, or "around" a word/WORD?

"around" a word means the word itself + a succeding space. aw

slide-50
SLIDE 50

Fundamental Objects #1 word and WORD

  • iw - inside word.
  • aw - around word.
  • iW - inside WORD.
  • aW - around WORD.
slide-51
SLIDE 51

Fundamental Objects #1 word and WORD

  • iw - inside word.
  • aw - around word.
  • iW - inside WORD.
  • aW - around WORD.
  • Why do we care?
slide-52
SLIDE 52

Fundamental Objects #1 word and WORD

  • iw - inside word.
  • aw - around word.
  • iW - inside WORD.
  • aW - around WORD.
  • Why do we care?
slide-53
SLIDE 53

Fundamental Objects #1 word and WORD

  • Compare:

diw

slide-54
SLIDE 54

Fundamental Objects #1 word and WORD

  • Compare:

daw

slide-55
SLIDE 55

Fundamental Objects #1 word and WORD

  • Compare:

ciwlot<Esc>

slide-56
SLIDE 56

Fundamental Objects #1 word and WORD

  • Compare:

cawlot<Esc>

slide-57
SLIDE 57

Fundamental Objects: #1 word and WORD

  • Vim allow you to express precisely what

you want to do!

  • When you are deleting a word, do you

want to preserve the space after the word? Or do you want to delete it?

  • When you are changing a word, do you

want to change the space after the word as well? Or are you okay with leaving it as-is?

slide-58
SLIDE 58

Fundamental Objects: #1 word and WORD

  • Is this an issue?
slide-59
SLIDE 59

Fundamental Objects: #1 word and WORD

  • Is this an issue?

– If you are editing prose, generally NO!

  • Too much thinking?
  • When you are deleting a word, you probably

want to delete the whitespace after it,

  • therwise there might be duplicate spaces.
  • When you are changing a word, you probably

don't need to change the space after it.

slide-60
SLIDE 60

Fundamental Objects #1 word and WORD

  • Hence, make this your muscle memory!

da ci

slide-61
SLIDE 61

Fundamental Objects #1 word and WORD

  • Hence, make this your muscle memory!

da ci

  • Note: depending on language, may not

apply to programming!

  • Figure out your language style

conventions, and make it a habit!

slide-62
SLIDE 62

Fundamental Objects #2 enclosed objects

  • "strings"
  • (cons (1 nil))
  • {document}
  • etc...
  • Are wrapped with matching characters:

"", (), [], {}, even html tags!

slide-63
SLIDE 63

Fundamental Objects #2 enclosed objects

  • General syntax for <enclosed object>:
  • i<closing delimiter>
  • a<closing delimiter>
slide-64
SLIDE 64

Fundamental Objects #2 enclosed objects

  • Example:
slide-65
SLIDE 65

Fundamental Objects #2 enclosed objects

  • Example:

di)

slide-66
SLIDE 66

Fundamental Objects #2 enclosed objects

  • Example:
slide-67
SLIDE 67

Fundamental Objects #2 enclosed objects

  • Example:

ca" NULL<Esc>

slide-68
SLIDE 68

Fundamental Objects #2 enclosed objects

  • Rule of thumb:
  • "inside" an enclosed object: what the

enclose object contains (characters inside the string specifier, content inside a html tag, etc)

  • "around" an enclosed object: what the

enclosed object contains, along with the delimiters themselves, with potentially whitespace around it as well.

slide-69
SLIDE 69

Fundamental Objects #2 enclosed objects

  • Vim provide fine grained control over

exactly what you wish to edit!

  • Usually when it comes to code, self

evident whether to use "around" or "inside".

slide-70
SLIDE 70

Fundamental Objects #2 enclosed objects

  • Available enclosed objects (preceded

with a or i):

  • ) -> ()
  • } -> {}
  • " -> ""
  • ] -> []
  • t -> html tags
  • and many many many more...
slide-71
SLIDE 71

Fundamental Objects #2 enclosed objects

  • Available enclosed objects (preceded with a
  • r i):
  • ) -> ()
  • } -> {}
  • " -> ""
  • ] -> []
  • t -> html tags
  • and many many many more...
  • Want even more for your language?
  • vim-textobj-user:

https://github.com/kana/vim-textobj-user

slide-72
SLIDE 72

Fundamental Objects #2 enclosed objects

  • Available enclosed objects (preceded with a
  • r i):
  • ) -> ()
  • } -> {}
  • " -> ""
  • ] -> []
  • t -> html tags
  • and many many many more...
  • Want even more for your language?
  • vim-textobj-user:

https://github.com/kana/vim-textobj-user

slide-73
SLIDE 73

Fundamental Objects #2 enclosed objects

  • Available enclosed objects (preceded with a
  • r i):
  • ) -> ()
  • } -> {}
  • " -> ""
  • ] -> []
  • t -> html tags
  • and many many many more...
  • Want even more for your language?
  • vim-textobj-user:

https://github.com/kana/vim-textobj-user

slide-74
SLIDE 74

Fundamental "Objects" #3 lines

  • ... are strictly speaking not objects
slide-75
SLIDE 75

Fundamental "Objects" #3 lines

  • ... are strictly

speaking not objects

– but screw it.

slide-76
SLIDE 76

Fundamental "Objects" #3 lines

  • ... are strictly speaking not objects

– but screw it.

  • Line objects don't quite follow the

syntax for regular objects (because they're not)...

slide-77
SLIDE 77

Fundamental "Objects" #3 lines

  • Two main operations on a line: (and

hence two main line objects)

  • 1. The entire line on the cursor.
  • 2. Everything after the current

cursor(inclusive) on that line.

slide-78
SLIDE 78

Fundamental "Objects" #3 lines

  • 1. The entire line on the cursor.
  • Has syntax of repeating the command:
  • dd - delete current line
  • cc - change current line
  • yy - yank current line
  • gcc - comment current line (vim-

commentary)

  • etc...
slide-79
SLIDE 79

Fundamental "Objects" #3 lines

  • 1. Everything on the line after the cursor.
  • is a <Shift> prefixed command:
  • D - delete everything after cursor on current line
  • C - change everything after cursor on current line
  • Exception: Y - yank current line

– stupid backwards compatibility reasons with vi – can be remapped:

  • Put this in your vimrc: nnoremap Y y$
  • etc...
slide-80
SLIDE 80

Fundamental Objects

  • These three categories of text objects

are extremely useful!

  • ALWAYS prefer using text objects
  • ver motions:

– they are precise: you always know exactly what they do. – they are prevalent: extremely good odds that you need text objects instead of motions.

slide-81
SLIDE 81

Fundamental Objects

  • But, there are issues with fundamental

text objects:

  • Although this happens very very rarely

when editing code, it still happens...

slide-82
SLIDE 82

Issues with text objects...

slide-83
SLIDE 83

Issues with text objects...

Hm... well..

slide-84
SLIDE 84

Issues with text objects...

Hm... well..

"very wordy and long " is not a text

  • bject!
slide-85
SLIDE 85
  • Editing Commands
  • Text Objects
  • Motions
  • Visual Mode
slide-86
SLIDE 86

Motions

  • Very occasionally (in a coding context),

you may need to express a description

  • f a phrase....
  • There's very little to talk about when it

comes to motions...

slide-87
SLIDE 87

Motions

  • From vimtutor, you may have seen these

motions: w,e,b,ge,gj,gk,j,k,h,l,$,^,0,g$

  • etc...
slide-88
SLIDE 88

Motions

slide-89
SLIDE 89

Motions

slide-90
SLIDE 90

A Much Better Game (if you want to do it the hard way)

slide-91
SLIDE 91

A Much Better Game (if you want to do it the hard way)

Even trickier! (old man rips you off, monsters give you ruppees)

slide-92
SLIDE 92

Motions

  • From vimtutor, you may have seen these

motions: w,e,b,ge,gj,gk,j,k,h,l,$,^,0,g$

  • etc...
  • They are ridiculously situational, very

convoluted, and hence utterly useless.

slide-93
SLIDE 93

Motions

  • At the end of the day, you will only ever

need and use 3 motions:

slide-94
SLIDE 94

Motions

  • At the end of the day, you will only ever need

and use 3 motions:

  • /<pattern><CR> and variants.
  • f<char> and variants.
  • t<char> and variants.
  • Only difference between t and f, is that f is

inclusive (think "around"), while t is exclusive (think "before").

slide-95
SLIDE 95

Next Time...

  • What happens when motions go wrong?

(surprisingly often, ambiguity!)

  • Not much said about motions, since its all

practice.

  • ALWAYS prefer text objects over motions.
  • Practice muscle memory! daci !