# textobj用法
[toc]
## h2
:::success
The operators that can be used are:
~ switch case |v_~|
d delete |v_d|
c change (4) |v_c|
y yank |v_y|
> shift right (4) |v_>|
< shift left (4) |v_<|
! filter through external command (1) |v_!|
= filter through 'equalprg' option command (1) |v_=|
gq format lines to 'textwidth' length (1) |v_gq|
The objects that can be used are:
aw a word (with white space) |v_aw|
iw inner word |v_iw|
aW a WORD (with white space) |v_aW|
iW inner WORD |v_iW|
as a sentence (with white space) |v_as|
is inner sentence |v_is|
ap a paragraph (with white space) |v_ap|
ip inner paragraph |v_ip|
ab a () block (with parentheses) |v_ab|
ib inner () block |v_ib|
aB a {} block (with braces) |v_aB|
iB inner {} block |v_iB|
at a <tag> </tag> block (with tags) |v_at|
it inner <tag> </tag> block |v_it|
a< a <> block (with <>) |v_a<|
i< inner <> block |v_i<|
a[ a [] block (with []) |v_a[|
i[ inner [] block |v_i[|
a" a double quoted string (with quotes) |v_aquote|
i" inner double quoted string |v_iquote|
a' a single quoted string (with quotes) |v_a'|
i' inner simple quoted string |v_i'|
a` a string in backticks (with backticks) |v_a`|
i` inner string in backticks |v_i`|
Additionally the following commands can be used:
: start Ex command for highlighted lines (1) |v_:|
r change (4) |v_r|
s change |v_s|
C change (2)(4) |v_C|
S change (2) |v_S|
R change (2) |v_R|
x delete |v_x|
D delete (3) |v_D|
X delete (2) |v_X|
Y yank (2) |v_Y|
p put |v_p|
P put without overwriting registers |v_P|
J join (1) |v_J|
U make uppercase |v_U|
u make lowercase |v_u|
^] find tag |v_CTRL-]|
I block insert |v_b_I|
A block append |v_b_A|
:::
## Vim text-objects for Python development
by David Winterbottom on Thursday, 13 June 2019
## 什麼是textobj
:::info
Text objects, as in the iw from ciw (“change inner word”), form an important part of your Vim mentalese1. This post details those that I find most useful for Python and Django development.
For brevity, the leading a (mnemonic: “a"n) or i (mnemonic: “inner”), that you combine with the following commands to form the full text-object, are omitted.
From core Vim:
w → a word
W → a WORD
t → a HTML/XML tag
s → a sentence
p → a paragraph
From the (highly recommended) wellle/targets.vim plugin:
, . ; : + - = ~ _ * # / | \ & $ → an area of text delimited by the given character (super useful).
a → a function argument.
Furthermore wellle/targets.vim overrides the built-in text objects to seek to the next occurence of the text-object if the cursor isn’t already within one. This is useful.
From other language-agnostic, third-party text objects:
l → a line (via kana/vim-textobj-line)
e → the entire buffer (via kana/vim-textobj-entire) so you can, say, indent the entire buffer with =ae.
i → an indented block (via kana/vim-textobj-indent). This isn’t quite as useful as it sounds for Python work as it stops at a blank line within an indented block – still worth having to hand.
c → a comment block (via glts/vim-textobj-comment).
jeetsukumaran/vim-pythonsense provides some Python text objects:
f → a Python function
c → a Python class2
d → a Python docstring
For editing Django templates (via mjbrownie/django-template-textobjects):
db → a {% block ... %}...{% endblock %} block
di → a {% if ... %}...{% endif %} conditional block
df → a {% for ... %}...{% endfor %} for loop block
dv → a {{ ... }} variable
dt → a {% tagname ... %} tag
Several of the above plugins are built using kana/vim-textobj-user, which provides a simple framework for building your own text-objects.
Useful references:
Cheatsheet for wellle/targets.vim
A list of all text-object plugins built with kana/vim-textobj-user
Related articles:
* Vim Text Objects: The Definitive Guide by Jared Carroll (2011).
* Vim Killer Features Part 1: Text Objects by Cody Veal (2013).
* Vim text objects, extend Vim’s natural language! by Owen Garland (2017).
:::