How Vim user grows - Lv1
- Lv0. who doesn't know about Vim
-
Lv1. who knows basic usage of Vim
- Lv2. who knows Visual mode
- Lv3. who knows various motions
- Lv4. who doesn't use Visual mode
i, a, <Esc>, x, dd, :e, :w, :q, ...
Lv3. Various motions
- Lv0. who doesn't know about Vim
- Lv1. who knows basic usage of Vim
- Lv2. who knows Visual mode
-
Lv3. who knows various motions
- Lv4. who doesn't use Visual mode
What is "motion"?
Vim has various motions.
motion is a command to move the cursor.
Example of motions:
- w - next word
- } - next paragraph
- % - matched parenthes - ([{}])
- `] - the end of the last changed text
- ...
See also :help Q_lr
Combination
Visual mode + various motions =
- Effective selection
- Less typing
- Fast editing
But...
Recall that Visual mode is:
- Interactive
- Visualized
- Easy to understand
- Similar to drag-and-type editing in ordinary editors
... Similar to ordinary editors?
So why don't you use them instead of Vim, the strange editor?
You still have a lot of work to do.
Lv4. Operators
- Lv0. who doesn't know about Vim
- Lv1. who knows basic usage of Vim
- Lv2. who knows Visual mode
- Lv3. who knows various motions
-
Lv4. who doesn't use Visual mode
What is "operator"?
Recall the last description of Visual mode:
- Start Visual mode, v
- Select text by moving the cursor, hhjjkkll
- Do something on it. d
something is called an operator.
operator is a command to edit a portion of text.
Basic usage
operator edits on the selected text in Visual mode. But
operator can also be used in Normal mode. If so,
operator takes a motion to determine text to be edited.
Example of usage:
- dw - delete text from the cursor to the next word
- y} - yank text from the cursor to the next paragraph
- >% - indent text between a pair of parenthes ([{}])
- ...
Orthogonality
operators and motions are orthogonal, so:
- Once you learn a new motion,
- you can do edit on various portions of text with operators you learned.
- Once you learn a new operator,
- you can do new kind of edit with various motions you learned.
In other editors, you have to learn many comibations of them. kill-line, kill-sentence, kill-pragraph, kill-sexp, ... too-many-to-learn
inclusive/exclusive (advanced)
{operator}{motion} is similar to v{motion}{operator}
But there are 2 types of motions:
-
inclusive motion
- works as same as v{motion}{operator}
-
exclusive motion
- the character at the destination of motion is not affected
The differences are natural - You don't need to be conscious of the types of motions.
*wise (advanced)
There are 3 types of the effects of operators:
-
characterwise
-
linewise
-
blockwise
*wise can be varied by operator and motion
- dw - characterwise - delete a word
- >j - linewise - indent 2 lines
- dj - linewise - delete 2 lines
Forcing *wise (advanced)
You can force *wise with the following keys:
- v - characterwise
- V - linewise
- <C-v> - blockwise
Usage: {operator}{*wise-specifier}{motion}
- dvj - characterwise
- dVj - linewise
- d<C-v>j - blockwise
Summary
The merits of operators are:
- Less typing - no need to type a key to start Visual mode
- Intuitive - do something on it
- Orthogonality - various combinations to edit various portions
In most cases, you can specify some portion of text by a single motion, so you don't need to use Visual mode often.
Lv5. Text objects
- Lv0. who doesn't know about Vim
- Lv1. who knows basic usage of Vim
- Lv2. who knows Visual mode
- Lv3. who knows various motions
- Lv4. who doesn't use Visual mode
-
Lv5. who knows text objects
Basic usage
Text object is a special kind of motion
Text object can be used in Visual mode or Operator-pending mode (i.e., after an operator)
Text object selects a kind of object, for example:
- aw - selects a word
- as - selects a sentence
- ap - selects a paragraph
- ...
See also :help text-objects
Comparison
Some text objects are similar to some motions, but
You don't need to adjust the cursor
- dw - delete from the cursor to the next word (so, to delete a word, you have to move the cursor at the beginnig of the word)
- daw - delete a word regardless of the cursor position
You'll be able to edit more effectively with text objects
Lv6. New text objects
- Lv0. who doesn't know about Vim
- Lv1. who knows basic usage of Vim
- Lv2. who knows Visual mode
- Lv3. who knows various motions
- Lv4. who doesn't use Visual mode
- Lv5. who knows text objects
-
Lv6. who writes new text objects
Once you master text objects...
You'll want to write your own text objects
Examples:
- selects a function,
- selects a fold,
- selects a date and/or time,
- ...
But it's too hard for usual Vimmers to write text objects in valid and right way
How to write your own text objects
textobj-user - a library to write new text objects - http://www.vim.org/scripts/script.php?script_id=2100
call textobj#user#plugin('datetime', {
\ 'full': {'select': ['adf', 'idf'],
\ '*pattern*': s:REGEXP_FULL},
\ })
- Define simple text objects by a regular expression (you can also use a function)
- Set up right key mappings for text objects
- Be happy because you don't need to know the dark side of Vim script