Skip to content

Commit 9861dc6

Browse files
authored
Merge pull request #73 from kana/add-filetype-example
Add example to define text objects for a filetype
2 parents 074ce25 + e2f60c4 commit 9861dc6

File tree

2 files changed

+64
-14
lines changed

2 files changed

+64
-14
lines changed

README.md

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,15 @@ call textobj#user#plugin('datetime', {
3434

3535
### Simple text objects surrounded by a pair of patterns
3636

37-
Define `aP` to select a PHP code with `<?php` and `?>`, and
38-
define `iP` to select a PHP code without `<?php` and `?>`:
37+
Define `aA` to select text from `<<` to the matching `>>`, and
38+
define `iA` to select text inside `<<` and `>>`:
3939

4040
```vim
41-
call textobj#user#plugin('php', {
42-
\ 'code': {
43-
\ 'pattern': ['<?php\>', '?>'],
44-
\ 'select-a': 'aP',
45-
\ 'select-i': 'iP',
41+
call textobj#user#plugin('braces', {
42+
\ 'angle': {
43+
\ 'pattern': ['<<', '>>'],
44+
\ 'select-a': 'aA',
45+
\ 'select-i': 'iA',
4646
\ },
4747
\ })
4848
```
@@ -85,6 +85,33 @@ endfunction
8585
```
8686

8787

88+
### Text objects for a specific filetype
89+
90+
Define `a(` to select text from `\left(` to the matching `\right)`, and
91+
define `i(` to select text inside `\left(` to the matching `\right)`,
92+
but *only for tex files*:
93+
94+
```vim
95+
call textobj#user#plugin('tex', {
96+
\ 'paren-math': {
97+
\ 'pattern': ['\\left(', '\\right)'],
98+
\ 'select-a': [],
99+
\ 'select-i': [],
100+
\ },
101+
\ })
102+
103+
augroup tex_textobjs
104+
autocmd!
105+
autocmd FileType tex call textobj#user#map('tex', {
106+
\ 'paren-math': {
107+
\ 'select-a': '<buffer> a(',
108+
\ 'select-i': '<buffer> i(',
109+
\ },
110+
\ })
111+
augroup END
112+
```
113+
114+
88115

89116

90117
## Further reading

doc/textobj-user.txt

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,14 @@ or use functions to define complex ones. For example...
6161
\ })
6262
<
6363
*textobj-user-example-between*
64-
(b) Define "aP" to select a PHP code with "<?php" and "?>", and
65-
define "iP" to select a PHP code without "<?php" and "?>":
64+
(b) Define "aA" to select text from "<<" to the matching ">>", and
65+
define "iA" to select text inside "<<" and ">>":
6666
>
67-
call textobj#user#plugin('php', {
68-
\ 'code': {
69-
\ 'pattern': ['<?php\>', '?>'],
70-
\ 'select-a': 'aP',
71-
\ 'select-i': 'iP',
67+
call textobj#user#plugin('braces', {
68+
\ 'angle': {
69+
\ 'pattern': ['<<', '>>'],
70+
\ 'select-a': 'aA',
71+
\ 'select-i': 'iA',
7272
\ },
7373
\ })
7474
<
@@ -105,6 +105,29 @@ or use functions to define complex ones. For example...
105105
\ : 0
106106
endfunction
107107
<
108+
*textobj-user-example-filetype*
109+
(d) Define "a(" to select text from "\left(" to the matching "\right)", and
110+
define "i(" to select text inside "\left(" to the matching "\right)",
111+
but only for tex files:
112+
>
113+
call textobj#user#plugin('tex', {
114+
\ 'paren-math': {
115+
\ 'pattern': ['\\left(', '\\right)'],
116+
\ 'select-a': [],
117+
\ 'select-i': [],
118+
\ },
119+
\ })
120+
121+
augroup tex_textobjs
122+
autocmd!
123+
autocmd FileType tex call textobj#user#map('tex', {
124+
\ 'paren-math': {
125+
\ 'select-a': '<buffer> a(',
126+
\ 'select-i': '<buffer> i(',
127+
\ },
128+
\ })
129+
augroup END
130+
<
108131

109132
You can define your own text objects like the above examples.
110133
See also |textobj-user-reference| for more details.

0 commit comments

Comments
 (0)