Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

The principle of Vimscript segment movement


May 24, 2021 Vim


Table of contents


If you haven't used Vim's ]] segment [] move commands [[ , . ][ Also read by the :help section

Still don't understand? I t's not a problem, it's the same when I first read this. Before we write the code, let's go and learn how these movements work, and then in the next chapter we'll make our Potion plug-ins support them.

Nroff file

The four "segment move" commands, as they literally mean, can be used to move between "segments" of a file.

These commands are designed by default for the nroff file. Nroff is similar to LaTex or Markdown -- it's used to write tag text (and eventually generates a UNIX man page).

Nroff files use a set of "macros" to define "segment headers." For example, here's awk man page:

.SH NAME                                                     ***
awk \- pattern-directed scanning and processing language
.SH SYNOPSIS                                                 ***
.B awk
[
.BI \-F
.I fs
]
[
.BI \-v
.I var=value
]
[
.I 'prog'
|
.BI \-f
.I progfile
]
[
.I file ...
]
.SH DESCRIPTION                                              ***
.I Awk
scans each input
.I file
for lines that match ...

To .SH the beginning of the SH is the segment header. *** out with a . . . The four-segment move command moves your cursor between the first lines of the segment.

Vim . And nroff's segment header starts with any line as a segment header, even if you're not editing the nroff file!

You can sections to change the segment header, but Vim still needs to have a point at the beginning of the line, and the segment header must be a pair of characters, so this change to the Potion file won't be flexible enough.

Brackets

The segment move command also sees another thing: an open or closed brace { ) } the first character of the line.

[[ and ]] open brackets, ][ and view closed parentheses. []

This extra "behavior" allows you to easily move between segments of the C-style language. However, these rules still don't take into account the type of file you're editing!

Add the following to a buffer:

Test           A B
Test

.SH Hello      A B

Test

{              A
Test
}                B

Test

.H World       A B

Test
Test           A B

Now execute :set filetype=basic tell Vim that this is a BASIC file and try the segment move command.

[[ The ]] will move A ][ marked A, and the lines marked B will move between rows marked B []

This tells us that Vim always uses the same two rules to handle segment movements, even if none of them work (as in the case of BASIC)!

Practice

Read :help section and now you should be able to understand segment movement.

Also read by the :help sections