tour.md 27 KB

Introduction

Welcome to the quick tour of Elvish. This tour works best if you have used another shell or programming language before.

If you are familiar with traditional shells like Bash, the sections basic shell language and shell scripting commands can help you "translate" your knowledge into Elvish.

If you are mainly interested in using Elvish interactively, jump directly to interactive features.

Basic shell language

Many basic language features of Elvish are very familiar to traditional shells. A notable exception is control structures, covered below in the advanced language features section.

Comparison with bash

If you are familiar with bash, the following table shows some (rough) correspondence between Elvish and bash syntax:

Feature Elvish bash equivalent
Barewords echo foo
Single-quoted strings echo 'foo'
echo 'It''s good' echo 'It'\''s good'
Double-quoted strings echo "foo"
echo "foo\nbar" echo $'foo\nbar'
echo "foo: "$foo echo "foo: $foo"
Comments # comment
Line continuation echo foo ^
bar
echo foo \
bar
Brace expansion echo {foo bar}.txt echo {foo,bar}.txt
Wildcards echo *.?
echo **.go find . -name '*.go'
echo *.?[set:ch] echo *.[ch]
Tilde expansion echo ~/foo
Setting variables var foo = bar foo=bar
set foo = bar foo=bar
foo=bar cmd
Using variables echo $foo
echo $E:HOME echo $HOME
Redirections head -n10 < a.txt > b.txt
Byte pipelines head -n4 a.txt | grep x
Output capture ls -l (which elvish) ls -l $(which elvish)
Background jobs echo foo &
Command sequence a; b a && b

Barewords

Like traditional shells, unquoted words that don't contain special characters are treated as strings (such words are called barewords):

~> echo foobar
foobar
~> ls /
bin   dev  home  lib64       mnt  proc  run   srv  tmp  var
boot  etc  lib   lost+found  opt  root  sbin  sys  usr
~> vim a.c

This is one of the most distinctive syntactical features of shells; non-shell programming languages typically treat unquoted words as names of functions and variables.

Read the language reference on barewords to learn more.

Single-quoted strings

Like traditional shells, single-quoted strings expand nothing; every character represents itself (except the single quote itself):

~> echo 'hello\world$'
hello\world$

Like Plan 9 rc, or zsh with the RC_QUOTES option turned on, the single quote itself can be written by doubling it

~> echo 'it''s good'
it's good

Read the language reference on single-quoted strings to learn more.

Double-quoted strings

Like many non-shell programming languages and $'' in bash, double-quoted strings support C-like escape sequences (e.g. \n for newline):

~> echo "foo\nbar"
foo
bar

Unlike traditional shells, Elvish does not support interpolation inside double-quoted strings. Instead, you can just write multiple words together, and they will be concatenated:

~> var x = foo
~> put 'x is '$x
▶ 'x is foo'

Read the language reference on double-quoted strings to learn more.

Comments

Comments start with # and extend to the end of the line:

~> echo foo # this is a comment
foo

Line continuation

Line continuation in Elvish uses ^ instead of \:

~> echo foo ^
   bar
foo bar

Unlike traditional shells, line continuation is treated as whitespace. In Elvish, the following code outputs foo bar:

echo foo^
bar

However, in bash, the following code outputs foobar:

echo foo\
bar

Brace expansion

Brace expansions in Elvish work like in traditional shells, but use spaces instead of commas:

~> echo {foo bar}.txt
foo.txt bar.txt

