unknown_document

We couldn't find that page... Were you looking for one of these?


Match Boundaries
By default, when a pattern matches a string the entire match is returned. For example, suppose we have a buffer that contains a number of dates: Initial Conditions 2 022-01-14 2022-07-09 2022-10-23 2022-12-20 2023-01-26 2023-03-17 2023-08-06 2023-11-05 COMMAND Top 1:1 /2022-1\d-\d\d Suppose we wanted to edit all dates that occur in Q4 (months 10, 11, and 12) of 2022. Our first thought might be to define capturing groups then use back-references to search and replace to make the necessary edits. This is a great solution when the edits are well-defined, but this hypothetical scenario requires us to manually-edit the dates. Our next thought might be to search for all dates that meet these constraints with a pattern such as: 2022-1\d-\d\d Search with generic pattern 2 022-01-14 2022-07-09 2 022-10-23 2022-12-20 2023-01-26 2023-03-17 2023-08-06 2023-11-05 NORMAL 33% 3:1 then jump to a result, move the cursor to the month and day, make the necessary edits, then repeat the search for each match. This works, but can be improved. Our pattern matches to correct lines, but returns the entire date when we only want the month and date. This is a good use-case for adding match boundaries . At a high-level, match boundaries break the searching process up into two steps: The entire pattern is used to define which text to matched, then The match boundaries define which portion of the matched text to return Match boundaries are defined using one or both of the following markers: Shortcut Definition \zs Defines where the matched text will start \ze Defines where the matched text will end When \zs is present, the match will start with the character immediately to the right of it. Likewise, when \ze is present, the match will end with the character immediately to the left of it. Back to our example, our current pattern does a good job of matching the correct lines in the buffer, we just need to isolate the match to the month and date. This can be done by defining the starting boundary: 2022-\zs1\d-\d\d and executing the search again: Search with start boundary 2 022-01-14 2022-07-09 2022- 1 0-23 2022- 12-20 2023-01-26 2023-03-17 2023-08-06 2023-11-05 NORMAL 33% 3:6 Now, when we just from match to match to make our edits, we are already in the correct location. Pretty cool. To extend the example, let's add an ending boundary to isolate only the month portion of the date: 2022-\zs1\d\ze-\d\d and execute again: Search with start and end boundaries 2 022-01-14 2022-07-09 2022- 1 0 -23 2022- 12 -20 2023-01-26 2023-03-17 2023-08-06 2023-11-05 NORMAL 33% 3:6 Match boundaries provide an extra degree of freedom that can be leveraged in some cases, such as this.
Searching in Neovim
Up to now we have reviewed many ways to navigate through a document, from simple cursor movements, to paging, to text objects. Vim also provides the ability to search a buffer for specific text or a pattern for desired text, then quickly navigating through matching text. Searching forward The first search operator we will look at it / , which searches the buffer forward from the current cursor location. Starting from the following buffer: Initial Conditions 1 . Beautiful is better than ugly. 2. Explicit is better than implicit. 3. Simple is better than complex. 4. Complex is better than complicated. COMMAND Top 1:1 /comp Initiate the search by typing / , followed by the text you want to search for, then finally hit enter to start the search. Search forward 1 . Beautiful is better than ugly. 2. Explicit is better than implicit. 3. Simple is better than c omplex. 4. Complex is better than complicated. NORMAL 60% 3:26 Vim found the text, and moved the cursor forward to the start of the first match. Since there are often multiple matches, Vim provides a shortcut command for repeating a search. To repeat the search, hit n . Repeat the previous search n 1. Beautiful is better than ugly. 2. Explicit is better than implicit. 3. Simple is better than c omplex. 4. Complex is better than c omplicated. NORMAL 80% 4:27 Repeating the search moves the cursor to the location of the next matching text. The cursor is now at the last match, what happens if we repeat the search once more? Repeat again n 1. Beautiful is better than ugly. 2. Explicit is better than implicit. 3. Simple is better than c omplex. 4. Complex is better than c omplicated. NORMAL 60% 3:26 When Vim is not able to find matching text, it "wraps around" to the top of the buffer and continues the search. In this example there are only two matches in the document, so the cursor returns to the first match. Searching backward Searching backward uses the ? operator, but otherwise operates the same as searching forward: Search backward 1. Beautiful is better than ugly. 2. Explicit is better than implicit. 3. Simple is b etter than c omplex. 4. Complex is better than complicated. NORMAL 60% 3:14 The n also works when searching backward, and like most operators this shortcut accepts a count . To move the cursor to the "second next match", precede the operator with a count of 2: Repeat search with count 2n 1. Beautiful is b etter than ugly. 2. Explicit is better than implicit. 3. Simple is b etter than complex. 4. Complex is better than complicated. NORMAL Top 1:17 As expected, this moves the cursor to the start of the first match. Now, suppose we moved the cursor too far, and wanted to jump to the second match. Vim has a second shortcut for repeating a search, but in the opposite direction: N . Repeat in the opposite direction N 1. Beautiful is b etter than ugly. 2. Explicit is b etter than implicit. 3. Simple is better than complex. 4. Complex is better than complicated. NORMAL 40% 2:16 Search for word under cursor Vim provides two additional shortcuts, & and # , for searching in the forward and backward directions, respectively. These shortcuts initiate a search and populate the search pattern with the word that is currently under the cursor, so that the search executes by simply hitting <CR> . Here is a quick summary of the search-related operators: Command Action /{pattern} search forward for the Nth occurrence of {pattern} ?{pattern} search backward for the Nth previous occurrence of {pattern} & repeat last :s # search backward for the Nth occurrence of the ident under the cursor n repeat the latest '/' or '?' N times N repeat the latest '/' or '?' N times in opposite direction This section has introduced the basics of searching a buffer with simple patterns. Vim extends the searching function with powerful patterns that can pinpoint specific text, and also supports the ability to search and replace text that has been matched.
Lua's Concatenation Operator
Concatenation refers to the common operation of joining of two strings . In Lua, when two strings are concatenated the result is a third string that contains the characters of the first string followed by the characters contained in the second string: local x = "left side" local y = "right side" print ( x .. y ) -- left sideright side -- separating strings with a literal " " print ( x .. " " .. y ) -- left side right side Note that the concatenation is literal: no space is added between two concatenated strings. When concatenating sentences be sure to include a literal space between the two strings. Concatenation also works with numbers , by first coercing each value to its string-equivalent then concatenating those values: local x = 2 local y = 3 print ( x .. y ) -- 23 Note that concatenation won't work with other value fundamentals, such as booleans or nil .
Control Structures
Lua implements several of the basic control structures that might be expected in a programming language. Here is a quick summary of the available options, then we will get into the details in the coming sections: Control Type Structure Conditional If-Then Condition-controlled Loop While Condition-controlled Loop Repeat Count-controlled Loop Numeric For Collection-controlled Loop Generic For
Lua's While Loop
The While Loop is a "condition-controlled" loop, which executes a block of code 0 or more times, stopping when the controlling condition is true . While loops follow the pattern: while [ boolean expression ] do -- loop body -- repeat as long as boolean expression is true end As a simple example, let's sum up all numbers from 0 to 4: local x = 0 local result = 0 while x < 5 do result = result + x x = x + 1 end print ( result ) -- 10 The break statement can be used to terminate a while loop early. The following is equivalent to the previous example. local x = 0 local result = 0 while true do result = result + x x = x + 1 if x >= 5 then break end end print ( result ) -- 10 Note that in this case we used a boolean expression that is simply true . This is a common practice in many situations, but requires a bit of care in order to prevent infinite loops . We should point out that when a While Loop is contained within a function, the loop can also terminate when it encounters a return statement: local function sum () local x = 0 local result = 0 while true do result = result + x x = x + 1 if x >= 5 then return result end end end print ( sum ()) -- 10
Anchors in Neovim Patterns
By default, patterns match anywhere in a string. In many cases you want patterns to match in only specific parts of a string. For example, if you want to match words at start with a pattern you don't want to match characters in the middle of the word. This can be achieved using anchors . Anchors don't match any characters, but instead they specify where in a string matches are allowed to begin and/or end. Some common anchors are: Anchor Description ^ Match at the start of a line $ Match at the end of a line \< Match at the start of a word \> Match at the end of a word \%^ Match at the start of a file \%$ Match at the end of a file Lets demonstrate the behavior of some of the more common anchors using the buffer below: Initial Conditions J ohn Evans Kim Aaron Mia Johnson Johnny Matthew Martin Nancy Kathleen John Heather Vanessa Perez-Johnson Scott Johnathon Ford Johnnie Dillon Odom Jessica Jennifer LittleJohn COMMAND Top 1:1 /John First, let's perform an un-anchored search to show the default behavior: Search for John J ohn Evans Kim Aaron Mia J ohn son John ny Matthew Martin Nancy Kathleen John Heather Vanessa Perez- John son Scott John athon Ford John nie Dillon Odom Jessica Jennifer Little John NORMAL 22% 2:11 We found 8 instances of the string "John", which appear at all different parts of the respective strings. Next lets add the "^" anchor and search again: Anchored search for John John Evans Kim Aaron Mia Johnson J ohn ny Matthew Martin Nancy Kathleen John Heather Vanessa Perez-Johnson Scott Johnathon Ford John nie Dillon Odom Jessica Jennifer LittleJohn NORMAL 33% 3:1 This matches only the 3 locations where "John" appears at the start of the line. Compare that to search with the "$" anchor: Anchored search for John J ohn Evans Kim Aaron Mia Johnson Johnny Matthew Martin Nancy Kathleen J ohn Heather Vanessa Perez-Johnson Scott Johnathon Ford Johnnie Dillon Odom Jessica Jennifer Little John NORMAL 44% 4:16 Here we match at the only two locations where the lines end with "John". Now let's try anchoring to work boundaries. First, let's try anchoring to the start of the word with "\<" Anchored search for John John Evans Ki m Aaron Mia J ohn son John ny Matthew Martin Nancy Kathleen John Heather Vanessa Perez- John son Scott John athon Ford John nie Dillon Odom Jessica Jennifer LittleJohn NORMAL 22% 2:11 Finally, lets anchor to the tail of the word, using ">": Anchored search for John John Evans Kim Aaron Mia Johnson Johnny Matthew Martin Nancy Kathleen J ohn Heather Vanessa Perez-Johnson Scott Johnathon Ford Johnnie Dillon Odom Jessica Jennifer Little John NORMAL 44% 4:16
Coreutils
"coreutils" refers to the GNU core utilities , which are the basic set of file, shell, and text manipulation utilities that are expected to be installed on every UNIX-like operating system. The coreutils include a fairly wide range of commands that apply to many situations. In this section we will focus on just a few of the more common commands that are used for working with text files: Basics Basic commands that are used for viewing and summarizing file content File Modification Commands that are used for modifying files, such as sorting them.
Lua's Repeat Loop
Like the While Loop , the Repeat Loop is a "condition-controlled" loop. Unlike the While Loop , the Repeat Loop guarantees that the loop body is executed at least one time, then stops when the controlling condition is true . Repeat loops follow the pattern: repeat -- loop body -- repeat as long as boolean expression is true until [ boolean expression ] As a simple example, let's sum up all numbers from 0 to 4 using a repeat loop : local x = 0 local result = 0 repeat result = result + x x = x + 1 until x >= 5 print ( result ) -- 10 The break statement can be used to terminate a repeat loop early, just like a while loop . The following is equivalent to the previous example. local x = 0 local result = 0 repeat result = result + x x = x + 1 if x >= 5 then break end until false print ( result ) -- 10 Note that in this case we set the boolean expression to false , so that it continues to execute until the break is encountered. As while the while loop , care must be taken to prevent infinite loops . Finally, let's implement the final example from the while loop section using a repeat loop , showing that the repeat loop also terminates when a return statement is encountered: local function sum () local x = 0 local result = 0 repeat result = result + x x = x + 1 if x >= 5 then return result end until false end print ( sum ()) -- 10
Lua's Relational Operators
Relational Lua's relational operators are more or less the same as in most other languages: local x = 2 local y = 3 print ( x > y ) -- false print ( x < y ) -- true print ( x >= y ) -- false print ( x <= y ) -- true print ( x <= x ) -- true print ( x >= x ) -- true The relational operators allow one to determine when values are greater-than or less-than others, and include a variant that also allows the variables to be equal. Equality Lua's equality operator uses two equal signs == , which is differentiated from the variable assignment operator = which uses a single equal sign: local x = 2 local y = 3 print ( x == y ) -- false print ( x == x ) -- true This operator returns true when the two values are equal, otherwise it returns false . Inequality Like equality , it can often be useful to know when two values are not equal. Lua implements this using the ~= operator: local x = 2 local y = 3 print ( x ~= y ) -- true print ( x ~= x ) -- false This operator returns false when the two values are equal, otherwise it returns true .