+1
.gitignore
+1
.gitignore
···
1
+
result
+70
doc/better-text-objs.txt
+70
doc/better-text-objs.txt
···
1
+
*better-text-objs.txt* Provide additional text objects
2
+
3
+
4
+
==============================================================================
5
+
INTRODUCTION *better-text-objs-intro*
6
+
7
+
This is a collection of handy text objects I found online,
8
+
written by romainl and habamax. I found these useful and
9
+
decided to package them into a plugin.
10
+
11
+
==============================================================================
12
+
OVERVIEW *better-text-objs-overview*
13
+
14
+
Pair |better-text-objs-pair|
15
+
Line |better-text-objs-line|
16
+
Backquote |better-text-objs-backquote|
17
+
Number |better-text-objs-number|
18
+
Buffer |better-text-objs-buffer|
19
+
Indent |better-text-objs-indent|
20
+
21
+
==============================================================================
22
+
PAIR *better-text-objs-pair*
23
+
24
+
`i_ i. i: i, i; i| i/ i\ i* i+ i- i#`
25
+
`a_ a. a: a, a; a| a/ a\ a* a+ a- a#`
26
+
27
+
Text objects in and around any two matching pairs of characters on the same
28
+
line.
29
+
30
+
==============================================================================
31
+
LINE *better-text-objs-line*
32
+
33
+
`il al`
34
+
35
+
Text objects to represent in and around the current line.
36
+
37
+
==============================================================================
38
+
BACKQUOTE *better-text-objs-backquote*
39
+
40
+
`ix ax`
41
+
42
+
Aliases for inner backquote and around backquote.
43
+
44
+
==============================================================================
45
+
NUMBER *better-text-objs-number*
46
+
47
+
`in an`
48
+
49
+
Text objects in and around a number (if present) under the cursor.
50
+
51
+
==============================================================================
52
+
BUFFER *better-text-objs-buffer*
53
+
54
+
`i% a%`
55
+
56
+
Text objects representing the active buffer, `i%` excludes the first and last
57
+
lines of the buffer, `a%` includes all lines of the buffer.
58
+
59
+
==============================================================================
60
+
INDENT *better-text-objs-indent*
61
+
62
+
`ii ai`
63
+
64
+
Text objects to represent the text at the same indent level, useful for indent
65
+
based programming languages, such as Python or Haskell. `ii` includes only
66
+
text at the same indent level, `ai` includes one line before and one line
67
+
after the current indent level.
68
+
69
+
------------------------------------------------------------------------------
70
+
vim:tw=78:ts=8:ft=help:norl:
+26
flake.lock
+26
flake.lock
···
1
+
{
2
+
"nodes": {
3
+
"nixpkgs": {
4
+
"locked": {
5
+
"lastModified": 1763017287,
6
+
"narHash": "sha256-il1gqLZcZCyZoP8fIJv800a7et2GZ6oOKb+bEV3FoXg=",
7
+
"owner": "NixOS",
8
+
"repo": "nixpkgs",
9
+
"rev": "84234b77dad4d4bcabe8ea4267411f6ba28b2126",
10
+
"type": "github"
11
+
},
12
+
"original": {
13
+
"owner": "NixOS",
14
+
"repo": "nixpkgs",
15
+
"type": "github"
16
+
}
17
+
},
18
+
"root": {
19
+
"inputs": {
20
+
"nixpkgs": "nixpkgs"
21
+
}
22
+
}
23
+
},
24
+
"root": "root",
25
+
"version": 7
26
+
}
+48
flake.nix
+48
flake.nix
···
1
+
{
2
+
3
+
description = "Improved text objects for {neo,}vim";
4
+
5
+
inputs = {
6
+
nixpkgs.url = "github:NixOS/nixpkgs";
7
+
};
8
+
9
+
outputs =
10
+
{ self
11
+
, nixpkgs
12
+
, ...
13
+
} @ rest:
14
+
let
15
+
supportedSystems = [ "x86_64-linux" "x86_64-darwin" ];
16
+
17
+
forAllSystems = nixpkgs.lib.genAttrs supportedSystems;
18
+
19
+
nixpkgsFor = forAllSystems (system:
20
+
import nixpkgs {
21
+
inherit system;
22
+
overlays = [ self.overlay ];
23
+
}
24
+
);
25
+
in
26
+
{
27
+
28
+
overlay = final: prev: rec {
29
+
better-text-objs =
30
+
with final; pkgs.vimUtils.buildVimPlugin {
31
+
pname = "better-text-objs";
32
+
version = "0.1.0";
33
+
src = ./.;
34
+
};
35
+
};
36
+
37
+
packages = forAllSystems (system:
38
+
{
39
+
inherit (nixpkgsFor."${system}") better-text-objs;
40
+
}
41
+
);
42
+
43
+
defaultPackage =
44
+
forAllSystems (system: self.packages."${system}".better-text-objs);
45
+
46
+
};
47
+
48
+
}
+156
plugin/better-text-objs.vim
+156
plugin/better-text-objs.vim
···
1
+
" Credits: romainl and habamax
2
+
3
+
" 24 simple text objects
4
+
" ----------------------
5
+
" i_ i. i: i, i; i| i/ i\ i* i+ i- i#
6
+
" a_ a. a: a, a; a| a/ a\ a* a+ a- a#
7
+
for char in [ '_', '.', ':', ',', ';', '<bar>', '/', '<bslash>', '*', '+', '-', '#' ]
8
+
execute 'xnoremap i' . char . ' :<C-u>normal! T' . char . 'vt' . char . '<CR>'
9
+
execute 'onoremap i' . char . ' :normal vi' . char . '<CR>'
10
+
execute 'xnoremap a' . char . ' :<C-u>normal! F' . char . 'vf' . char . '<CR>'
11
+
execute 'onoremap a' . char . ' :normal va' . char . '<CR>'
12
+
endfor
13
+
14
+
" line text objects
15
+
" -----------------
16
+
" il al
17
+
xnoremap il g_o^
18
+
onoremap il :<C-u>normal vil<CR>
19
+
xnoremap al $o0
20
+
onoremap al :<C-u>normal val<CR>
21
+
22
+
" backquote text objects
23
+
" -----------------
24
+
" ix ax
25
+
xnoremap ix :<C-u>normal vi`<CR>
26
+
onoremap ix :<C-u>normal vi`<CR>
27
+
xnoremap ax :<C-u>normal va`<CR>
28
+
onoremap ax :<C-u>normal va`<CR>
29
+
30
+
" number text object (integer and float)
31
+
" --------------------------------------
32
+
" in
33
+
function! VisualNumber()
34
+
call search('\d\([^0-9\.]\|$\)', 'cW')
35
+
normal v
36
+
call search('\(^\|[^0-9\.]\d\)', 'becW')
37
+
endfunction
38
+
xnoremap in :<C-u>call VisualNumber()<CR>
39
+
onoremap in :<C-u>normal vin<CR>
40
+
41
+
" buffer text objects
42
+
" -------------------
43
+
" i% a%
44
+
xnoremap i% :<C-u>let z = @/\|1;/^./kz<CR>G??<CR>:let @/ = z<CR>V'z
45
+
onoremap i% :<C-u>normal vi%<CR>
46
+
xnoremap a% GoggV
47
+
onoremap a% :<C-u>normal va%<CR>
48
+
49
+
" square brackets text objects
50
+
" ----------------------------
51
+
" ir ar
52
+
xnoremap ir i[
53
+
xnoremap ar a[
54
+
onoremap ir :normal vi[<CR>
55
+
onoremap ar :normal va[<CR>
56
+
57
+
" C++ style block comment text objects
58
+
" ------------------------------------
59
+
" i? a?
60
+
xnoremap a? [*o]*
61
+
onoremap a? :<C-u>normal va?V<CR>
62
+
xnoremap i? [*jo]*k
63
+
onoremap i? :<C-u>normal vi?V<CR>
64
+
65
+
" last change text object
66
+
" -----------------------
67
+
" ik ak
68
+
xnoremap ik `]o`[
69
+
onoremap ik :<C-u>normal vik<CR>
70
+
onoremap ak :<C-u>normal vikV<CR>
71
+
72
+
73
+
" Indent text object
74
+
" ------------------
75
+
" ii ai
76
+
func! s:indent_textobj(inner)
77
+
if getline('.') =~ '^\s*$'
78
+
let ln_start = s:detect_nearest_line()
79
+
let ln_end = ln_start
80
+
else
81
+
let ln_start = line('.')
82
+
let ln_end = ln_start
83
+
endif
84
+
85
+
let indent = indent(ln_start)
86
+
if indent > 0
87
+
while indent(ln_start) >= indent && ln_start > 0
88
+
let ln_start = prevnonblank(ln_start-1)
89
+
endwhile
90
+
91
+
while indent(ln_end) >= indent && ln_end <= line('$')
92
+
let ln_end = s:nextnonblank(ln_end+1)
93
+
endwhile
94
+
else
95
+
while indent(ln_start) == 0 && ln_start > 0 && getline(ln_start) !~ '^\s*$'
96
+
let ln_start -= 1
97
+
endwhile
98
+
while indent(ln_start) > 0 && ln_start > 0
99
+
let ln_start = prevnonblank(ln_start-1)
100
+
endwhile
101
+
while indent(ln_start) == 0 && ln_start > 0 && getline(ln_start) !~ '^\s*$'
102
+
let ln_start -= 1
103
+
endwhile
104
+
105
+
while indent(ln_end) == 0 && ln_end <= line('$') && getline(ln_end) !~ '^\s*$'
106
+
let ln_end += 1
107
+
endwhile
108
+
while indent(ln_end) > 0 && ln_end <= line('$')
109
+
let ln_end = s:nextnonblank(ln_end+1)
110
+
endwhile
111
+
endif
112
+
113
+
if a:inner || indent == 0
114
+
let ln_start = s:nextnonblank(ln_start+1)
115
+
endif
116
+
117
+
if a:inner
118
+
let ln_end = prevnonblank(ln_end-1)
119
+
else
120
+
let ln_end = ln_end-1
121
+
endif
122
+
123
+
if ln_end < ln_start
124
+
let ln_end = ln_start
125
+
endif
126
+
127
+
exe ln_end
128
+
normal! V
129
+
exe ln_start
130
+
endfunc
131
+
132
+
133
+
func! s:nextnonblank(lnum) abort
134
+
let res = nextnonblank(a:lnum)
135
+
if res == 0
136
+
let res = line('$')+1
137
+
endif
138
+
return res
139
+
endfunc
140
+
141
+
142
+
func! s:detect_nearest_line() abort
143
+
let lnum = line('.')
144
+
let nline = s:nextnonblank(lnum)
145
+
let pline = prevnonblank(lnum)
146
+
if abs(nline - lnum) > abs(pline - lnum) || getline(nline) =~ '^\s*$'
147
+
return pline
148
+
else
149
+
return nline
150
+
endif
151
+
endfunc
152
+
153
+
onoremap <silent>ii :<C-u>call <sid>indent_textobj(v:true)<CR>
154
+
onoremap <silent>ai :<C-u>call <sid>indent_textobj(v:false)<CR>
155
+
xnoremap <silent>ii :<C-u>call <sid>indent_textobj(v:true)<CR>
156
+
xnoremap <silent>ai :<C-u>call <sid>indent_textobj(v:false)<CR>