-
Notifications
You must be signed in to change notification settings - Fork 9
Tutorial
V is an evolution of vim, which is most definitely not a programming language. It's an editor. Because of this, programming in V will be unlike any other language you have learned before. There are some terminologies you'll need to learn (buffer, cursor, movements, operators, modes etc.), and using it can be confusing or difficult at times. However, it's also extremely fun to program in.
If you are unfamiliar with vim, the best way to get started learning V is by learning vim. If you are on OS X or Linux, it is probably already installed for you. If you are on Windows, you can install it from here. Once you have vim installed, type vimtutor
at the commandline to start learning vim. This is a 20 minute lesson, and goes over the basics of editing text in the vim editor.
If you are familiar with vim, the best way to start learning V is to go over the official list of commands.
If you would rather not install or learn vim, you may also go over this tutorial, which may not be as complete as vimtutor, but will approach things in a more code-golf oriented way.
#1.0, The Basics
First, let's talk about how V takes input and output. It's really quite simple. At the start of a V program, all input is loaded into the buffer, which is really just something that holds text. Every character is treated exactly the same, so newlines/quote characters, whitespace etc. take up exactly the same space as any other character. Because of this, V is a 2D language; if the input is two lines, two lines are loaded into the buffer.
During the program, most of the commands will cause the text in the buffer to change in someway.
Then, when the program is done running, all of the text in the buffer is printed. This leads to one of the cool properties of V, and our first program: The cat program. In V, the empty program is a cat program. All input is implicitly sent to the buffer, nothing changes the buffer, and then everything in the buffer is implicitly printed.
#1.1, Normal and Insert mode
Vim (and by extension, V) has four modes: Normal, Insert, Command, and Visual. What this means, is that every character does something different depending on what mode V is currently in. To start with, we'll focus on the two modes that are most useful and important: Normal and Insert. Normal mode is the default. When the program starts it is in normal mode, most of the powerful commands are in normal mode, and before the program ends V implicitly leaves whatever mode it is in to return to normal mode. In normal mode, every keystroke is a command of sorts, whether it's a motion, an operator, or a key that sends you to a new mode. Once you enter insert mode, these keys stop being commands, and start being characters. In fact, in insert mode every single printable ASCII character is just that, a character. This allows us to write things into the buffer using insert mode.
Let's try it out. To enter insert mode from normal, we will use the i
command (Mnemonic: insert). So the following program will print "Hello world!"
iHello world!
While 'i' is the primary command used to enter insert mode, there are many others. For example, any one of these characters will cause you to enter insert mode, but with different side effects:
i " insert
I " INSERT
a " append
A " APPEND
o " open
O " OPEN
C " CHANGE (to end of line)
s " Substitute (current character)
S " SUBSTITUTE (whole line)
R " REPLACE mode
Try playing around with them and some input to see how they affect the location of the inserted text!
This leads to the next thing you need to know about V: The cursor. Try changing the following program from iHello
to aHello
and then AHello
and watch what changes. You'll notice that the location of the text being entered varies; 'i' puts it at the beginning, 'a' puts it after the first input character, and 'A' puts it after all of the input characters. This is because V has a cursor, which is literally exactly the same thing as the cursor you are used to. It defines where text is entered, and where normal mode commands have their affects. But there is one important note:
In Normal mode, the cursor is on a character, in Insert mode the cursor is between characters.
For example, if the text was like this:
Hello, World!
Cursor: ^
Then the cursor is literally on top of the 'r' character. Normal mode commands will start their affects on the 'r' character. (Unless it's a linewise command, but we'll get to that later. One thing at a time!) But if we press 'i', the cursor will be where the bar is:
Hello, Wo|rld!
And if we had pressed 'a', the cursor would be here:
Hello, Wor|ld!
And if we had pressed 'A', the cursor would be here:
Hello, World!|
For completeness sake, the 'I' command would put the cursor here:
|Hello, World!
'I' puts the cursor on the first non-whitespace character, and then enters insert mode. So if there was leading whitespace...
Four leading spaces!
Cursor: ^
Pressing 'I' would put the cursor here:
|Four leading spaces!
#1.2, Motions
Some Normal-mode commands can move the location of the cursor without having any effect on the text in the buffer. These are called "motions". The most basic motions are 'h', 'j', 'k', and 'l'. These correspond to:
h " Left
j " Down
k " Up
l " Right
This might seem like a very strange choice of keys. But there is actually historical precendence for using these particular keys as cursor keys! This question on the Vi/Vim stack exchange covers it very nicely.