What is a Terminal?
Much of the terminology used when discussing modern terminals comes from history, which can make understanding terminals seem more confusing than it should be. A small amount history can help provide some background information to help some of the concepts be more relatable. Early computers were comprised of multiple refrigerator-sized cabinets, each of which performed one of the basic tasks of the system. For example, one cabinet would contain the "central processing unit " (CPU), while other cabinets held tape drives , disk drives , punched-card readers , a line printer , and any other peripherals that were available to the computer. The user-interface to these computers consisted of a keyboard and some means of displaying the output from the computer, such as a sequence of light bulbs or later a Cathode-ray Tube (CRT) display , connected to the computer via cables. The user-interface was typically arranged into a "console ", where the operator could sit and interact with the computer. Since the user-interface represented the point at which electrical signals "enter" and "exit" the network, the user-interfaces themselves became known as "terminals ". Standardization As often happens in technology development, there were initially a wide range of competing technologies, but over time market forces and economies of scale led these competing technologies to converge towards standardization around a handful of those technologies, the names of which have become much of the jargon that surrounds the terminal today. Early terminals were electro-mechanical "Teletype writers", generally shortened to "TTY ", which is a term still used today when interacting with the terminal. As with a modern keyboard, each key on the TTY represented a human-readable character such as a letter, number, punctuation, etc. As the user would press a key to input data into the computer, each key would be "encoded " into a sequence of 8 1s and 0s called bytes , which could be understood by the computer. These bytes were sent over the cable to the CPU, which performed the requested operations, then the resulting byte(s) were transmitted back to the terminal where they were "decoded" back into characters for human-consumption. The rules used for encoding and decoding each character eventually became the ASCII standard. Despite the benefits of standardization, the ASCII standard was written to support US English, which led to several extensions in order to support other languages, which eventually led to the UTF-8 standard which unified the various extensions into a single encoding that is broadly in use today. Over time display technologies evolved which led to the introduction of "video terminals" (VT) which allowed text and other graphical information to be displayed on a screen. Some of the most popular video terminals were the VT100 and later VT200 series, which introduced support for the ANSI escape codes that have become standards that are still in use today. Emulation As microprocessor technology advanced and the cost of memory and other peripherals dropped, terminals began to handle increasingly-advanced operations, which led to the introduction of "intelligent terminals ", which differentiated them from the "dumb terminals" they replaced. In order to make the newer terminals "backward compatible " with existing software, these terminals included hardware to "emulate " the older devices. Over time the emulation functions were implemented in software, which eventually led to the introduction of fully-software "terminal emulators " that could offer features that would have been difficult to impossible to implement in hardware, such as command-line completion and syntax highlighting . With the introduction of terminal emulators, "the terminal" became a software window that is opened in order to gain access to the operating system . The software that provides this access is called "the shell", which is the subject of the next section.
Neovim Fundamentals
So you have made the decision to jump into Neovim, and looking for a place to get started. Like any new skill, learning the first few basics can seem challenging, but once you get past them you will become proficient with Neovim in no time. At that point, the adrenaline rush that comes with Neovim proficiency leaves us wanting more, and at this critical juncture new users can take one of two paths: The first path looks a bit like this: Search the internet to see all of the cool configurations people show off Install a bunch of popular plugins Copy & paste snippets of code into their configuration file Get frustrated because things stop working as expected Conclude that Neovim is difficult The second path looks a bit like this: Learn stock Neovim to understand the basics of using the editor Identify specific improvements that can be made to suit your workflow Assess a few potential solutions then select the solution that works the best for you Continue working until another improvement is identified Iterate as your skill level and needs evolve over time In other words, mastering Neovim is more of a marathon than a sprint. It helps to start by understanding what you want to achieve with Neovim. Are you looking for a full IDE, or a text editor? Are you drawn to Neovim for its speed and efficiency, or do you plan to configure every last aspect of how it works? Each of us will have slightly different answers. We prefer to follow the Unix philosophy - each tool in our arsenal should do one thing and do it well. As such, we rely on Neovim as an excellent text editor, then use other tools to manage our git repositories, etc. When it makes sense to do so, we can create a thin wrapper within Neovim to provide just the functionality that we need within Neovim. We generally limit our plugins to only those that truly provide unique functionality that we can't achieve with stock Neovim or with our own personal plugins or user-commands . Regardless of your philosophy, our goal is to start from the basics and slowly build proficiency with Neovim, then progress to more advanced topics such as configuration and plugins . Let's get started!
Neovim's Tabs
Up to now, we have learned that buffers are in-memory representations of files, and windows provide viewports that allow one or more buffers to be viewed at a time in splits . This brings us to the final concept in this section, Tabs. Thinking back to our discussion about windows , we started with a single window , then added a few splits to create a layout. The collection of windows in the layout fills up the screen, and the full screen could be considered "a page of windows ". Neovim allows you to define and switch between multiple pages of windows by organizing them into "tabs". Here is a list of some of the more common tab-related commands . Command Action :tabnew edit a file in a new tab page :tab create new tab when opening new window :tabs list the tab pages and what they contain :tabclose close current tab page :tabonly close all tab pages except the current one :tabprevious go to previous tab page :tabnext go to next tab page :tabNext go to previous tab page :tabmove move tab page to other position :tabfirst go to first tab page :tablast go to last tab page :tabfind find file in 'path', edit it in a new tab page
Getting Help in Neovim
Neovim includes an extensive help system, which provides a significant amount of detail about virtually any vim-related topic. To open the help system, from Normal mode enter the command : :help Which splits the current window and displays a buffer containing the main help page. We will review splits shortly in the windows chapter. navigate the help window as you would any buffer , for example using j , k , etc. Help for a specific topic The help contents for a specific topic or command by adding the topic or command when invoking the help system. For example, to review the documentation for the help system itself: :help help Following links Help contents often contain links to other help topics. To review the linked content, move the cursor over the link and type C-] . Changing topics Once in the help system you can manually jump to other topics using the tag command: :tag [topic] where topic refers to the help topic you want to jump to. Returning to topics Return to the previous help topic by invoking C-T . Exiting the help window Exit the help window and return to the original window by typing C-W c or :quit .
Working with Lua Patterns
In the previous section we looked at format strings , which provide a convenient way to create consistent, well-formatted strings that can contain unique values. In this chapter we look at patterns, which are roughly the opposite: patterns allow us to extract values from well-formatted strings. A pattern defines a specification that describes the content of a strings . When a pattern is compared to a "target" string , if the pattern describes the string we say that the target "matches the pattern". Patterns are generally composed of a sequence of smaller patterns, called "atoms". These atoms are arranged in a specific sequence to create the specification. As a simple example, let's build a pattern that matches phone numbers, then we will describe the process in more detail in the coming sections. We will assume phone numbers matching the format: +1(234)567-8910 The first step is to break this string into a sequence of components: +[country code]([area code])[prefix]-[line number] Next, we need to specify each component in terms of regular expressions, then combine them into the pattern. One possible final pattern might be: +%d[(]%d%d%d[)]%d%d%d-%d%d%d%d which is made up of: A literal + The "character class " %d, which specifies that the characters in those locations must be digits, Literal ( and ) surrounding a sequence of 3 digits %d Sequences of 3 and 4 digits, separated by a literal - If we apply this pattern to our target phone number, the target string is returned (which is a truthy value indicating that a match occurred. For comparison, we include a few other target strings with slightly different format, which produce nil (i.e. falsy ) values indicating that a match did not occur. localpattern="+%d[(]%d%d%d[)]%d%d%d[-]%d%d%d%d" print(string.match("+1(234)567-8910",pattern))-- +1(234)567-8910 print(string.match("+1(23)4567-8910",pattern))-- nil print(string.match("(234)567-8910",pattern))-- nil With that quick introduction, let's learn how to create patterns.
Neovim's Page Motions
Vim also includes a variety of commands that support moving the cursor in page-related increments, which can be convenient when moving the cursor larger distances within a buffer. Command Action C-U Move cursor up one-half page C-D Move cursor down one-half page C-B Move screen up one page C-F Move screen down one page To demonstrate how these commands behave, the first sequence of steps navigates down the page using C-D : Start Initial Conditions Siddhartha by Herman Hesse THE SON OF THE BRAHMAN In the shade of the house, in the sunshine of the riverbank near the boats, in the shade of the Salwood forest, in the shade of the fig tree is where Siddhartha grew up, the handsome son of the Brahman, the NORMAL Top 1:1 1 Move the cursor<C-d> THE SON OF THE BRAHMAN In the shade of the house, in the sunshine of the riverbank near the boats, in the shade of the Salwood forest, in the shade of the fig tree is where Siddhartha grew up, the handsome son of the Brahman, the young falcon, together with his friend Govinda, son of a Brahman. The sun tanned his light shoulders by the banks of the river when bathing, performing the sacred ablutions, the sacred offerings. In the mango grove, shade poured into his black eyes, when playing as a boy, when NORMAL 36% 5:1 2 Move the cursor (again)<C-d> In the shade of the house, in the sunshine of the riverbank near the boats, in the shade of the Salwood forest, in the shade of the fig tree is where Siddhartha grew up, the handsome son of the Brahman, the young falcon, together with his friend Govinda, son of a Brahman. The sun tanned his light shoulders by the banks of the river when bathing, performing the sacred ablutions, the sacred offerings. In the mango grove, shade poured into his black eyes, when playing as a boy, when his mother sang, when the sacred offerings were made, when his father, the scholar, taught him, when the wise men talked. NORMAL 56% 9:1 3 Move the cursor back<C-d> In the shade of the house, in the sunshine of the riverbank near the boats, in the shade of the Salwood forest, in the shade of the fig tree is where Siddhartha grew up, the handsome son of the Brahman, the young falcon, together with his friend Govinda, son of a Brahman. The sun tanned his light shoulders by the banks of the river when bathing, performing the sacred ablutions, the sacred offerings. In the mango grove, shade poured into his black eyes, when playing as a boy, when his mother sang, when the sacred offerings were made, when his father, the scholar, taught him, when the wise men talked. NORMAL 81% 13:1 4 Move the cursor back<C-d> In the shade of the house, in the sunshine of the riverbank near the boats, in the shade of the Salwood forest, in the shade of the fig tree is where Siddhartha grew up, the handsome son of the Brahman, the young falcon, together with his friend Govinda, son of a Brahman. The sun tanned his light shoulders by the banks of the river when bathing, performing the sacred ablutions, the sacred offerings. In the mango grove, shade poured into his black eyes, when playing as a boy, when his mother sang, when the sacred offerings were made, when his father, the scholar, taught him, when the wise men talked. NORMAL 94% 15:1 Note that the cursor remained at the top of the screen, maximizing the amount of content below the cursor, until it approached the bottom of the buffer. At that point the cursor progressed toward the bottom of the buffer. Next, navigate back up the page using C-U : Start Move the cursor back In the shade of the house, in the sunshine of the riverbank near the boats, in the shade of the Salwood forest, in the shade of the fig tree is where Siddhartha grew up, the handsome son of the Brahman, the young falcon, together with his friend Govinda, son of a Brahman. The sun tanned his light shoulders by the banks of the river when bathing, performing the sacred ablutions, the sacred offerings. In the mango grove, shade poured into his black eyes, when playing as a boy, when his mother sang, when the sacred offerings were made, when his father, the scholar, taught him, when the wise men talked. NORMAL 94% 15:1 1 Move the cursor back<C-u> by Herman Hesse THE SON OF THE BRAHMAN In the shade of the house, in the sunshine of the riverbank near the boats, in the shade of the Salwood forest, in the shade of the fig tree is where Siddhartha grew up, the handsome son of the Brahman, the young falcon, together with his friend Govinda, son of a Brahman. The sun tanned his light shoulders by the banks of the river when bathing, NORMAL 92% 11:1 2 Move the cursor back<C-u> Siddhartha by Herman Hesse THE SON OF THE BRAHMAN In the shade of the house, in the sunshine of the riverbank near the boats, in the shade of the Salwood forest, in the shade of the fig tree is where Siddhartha grew up, the handsome son of the Brahman, the NORMAL 70% 7:1 3 Move the cursor back<C-u> Siddhartha by Herman Hesse THE SON OF THE BRAHMAN In the shade of the house, in the sunshine of the riverbank near the boats, in the shade of the Salwood forest, in the shade of the fig tree is where Siddhartha grew up, the handsome son of the Brahman, the NORMAL 30% 3:1 Similar to the previous sequence, while paging upward the cursor remains at the bottom of the screen until it approaches the top of the buffer.
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