SLIDE 1
Motions and Visual Mode Part II Core Commands The World as Everyone - - PowerPoint PPT Presentation
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 2
SLIDE 3
The World as Everyone Else Sees it
SLIDE 4
The World as Everyone Else Sees it
SLIDE 5
The World as Everyone Else Sees it
Other editors see text as a string of characters
SLIDE 6
The World as Everyone Else Sees it
Other editors see text as a string of characters
SLIDE 7
The World as Everyone Else Sees it
Other "editors" see text as a string of characters
SLIDE 8
The World as Vim Sees It
SLIDE 9
The World as Vim Sees It
SLIDE 10
The World as Vim Sees It
SLIDE 11
The World as Vim Sees It
Vim (and you) see text as a sequence of objects.
SLIDE 12
- Editing Commands
- Text Objects
- Motions
- Visual Mode
SLIDE 13
- Editing Commands
- Text Objects
- Motions
- Visual Mode
SLIDE 14
If you still recall....
- Editing...
SLIDE 15
If you still recall....
- Editing...
– is the process of making changes to text.
- Hence, we have editing commands.
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
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
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
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
Other editing commands
y<object>
- Yanks <object> under current cursor to
register. =<object>
- Reformats code for <objects> under
current cursor.
SLIDE 21
Other editing commands
gu/gU<object>
- Decapitalize/Capitalize <object> under
current cursor. >/<<object>
- Indent/Dedent for <objects> under
current cursor.
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
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
Today: Editing Commands
c<object>
- Change <object> under current cursor
to... (goes to insert mode). d<object>
- Delete <object> under cursor.
SLIDE 25
- Editing Commands
- Text Objects
- Motions
- Visual Mode
SLIDE 26
Text Objects
- Vim sees the world as inherently
composed of objects.
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
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
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
Fundamental Objects: #1 word and WORD
SLIDE 31
Fundamental Objects: #1 word and WORD
- Probably one of the most confusing
parts of Vim.
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
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
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
Fundamental Objects: #1 word and WORD
SLIDE 36
Fundamental Objects: #1 word and WORD
l_list is a word! (variable name)
SLIDE 37
Fundamental Objects: #1 word and WORD
NULL is a word! (variable value)
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
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
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
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
Fundamental Objects: #1 word and WORD
l_list->next is a WORD!
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
Fundamental Objects #1 word and WORD
- So how to express a word or WORD
- bject?
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
Two flavors of text objects
- Two flavors:
1.i - also known as "inside" 2.a - also known as "around"
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
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
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
Fundamental Objects #1 word and WORD
- iw - inside word.
- aw - around word.
- iW - inside WORD.
- aW - around WORD.
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
Fundamental Objects #1 word and WORD
- iw - inside word.
- aw - around word.
- iW - inside WORD.
- aW - around WORD.
- Why do we care?
SLIDE 53
Fundamental Objects #1 word and WORD
- Compare:
diw
SLIDE 54
Fundamental Objects #1 word and WORD
- Compare:
daw
SLIDE 55
Fundamental Objects #1 word and WORD
- Compare:
ciwlot<Esc>
SLIDE 56
Fundamental Objects #1 word and WORD
- Compare:
cawlot<Esc>
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
Fundamental Objects: #1 word and WORD
- Is this an issue?
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
Fundamental Objects #1 word and WORD
- Hence, make this your muscle memory!
da ci
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
Fundamental Objects #2 enclosed objects
- "strings"
- (cons (1 nil))
- {document}
- etc...
- Are wrapped with matching characters:
"", (), [], {}, even html tags!
SLIDE 63
Fundamental Objects #2 enclosed objects
- General syntax for <enclosed object>:
- i<closing delimiter>
- a<closing delimiter>
SLIDE 64
Fundamental Objects #2 enclosed objects
- Example:
SLIDE 65
Fundamental Objects #2 enclosed objects
- Example:
di)
SLIDE 66
Fundamental Objects #2 enclosed objects
- Example:
SLIDE 67
Fundamental Objects #2 enclosed objects
- Example:
ca" NULL<Esc>
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
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
Fundamental Objects #2 enclosed objects
- Available enclosed objects (preceded
with a or i):
- ) -> ()
- } -> {}
- " -> ""
- ] -> []
- t -> html tags
- and many many many more...
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
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
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
Fundamental "Objects" #3 lines
- ... are strictly speaking not objects
SLIDE 75
Fundamental "Objects" #3 lines
- ... are strictly
speaking not objects
– but screw it.
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
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
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
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
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
Fundamental Objects
- But, there are issues with fundamental
text objects:
- Although this happens very very rarely
when editing code, it still happens...
SLIDE 82
Issues with text objects...
SLIDE 83
Issues with text objects...
Hm... well..
SLIDE 84
Issues with text objects...
Hm... well..
"very wordy and long " is not a text
- bject!
SLIDE 85
- Editing Commands
- Text Objects
- Motions
- Visual Mode
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
Motions
- From vimtutor, you may have seen these
motions: w,e,b,ge,gj,gk,j,k,h,l,$,^,0,g$
- etc...
SLIDE 88
Motions
SLIDE 89
Motions
SLIDE 90
A Much Better Game (if you want to do it the hard way)
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
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
Motions
- At the end of the day, you will only ever
need and use 3 motions:
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
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 !