Skip to content
David-Apps edited this page Sep 24, 2023 · 12 revisions

These functions count the matches for a regular expression in the current buffer or the current line. Use the ci command to control whether the searches are case sensitive.

The function match counts all matches.

The function lmatch counts the lines with at least 1 match.

The function dotmatch counts matches in the current line.

The function mc makes a copy of the buffer from the current line to the last line and, at the beginning of each of these lines, writes the number of matches in that line. Use the ^ or up commands to return to the original buffer. For example, <mc . writes the number of characters in the line at the beginning of each line.

The function sortmc:

  • makes a copy of the buffer
  • at the beginning of each lline, writes the number of matches in that line and the line number
  • sorts the lines by the number of matches

Use the ^ or up commands to return to the original buffer. For example, <sortmc \S+ sorts the lines by the number of words in each line.

# Count matches.
# usage: <match [regular expression]
# This function uses the label z.
function:match {
db0
H-
sw+
e/temporary buffer for function match to use
if(*) {
eret
ebvar-
!echo temporary buffer already exists
} else {
# Use global substitutions so that we can call the function with no arguments to use the remembered search string.
sg+
etmp
f temporary buffer for function match to use
M0
kz
g/~0/f .w/temporary buffer for function match to use@$
'zX
e/temporary buffer for function match to use
a

.
.g/~0/fd
if(?) {
d
,s/~0/&\n/gf
v/~0/fd
}
=
bw
eret
q/temporary buffer for function match to use
}
}

# Count lines that match.
# usage: <lmatch [regular expression]
# This function uses the label z.
function:lmatch {
db0
H-
sw+
e/temporary buffer for function lmatch to use
if(*) {
eret
ebvar-
!echo temporary buffer already exists
} else {
# Use global substitutions so that we can call the function with no arguments to use the remembered search string.
sg+
etmp
f temporary buffer for function lmatch to use
M0
kz
g/~0/f .w/temporary buffer for function lmatch to use@$
'zX
e/temporary buffer for function lmatch to use
=
bw
eret
q/temporary buffer for function lmatch to use
}
}

# Count matches in the current line.
# usage: <dotmatch [regular expression]
function:dotmatch {
db0
H-
sw+
e/temporary buffer for function dotmatch to use
if(*) {
eret
ebvar-
!echo temporary buffer already exists
} else {
# Use global substitutions so that we can call the function with no arguments to use the remembered search string.
sg+
etmp
f temporary buffer for function dotmatch to use
M0
.w/temporary buffer for function dotmatch to use@$
e/temporary buffer for function dotmatch to use
s/~0/&\n/gf
d
=
bw
eret
q/temporary buffer for function dotmatch to use
}
}

# Count matches for each line.
# usage: <mc [regular expression]
function+mc {
db0
H-
sw+
e/temporary buffer for function mc to use
if(*) {
eret
ebvar-
!echo temporary buffer already exists
} else {
# Use global substitutions so that we can call the function with no arguments to use the remembered search string.
sg+
etmp
f temporary buffer for function mc to use
M0
;w/temporary buffer for function mc to use@$
if(*) {
etmp
f mc ~0
r/temporary buffer for function mc to use
1X
while(*) {
.w/temporary buffer for function mc to use@0
e/temporary buffer for function mc to use
s/~0/&\n/gf
if(*) {
;d
s/.*/%line/f
} else {
,c
0
.
}
eret
-r/temporary buffer for function mc to use@.
J
+X
}
e/temporary buffer for function mc to use
bw
eret
bw
1
} else {
h
}
q/temporary buffer for function mc to use
}
}

# Sort lines by match count.
# usage: <sortmc [regular expression]
function+sortmc {
db0
.X
if(*) {
H-
ebvar-
sw+
e/temporary buffer for function sortmc to use
if(*) {
eret
!echo temporary buffer already exists
} else {
# Use global substitutions so that we can call the function with no arguments to use the remembered search string.
sg+
etmp
f temporary buffer for function sortmc to use
M0
w/temporary buffer for function sortmc to use@$
etmp
f sortmc ~0
r/temporary buffer for function sortmc to use
1X
while(*) {
.w/temporary buffer for function sortmc to use@0
s/.*/%line &/f
e/temporary buffer for function sortmc to use
s/~0/&\n/gf
if(*) {
;d
s/.*/%line/f
} else {
,c
0
.
}
eret
-r/temporary buffer for function sortmc to use@.
J
+X
}
e/temporary buffer for function sortmc to use
bw
eret
q/temporary buffer for function sortmc to use
W !sort -gr
bw
1
}
}
}