The opening brace { must not be followed by a whitespace, to disambiguate from lambdas.

Note: commas might still work as a separator in Elvish's brace expansions, but it will eventually be deprecated and removed soon.

Read the language reference on braced lists to learn more.

Wildcards

The basic wildcard characters, * and ?, work like in traditional shells:

~> ls
bar.ch  d1  d2  d3  foo.c  foo.h  lorem.go  lorem.txt
~> echo *.?
foo.c foo.h

Elvish also supports **, which matches multiple path components:

~> find . -name '*.go'
./d1/a.go
./d2/b.go
./lorem.go
./d3/d4/c.go
~> echo **.go
d1/a.go d2/b.go d3/d4/c.go lorem.go

Character classes are a bit more verbose in Elvish. First, a character set is written like [set:ch], instead of just [ch]. Second, they don't appear on their own, but as a suffix to ?. For example, to match files ending in either .c or .h, use:

~> echo *.?[set:ch]
foo.c foo.h

A character set suffix can also be applied to *. For example, to match files who extension only contains c and h:

~> echo *.*[set:ch]
bar.ch foo.c foo.h

Read the language reference on wildcard expansion to learn more.

Tilde expansion

Tilde expansion works likes in traditional shells. Assuming that the home directory of the current user is /home/me, and the home directory of elf is /home/elf:

~> echo ~/foo
/home/me/foo
~> echo ~elf/foo
/home/elf/foo

Read the language reference on tilde expansion to learn more.

Setting variables

Variables are declared with the var command, and set with the set command:

~> var foo = bar
~> echo $foo
bar
~> set foo = quux
~> echo $foo
quux

The spaces around = are mandatory.

Unlike traditional shells, variables must be declared before they can be set; setting an undeclared variable results in an error.

Like traditional shells, Elvish supports setting a variable temporarily for the duration of a command, by prefixing the command with foo=bar. For example:

~> var foo = original
~> fn f { echo $foo }
~> foo=temporary f
temporary
~> echo $foo
original

Read the language reference on the var command, the set command and temporary assignments to learn more.

Using variables

Like traditional shells, using the value of a variable requires the $ prefix.

~> var foo = bar
~> echo $foo
bar

Unlike traditional shells, variables must be declared before being used; if the foo variable wasn't declared with var first, echo $foo results in an error.

Elvish does not perform $IFS splitting on variables, so $foo always evaluates to one value, even if it contains whitespaces and newlines. You never need to write echo "$foo" again. (And in fact, double-quoted strings do not support interpolation).

Also unlike traditional shells, environment variables in Elvish live in a separate E: namespace:

~> echo $E:HOME
/home/elf

Read the language reference on variables, variable use and special namespaces to learn more.

Redirections

Redirections in Elvish work like in traditional shells. For example, to save the first 10 lines of a.txt to a1.txt:

~> head -n10 < a.txt > a1.txt

Read the language reference on redirections to learn more.

Byte pipelines

UNIX pipelines in Elvish (called byte pipelines, to distinguish from value pipelines) work like in traditional shells. For example, to find occurrences of x in the first 4 lines of a.txt:

~> cat a.txt
foo
barx
lorem
quux
lux
nox
~> head -n4 a.txt | grep x
barx
quux

Read the language reference on pipelines to learn more.

Output capture

Output of commands can be captured and used as values with (). For example, the following command shows details of the elvish binary:

~> ls -l (which elvish)
-rwxr-xr-x 1 xiaq users 7813495 Mar  2 21:32 /home/xiaq/go/bin/elvish

Note: the same feature is usually known as command substitution in traditonal shells.

Unlike traditional shells, Elvish only splits the output on newlines, not any other whitespace characters.

Read the language reference on output capture to learn more.

Background jobs

Add & to the end of a pipeline to make it run in the background, similar to traditional shells:

~> echo foo &
foo
job echo foo & finished

Unlike traditional shells, the & character does not serve to separate commands. In bash you can write echo foo & echo bar; in Elvish you still need to terminate the first command with ; or newline: echo foo &; echo bar.

Read the language reference on background pipelines to learn more.

Command sequence

Join commands with a ; or newline to run them sequentially (insert a newline with Alt-Enter):

~> echo a; echo b
a
b
~> echo a
   echo b
a
b

In Elvish, when a command fails (e.g. when an external command exits with a non-zero status), execution gets terminated.

~> echo before; false; echo after
before
Exception: false exited with 1
[tty 2], line 1: echo before; false; echo after

In this aspect, Elvish's behavior is similar to joining all commands with && or setting set -e in traditional shells:

Advanced language features

Building on a core of familiar shell-like syntax, the Elvish language incorporates many advanced features that make it a modern dynamic programming language.

Value output

Like in traditional shells, commands in Elvish can output bytes. The echo command outputs bytes:

~> echo foo bar
foo bar

Additionally, commands can also output values. Values include not just strings, but also lambdas, numbers, lists and maps. The put command outputs values:

~> put foo [foo] [&foo=bar] { put foo }
▶ foo
▶ [foo]
▶ [&foo=bar]
▶ <closure 0xc000347500>

Many builtin commands output values. For example, string functions in the str: module outputs their results as values. This makes those functions work seamlessly with strings that contain newlines or even NUL bytes:

~> use str
~> str:join ',' ["foo\nbar" "lorem\x00ipsum"]
▶ "foo\nbar,lorem\x00ipsum"

Unlike most programming languages, Elvish commands don't have return values. Instead, they use the value output to "return" their results.

Read the reference for builtin commands to learn which commands work with value inputs and outputs. Among them, here are some general-purpose primitives:

Command Functionality
all Passes value inputs to value outputs
each Applies a function to all values from value input
put Writes arguments as value outputs
slurp Convert byte input to a single string in value output

Value pipelines

Pipelines work with value outputs too. When forming pipelines, a command that writes value outputs can be followed by a command that takes value inputs. For example, the each command takes value inputs, and applies a lambda to each one of them:

~> put foo bar | each {|x| echo 'I got '$x }
I got foo
I got bar

Read the language reference on pipelines to learn more about pipelines in general.

Value output capture

Output capture works with value output too. Capturing value outputs always recovers the exact values there were written. For example, the str:join command joins a list of strings with a separator, and its output can be captured and saved in a variable:

~> use str
~> var s = (str:join ',' ["foo\nbar" "lorem\x00ipsum"])
~> put $s
▶ "foo\nbar,lorem\x00ipsum"

Read the language reference on output capture to learn more.

Lists and maps

Lists look like [a b c], and maps look like [&key1=value1 &key2=value2]:

~> var li = [foo bar lorem ipsum]
~> put $li
▶ [foo bar lorem ipsum]
~> var map = [&k1=v2 &k2=v2]
~> put $map
▶ [&k1=v2 &k2=v2]

You can get elements of lists and maps by indexing them. Lists are zero-based and support slicing too:

~> put $li[0]
▶ foo
~> put $li[1..3]
▶ [bar lorem]
~> put $map[k1]
▶ v2

Read the language reference on lists and maps to learn more.

Numbers

Elvish has a number type. There is no dedicated syntax for it; instead, it can constructed using the num builtin:

~> num 1
▶ (num 1)
~> num 1e2
▶ (num 100)

Most arithmetic commands in Elvish support both typed numbers and strings that can be converted to numbers. They usually output typed numbers:

~> + 1 2
▶ (num 3)
~> use math
~> math:pow (num 10) 3
▶ (num 1000)

Note: The set of number types will likely expand in future.

Read the language reference on numbers and the reference for the math module to learn more.

Booleans

Elvish has two boolean values, $true and $false.

Read the language reference on booleans to learn more.

Options

Many Elvish commands take options, which look like map pairs (&key=value). For example, the echo command takes a sep option that can be used to override the default separator of space:

~> echo &sep=',' foo bar
foo,bar
~> echo &sep="\n" foo bar
foo
bar

Lambdas

Lambdas are first-class values in Elvish. They can be saved in variables, used as commands, passed to commands, and so on.

Lambdas can be written by enclosing its body with { and }:

~> var f = { echo "I'm a lambda" }
~> $f
I'm a lambda
~> put $f
▶ <closure 0xc000265bc0>
~> var g = (put $f)
~> $g
I'm a lambda

The opening brace { must be followed by some whitespace, to disambiguate from brace expansion.

Lambdas can take arguments and options, which can be written in a signature:

~> var f = {|a b &opt=default|
     echo "a = "$a
     echo "b = "$b
     echo "opt = "$opt
   }
~> $f foo bar
a = foo
b = bar
opt = default
~> $f foo bar &opt=option
a = foo
b = bar
opt = option

Read the language reference on functions to learn more about functions.

Control structures

Control structures in Elvish look very different from traditional shells. For example, this is how an if command looks:

~> if (eq (uname) Linux) { echo "You're on Linux" }
You're on Linux

The if command takes a conditional expression (an output capture in this case), and the body to execute as a lambda. Since lambdas allow internal newlines, you can also write it like this:

~> if (eq (uname) Linux) {
     echo "You're on Linux"
   }
You're on Linux

However, you must write the opening brace { on the same line as if. If you write it on a separate line, Elvish would parse it as two separate commands.

The for command looks like this:

~> for x [expressive versatile] {
     echo "Elvish is "$x
   }
Elvish is expressive
Elvish is versatile

Read the language reference on the if command, the for command, and additionally the while command to learn more.

Exceptions

Elvish uses exceptions to signal errors. For example, calling a function with the wrong number of arguments throws an exception:

~> var f = { echo foo } # doesn't take arguments
~> $f a b
Exception: arity mismatch: arguments here must be 0 values, but is 2 values
[tty 2], line 1: $f a b

Moreover, non-zero exits from external commands are also turned into exceptions:

~> false
Exception: false exited with 1
[tty 3], line 1: false

Exceptions can be caught using the try command:

~> try {
     false
   } except e {
     echo 'got an exception'
   }
got an exception

Read the language reference on the exception value type and the try command to learn more.

Namespaces and modules

The names of variables and functions can have namespaces prepended to their names. Namespaces always end with :.

The using variables section has already shown the E: namespace. Other namespaces can be added by importing modules with use. For example, the str: module provides string utilities:

~> use str
~> str:to-upper foo
▶ FOO

You can define your own modules by putting .elv files in ~/.config/elvish/lib (or ~\AppData\Roaming\elvish\lib). For example, to define a module called foo, put the following in foo.elv under the aforementioned directory:

fn f {
  echo 'in a function in foo'
}

This module can now be used like this:

~> use foo
~> foo:f
in a function in foo

Read the language reference on namespaces and modules to learn more.

External command support

As shown in examples above, Elvish supports calling external commands directly by writing their name. If an external command exits with a non-zero code, it throws an exception.

Unfortunately, many of the advanced language features are only available for internal commands and functions. For example:

  • They can only write byte output, not value output.

  • They only take string arguments; non-string arguments are implicitly coerced to strings.

  • They don't take options.

Read the language reference on ordinary commands to learn more about when Elvish decides that a command is an external command.

Interactive features

Read the API of the interactive editor to learn more about UI customization options.

Tab completion

Press Tab to start completion. For example, after typing vim and Space, press Tab to complete filenames:

@ttyshot learn/tour/completion

Basic operations should be quite intuitive:

  • To navigate the candidate list, use arrow keys ▲ ▼ ◀ ▶ or Tab and Shift-Tab.

  • To accept the selected candidate, press Enter.

  • To cancel, press Escape.

As indicated by the horizontal scrollbar, you can scroll to the right to find additional results that don't fit in the terminal.

You may have noticed that the cursor has moved to the right of "COMPLETING argument". This indicates that you can continue typing to filter candidates. For example, after typing .md, the UI looks like this:

@ttyshot learn/tour/completion-filter

Read the reference on completion API to learn how to program and customize tab completion.

Command history

Elvish has several UI features for working with command history.

History walking

Press ▲ to fetch the last command. This is called history walking mode:

@ttyshot learn/tour/history-walk

Press ▲ to go further back, ▼ to go forward, or Escape to cancel.

To restrict to commands that start with a prefix, simply type the prefix before pressing ▲. For example, to walk through commands starting with echo, type echo before pressing ▲:

@ttyshot learn/tour/history-walk-prefix

History listing

Press Ctrl-R to list the full command history:

@ttyshot learn/tour/history-list

Like in completion mode, type to filter the list, press ▲ and ▼ to navigate the list, Enter to insert the selected entry, or Escape to cancel.

Last command

Finally, Elvish has a last command mode dedicated to inserting parts of the last command. Press Alt-, to trigger it:

@ttyshot learn/tour/lastcmd

Directory history

Elvish remembers which directories you have visited. Press Ctrl-L to list visited directories. Use ▲ and ▼ to navigate the list, Enter to change to that directory, or Escape to cancel.

@ttyshot learn/tour/location

Type to filter:

@ttyshot learn/tour/location-filter

Navigation mode

Press Ctrl-N to start the builtin filesystem navigator.

@ttyshot learn/tour/navigation

Unlike other modes, the cursor stays in the main buffer in navigation mode. This allows you to continue typing commands; while doing that, you can press Enter to insert the selected filename. You can also press Alt-Enter to insert the filename without exiting navigation mode; this is useful when you want to insert multiple filenames.

Startup script

Elvish's interactive startup script is rc.elv. Non-interactive Elvish sessions do not have a startup script.

POSIX aliases

Elvish doesn't support POSIX aliases, but you can get a similar experience simply by defining functions:

fn ls {|@a| e:ls --color $@a }

The e: prefix (for "external") ensures that the external command named ls will be called. Otherwise this definition will result in infinite recursion.

Prompt customization

The left and right prompts can be customized by assigning functions to edit:prompt and edit:rprompt. The following example defines prompts similar to the default, but uses fancy Unicode.

@ttyshot learn/tour/unicode-prompts

The tilde-abbr command abbreviates home directory to a tilde. The constantly command returns a function that always writes the same value(s) to the value output. The styled command writes styled output.

Changing PATH

Another common task in the interactive startup script is to set the search path. You can do set the environment variable directly (all environment variables have a E: prefix):

set E:PATH = /opts/bin:/bin:/usr/bin

But it is usually nicer to set the $paths instead:

set paths = [/opts/bin /bin /usr/bin]

Shell scripting commands

Elvish has its own set of builtin commands. This section helps you find commands that correspond to commands in traditional shells.

command

To force Elvish to treat a command as an external command, prefix it with e:.

export

In Elvish, environment variables live in the E: namespace. There is no concept of exporting a variable to the environment; environment variables are always "exported" to child processes, and non-environment variables never are.

source

To build reusable libraries, use Elvish's module mechanism.

To execute a dynamic piece of code for side effect, use eval. If the code lives in a file, write eval (slurp < /path/to/file).

Due to Elvish's scoping rules, files executed using either of the mechanism above can't create new variables in the current namespace. For example, eval 'var foo = bar'; echo $foo won't work. However, the REPL's namespace can be manipulated with edit:add-var.

test

To test files, use commands in the path module.

To compare numbers, use number comparison commands.

To compare strings, use string comparison commands.

To perform boolean operations, use and, or or not. Note: and and or are part of the language rather than the builtin module, since they perform short-circuit evaluation and don't always evaluate all the arguments.

which

To check if an external command exists, use has-external.

To query the path of an external command, use search-external.