SLIDE 1
Lecture 8: Visual Mode, /<CR>, Drafting Commands, Metaediting
Part II: The Core Commands
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 Last Time
- Text Objects!
- Motions....
SLIDE 4 Last Time
- Text Objects!
- Motions....
- Today: Visual Mode!
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 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 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
– Are straightforward, and uninteresting.
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
- Visual Mode
- 1 Character Editing Commands and
Drafting Commands
- /<pattern><CR> and Metaediting.
SLIDE 10
- Visual Mode
- 1 Character Editing Commands and
Drafting Commands
- /<pattern><CR> and Metaediting.
SLIDE 11
Issues with Motions
SLIDE 12
Issues with Motions
Task: Change to "in C, we can use ``variableinstance.function'' to access its member variables."
SLIDE 13
Issues with Motions
Task: Change to "in C, we can use ``variableinstance.function'' to access its member variables." Well?
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
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 Issues with Motions
c<object>
- Command deals with only ONE object or
ONE motion!
SLIDE 17 Issues with Motions
c<object>
- Command deals with only ONE object or
ONE motion!
- How do we deal with complicated
descriptions? (relative, cheating, etc...)
SLIDE 18
In the good ol' days of vi...
SLIDE 19
In the good ol' days of vi...
SLIDE 20
In the good ol' days of vi...
Can't do that...
SLIDE 21 In the good ol' days of vi...
Can't do that...
(workaround: marks, regex tricks, more accurate regex matching)
SLIDE 22
In the good ol' days of vi...
SLIDE 23
Today: Vim
Editing Commands follow the format: <command><object/motion>
SLIDE 24 Today: Vim
Editing Commands follow the format: <command><object/motion>
Issue: how do we know that
- ur description has ended?
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 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 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 Visual Mode: The Idea
- Instead of structuring our commands
like this: <command><object/motion>
<object/motion><command>
SLIDE 29 Visual Mode: The Idea
- Instead of structuring our commands
like this: <command><object/motion>
<object/motion><command>
- How do we know that our motion has
terminated?
SLIDE 30 Visual Mode: The Idea
- Instead of structuring our commands like
this: <command><object/motion>
<object/motion><command>
- How do we know that our motion has
terminated? When we reach an edit command!
SLIDE 31 Visual Mode
- Three kinds, with radically different
use cases:
– Visual Mode (today) – Visual Line Mode – Visual Block Mode
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 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 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 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 Visual Mode
- Commands:
- gv - reselect previous selection
– VERY useful for redoing typos
– where and how is previous selection stored? – what happens if we perform edits?
SLIDE 37 Visual Mode
- Commands:
- gv - reselect previous selection
– VERY useful for redoing typos
– where and how is previous selection stored? – what happens if we perform edits?
- Will be addressed in HW 3!
SLIDE 38
- Visual Mode
- 1 Character Editing Commands and
Drafting Commands
- /<pattern><CR> and Metaediting.
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 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 Drafting Commands
- Drafting: appending/inserting huge
chuncks of text.
SLIDE 42 Drafting Commands
- Drafting: appending/inserting huge
chuncks of text.
- Not much involved, remember a few
very common use case patterns:
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 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 Speaking of Which...
- The /<pattern><CR> command...
- ... is THE MOST IMPORTANT command
in Vim!
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
- Visual Mode
- 1 Character Editing Commands and
Drafting Commands
- /<pattern><CR> and Metaediting.
SLIDE 48 /<pattern><CR> ... again!
- ... is very very versatile!
SLIDE 49 /<pattern><CR> ... again!
- ... is very very versatile!
- But first, what's a <pattern>?
SLIDE 50 vimregex
- a <pattern> is a form of vim regular
expression.
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 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 vimregex
- 4 flavors:
- verynomagic
- nomagic
- magic
- verymagic
- ... Each with their own rules!
- ... and extremely questionable reasons
for having 4 different standards...
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 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:
SLIDE 56 vimregex
– 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 58 vimregex
- So what can we do?
- Useful facts:
- . - matches any character.
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 vimregex
- So what can we do?
- Useful facts:
- . - matches any character.
- * - matches any number of previous
group.
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 Back to /<pattern><CR>
- Like many commands in Vim, it can
either be a forward direction or a reverse direction.
SLIDE 63 Back to /<pattern><CR>
- Like many commands in Vim, it can
either be a forward direction or a reverse direction. /<pattern><CR>
?<pattern><CR>
SLIDE 64 Is there more to /pattern<CR>?
- Yes! But first, the interface...
SLIDE 65 Command Line Interface
- The /<pattern><CR> command has
"nearly" the exact same interface as typing out ex commands.
– One Line – Insert Mode Commands – History Recall
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 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 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 Command Line Interface
- Commands:
- In normal mode:
- * - searches word under cursor
- # - reverse searches word under cursor
- Saves going into command line
altogether!
SLIDE 70 Command Line Interface
SLIDE 71 Command Line Interface
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 Command Line Interface
- First, a detour:
- Where, and how, does Vim stores its
search history?
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 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 Command Line Interface
- First, a detour:
- Where, and how, does Vim stores its
search history?
– .viminfo file – loaded into a specialized memory segment.
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 Simple Case: flags
- A more general syntax of search:
/<pattern>/<flags><CR>
- If <pattern> is left blank, automatically
repeats previous search!
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 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 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.
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.
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!