Skip to content

Commit 08ed206

Browse files
committed
Multi-line editing and other updates
1 parent 767baa8 commit 08ed206

File tree

3 files changed

+203
-154
lines changed

3 files changed

+203
-154
lines changed

book/line_editor.md

Lines changed: 201 additions & 154 deletions
Original file line numberDiff line numberDiff line change
@@ -1,124 +1,212 @@
11
# Reedline, Nu's Line Editor
22

3-
Nushell's line editor [Reedline](https://github.com/nushell/reedline) is a
4-
cross-platform line reader designed to be modular and flexible. The engine is
5-
in charge of controlling the command history, validations, completions, hints
6-
and screen paint.
3+
Nushell's line-editor [Reedline](https://github.com/nushell/reedline) is
4+
cross-platform and designed to be modular and flexible. The line-editor is
5+
in charge of controlling the command history, validations, completions, hints,
6+
screen paint, and more.
77

8-
## Configuration
8+
[[toc]]
99

10-
### Editing Mode
10+
## Multi-line Editing
1111

12-
Reedline allows you to edit text using two modes: vi and emacs. If not
13-
specified, the default edit mode is emacs mode. In order to select your
14-
favorite you need to modify your config file and write down your preferred
15-
mode.
12+
Reedline allows Nushell commandlines to extend across multiple lines. This can be accomplished using several methods:
1613

17-
For example:
14+
1. Pressing <kbd>Enter</kbd> when a bracketed expression is open.
15+
16+
For example:
17+
18+
```nu
19+
def my-command [] {
20+
```
21+
22+
Pressing <kbd>Enter </kbd> after the open-bracket will insert a newline. This will also occur with opening (and valid) `(` and `[` expressions.
23+
24+
This is commonly used to create blocks and closures (as above), but also list, record, and table literals:
25+
26+
```nu
27+
let file = {
28+
name: 'repos.sqlite'
29+
hash: 'b939a3fa4ca011ca1aa3548420e78cee'
30+
version: '1.4.2'
31+
}
32+
```
33+
34+
It can even be used to continue a single command across multiple lines:
35+
36+
```nu
37+
(
38+
ffmpeg
39+
-i input.mp4
40+
-vf "scale=1280:720,setsar=1:1"
41+
-b:v 1500k
42+
-preset veryfast
43+
-crf 23
44+
-c:a aac
45+
-b:a 192k
46+
-movflags +faststart
47+
-y
48+
output.mp4
49+
)
50+
```
51+
52+
2. Pressing <kbd>Enter</kbd> at the end of a line with a trailing pipe-symbol (`|`).
53+
54+
```nu
55+
ls |
56+
where name =~ '^[0-9]' | # filenames starting with a digit
57+
get name | # get the filenames
58+
mv ...$in ./backups/ # and move to backups folder
59+
```
60+
61+
3. Manually insert a newline using <kbd>Alt</kbd>+<kbd>Enter</kbd> or <kbd>Shift</kbd>+<kbd>Enter</kbd>.
62+
63+
This can be used to create a somewhat more readable version of the previous commandline:
64+
65+
```nu
66+
ls
67+
| where name =~ '^[0-9]' # filenames starting with a digit
68+
| get name # get the filenames
69+
| mv ...$in ./backups/ # and move to backups folder
70+
```
71+
72+
::: tip
73+
It's possible that one or both of these keybindings may be intercepted by the terminal application or window-manager. For instance, Windows Terminal (and most other terminal applications on Windows) assign <kbd>Alt</kbd>+<kbd>Enter</kbd> to expand the terminal to full-screen. If neither of the above keybindings work in your terminal, you can assign a different keybinding to:
74+
75+
```nu
76+
event: { edit: insertnewline }
77+
```
78+
79+
See [Keybindings](#keybindings) below for more details.
80+
81+
:::
82+
83+
4. Pressing <kbd>Ctrl</kbd>+<kbd>O</kbd> opens the current commandline in your editor. Saving the resulting file and exiting the editor will update the commandline with the results.
84+
85+
## Setting the Editing Mode
86+
87+
Reedline allows you to edit text using two modes — Vi and Emacs. If not
88+
specified, the default mode is Emacs. To change the mode, use the
89+
`edit_mode` setting.
1890

1991
```nu
20-
$env.config = {
21-
...
22-
edit_mode: emacs
23-
...
24-
}
92+
$env.config.edit_mode = 'vi'
2593
```
2694

27-
#### Default Keybindings
28-
29-
Each edit mode comes with the usual keybinding for vi and emacs text editing.
30-
31-
Emacs and Vi Insert keybindings
32-
33-
| Key | Event |
34-
| ----------- | --------------------- |
35-
| Esc | Esc |
36-
| Backspace | Backspace |
37-
| End | Move to end of line |
38-
| End | Complete history hint |
39-
| Home | Move to line start |
40-
| Ctr + c | Cancel current line |
41-
| Ctr + l | Clear screen |
42-
| Ctr + r | Search history |
43-
| Ctr + Right | Complete history word |
44-
| Ctr + Right | Move word right |
45-
| Ctr + Left | Move word left |
46-
| Up | Move menu up |
47-
| Up | Move up |
48-
| Down | Move menu down |
49-
| Down | Move down |
50-
| Left | Move menu left |
51-
| Left | Move left |
52-
| Right | History hint complete |
53-
| Right | Move menu right |
54-
| Right | Move right |
55-
| Ctr + b | Move menu left |
56-
| Ctr + b | Move left |
57-
| Ctr + f | History hint complete |
58-
| Ctr + f | Move menu right |
59-
| Ctr + f | Move right |
60-
| Ctr + p | Move menu up |
61-
| Ctr + p | Move up |
62-
| Ctr + n | Move menu down |
63-
| Ctr + n | Move down |
64-
65-
Vi Normal keybindings
66-
67-
| Key | Event |
68-
| ------- | ------------------- |
69-
| Ctr + c | Cancel current line |
70-
| Ctr + l | Clear screen |
71-
| Up | Move menu up |
72-
| Up | Move up |
73-
| Down | Move menu down |
74-
| Down | Move down |
75-
| Left | Move menu left |
76-
| Left | Move left |
77-
| Right | Move menu right |
78-
| Right | Move right |
79-
80-
Besides the previous keybindings, while in Vi normal mode you can use the classic
81-
vi mode of executing actions by selecting a motion or an action. The available
82-
options for the combinations are:
83-
84-
Vi Normal motions
85-
86-
| Key | motion |
87-
| --- | ----------------- |
88-
| w | Word |
89-
| 0 | Line start |
90-
| $ | Line end |
91-
| f | Right until char |
92-
| t | Right before char |
93-
| F | Left until char |
94-
| T | Left before char |
95-
96-
Vi Normal actions
97-
98-
| Key | action |
99-
| --- | ------------------------------- |
100-
| d | Delete |
101-
| p | Paste after |
102-
| P | Paste before |
103-
| h | Move left |
104-
| l | Move right |
105-
| j | Move down |
106-
| k | Move up |
107-
| w | Move word right |
108-
| b | Move word left |
109-
| i | Enter Vi insert at current char |
110-
| a | Enter Vi insert after char |
111-
| 0 | Move to start of line |
112-
| ^ | Move to start of line |
113-
| $ | Move to end of line |
114-
| u | Undo |
115-
| c | Change |
116-
| x | Delete char |
117-
| s | History search |
118-
| D | Delete to end |
119-
| A | Append to end |
120-
121-
### Command History
95+
This can be changed at the commandline or persisted in `config.nu`.
96+
97+
::: note
98+
Vi is a "modal" editor with "normal" mode and an "insert" mode. We recommend
99+
becoming familiar with these modes through the use of the Vim or Neovim editors
100+
before using Vi mode in Nushell. Each has a built-in tutorial covering the basics
101+
(and more) of modal editing.
102+
:::
103+
104+
## Default Keybindings
105+
106+
Each edit mode comes with common keybindings for Vi and Emacs text editing.
107+
108+
### Emacs and Vi-insert Keybindings
109+
110+
| Key | Event |
111+
| ------------------------------------------ | ----------------------------------- |
112+
| <kbd>Shift</kbd>+<kbd>Enter</kbd> | Insert newline |
113+
| <kbd>Alt</kbd>+<kbd>Enter</kbd> | Insert newline |
114+
| <kbd>Backspace</kbd> | Backspace |
115+
| <kbd>End</kbd> | Move to end of line |
116+
| <kbd>End</kbd> | Complete history hint |
117+
| <kbd>Home</kbd> | Move to line start |
118+
| <kbd>Ctrl</kbd>+<kbd>C</kbd> | Cancel current line |
119+
| <kbd>Ctrl</kbd>+<kbd>L</kbd> | Clear screen |
120+
| <kbd>Ctrl</kbd>+<kbd>R</kbd> | Search history |
121+
| <kbd>Ctrl</kbd>+<kbd>→</kbd> (Right Arrow) | Complete history word |
122+
| <kbd>Ctrl</kbd>+<kbd>→</kbd> (Right Arrow) | Move word right |
123+
| <kbd>Ctrl</kbd>+<kbd>←</kbd> (Left Arrow) | Move word left |
124+
| <kbd>↑</kbd> (Up Arrow) | Move up |
125+
| <kbd>Ctrl</kbd>+<kbd>P</kbd> | Move up |
126+
| <kbd>↑</kbd> (Up Arrow) | Move menu up |
127+
| <kbd>Ctrl</kbd>+<kbd>P</kbd> | Move menu up |
128+
| <kbd>↓</kbd> (Down Arrow) | Move down |
129+
| <kbd>Ctrl</kbd>+<kbd>N</kbd> | Move down |
130+
| <kbd>↓</kbd> (Down Arrow) | Move menu down |
131+
| <kbd>Ctrl</kbd>+<kbd>N</kbd> | Move menu down |
132+
| <kbd>←</kbd> (Left Arrow) | Move left |
133+
| <kbd>Ctrl</kbd>+<kbd>B</kbd> | Move left |
134+
| <kbd>←</kbd> (Left Arrow) | Move menu left |
135+
| <kbd>Ctrl</kbd>+<kbd>B</kbd> | Move menu left |
136+
| <kbd>→</kbd> (Right Arrow) | Move right |
137+
| <kbd>Ctrl</kbd>+<kbd>F</kbd> | Move right |
138+
| <kbd>→</kbd> (Right Arrow) | Move menu right |
139+
| <kbd>Ctrl</kbd>+<kbd>F</kbd> | Move menu right |
140+
| <kbd>→</kbd> (Right Arrow) | History-hint complete |
141+
| <kbd>Ctrl</kbd>+<kbd>F</kbd> | History-hint complete |
142+
| <kbd>Alt</kbd>+<kbd>F</kbd> | History-hint complete one word |
143+
| <kbd>Alt</kbd>+<kbd>←</kbd> (Left Arrow) | History-hint complete one word less |
144+
145+
### Vi-insert Keybindings
146+
147+
| Key | Event |
148+
| -------------- | ------------------------ |
149+
| <kbd>Esc</kbd> | Switch to Vi-normal mode |
150+
151+
### Vi-normal Keybindings
152+
153+
| Key | Event |
154+
| ------------------------------------------- | ------------------- |
155+
| <kbd>Ctrl</kbd>+<kbd>C</kbd> | Cancel current line |
156+
| <kbd>Ctrl</kbd>+<kbd>L</kbd> | Clear screen |
157+
| <kbd>↑</kbd> (Up Arrow) | Move menu up |
158+
| <kbd>↑</kbd> (Up Arrow) | Move up |
159+
| <kbd>↓</kbd> (Down Arrow) | Move menu down |
160+
| <kbd>↓</kbd> (Down Arrow) | Move down |
161+
| <kbd>←</kbd> (Left Arrow) | Move menu left |
162+
| <kbd>←</kbd> (Left Arrow) | Move left |
163+
| <kbd>→</kbd> (Right Arrow) | Move menu right |
164+
| <kbd>→</kbd> (Right Arrow) | Move right |
165+
| <kbd>Ctrl></kbd>+<kbd>→</kbd> (Right Arrow) | Move right one word |
166+
| <kbd>Ctrl></kbd>+<kbd>←</kbd> (Left Arrow) | Move left one word |
167+
168+
As with Vi, many motions and actions can be combined with an optional count in normal-mode. For example, <kbd>3</kbd><kbd>d</kbd><kbd>w</kbd> deletes the next three words.
169+
170+
### Vi-normal Motions
171+
172+
| Key | Motion |
173+
| -------------------------------------- | --------------------------------------------- |
174+
| <kbd>w</kbd> | Move to beginning of next word |
175+
| <kbd>e</kbd> | Move to end of current or next word |
176+
| <kbd>b</kbd> | Move to beginning of current or previous word |
177+
| <kbd>0</kbd> | Move to start of line |
178+
| <kbd>$</kbd> | Move to end of line |
179+
| <kbd>h</kbd> | Move left |
180+
| <kbd>l</kbd> | Move right |
181+
| <kbd>j</kbd> | Move down |
182+
| <kbd>k</kbd> | Move up |
183+
| <kbd>f</kbd>+\<char\> | Move right to \<char\> |
184+
| <kbd>t</kbd>+\<char\> | Move right to before \<char\> |
185+
| <kbd>Shift</kbd>+<kbd>F</kbd>+\<char\> | Move left to \<char\> |
186+
| <kbd>Shift</kbd>+<kbd>T</kbd>+\<char\> | Move left to after \<char\> |
187+
188+
### Vi-normal Actions
189+
190+
| Key | Action |
191+
| ----------------------------- | -------------------------------------------------- |
192+
| <kbd>d</kbd> | Delete |
193+
| <kbd>Shift</kbd>+<kbd>D</kbd> | Delete to end of line |
194+
| <kbd>p</kbd> | Paste after current character |
195+
| <kbd>Shift</kbd>+<kbd>P</kbd> | Paste before current character |
196+
| <kbd>i</kbd> | Enter Vi insert-mode (append) at current character |
197+
| <kbd>Shift</kbd>+<kbd>I</kbd> | Enter insert-mode at beginning of line |
198+
| <kbd>a</kbd> | Append after current character |
199+
| <kbd>Shift</kbd>+<kbd>A</kbd> | Append to end of line |
200+
| <kbd>0</kbd> | Move to start of line |
201+
| <kbd>^</kbd> | Move to start of line |
202+
| <kbd>$</kbd> | Move to end of line |
203+
| <kbd>c</kbd> | Change |
204+
| <kbd>r</kbd> | Replace |
205+
| <kbd>s</kbd> | Substitute character(s) |
206+
| <kbd>x</kbd> | Delete character |
207+
| <kbd>u</kbd> | Undo |
208+
209+
## Command History
122210

123211
As mentioned before, Reedline manages and stores all the commands that are
124212
edited and sent to Nushell. To configure the max number of records that
@@ -136,50 +224,9 @@ Reedline should store you will need to adjust this value in your config file:
136224
}
137225
```
138226

139-
### Customizing your Prompt
140-
141-
Reedline prompt is also highly customizable. In order to construct your perfect
142-
prompt, you could define the next environment variables in your config file:
143-
144-
```nu
145-
# Use nushell functions to define your right and left prompt
146-
def create_left_prompt [] {
147-
let path_segment = ($env.PWD)
148-
149-
$path_segment
150-
}
151-
152-
def create_right_prompt [] {
153-
let time_segment = ([
154-
(date now | format date '%m/%d/%Y %r')
155-
] | str join)
156-
157-
$time_segment
158-
}
159-
160-
$env.PROMPT_COMMAND = { create_left_prompt }
161-
$env.PROMPT_COMMAND_RIGHT = { create_right_prompt }
162-
```
163-
164-
::: tip
165-
You don't have to define the environment variables using Nushell
166-
functions. You can use simple strings to define them.
167-
:::
168-
169-
You can also customize the prompt indicator for the line editor by modifying
170-
the next env variables.
227+
## Customizing the Prompt
171228

172-
```nu
173-
$env.PROMPT_INDICATOR = "〉"
174-
$env.PROMPT_INDICATOR_VI_INSERT = ": "
175-
$env.PROMPT_INDICATOR_VI_NORMAL = "〉"
176-
$env.PROMPT_MULTILINE_INDICATOR = "::: "
177-
```
178-
179-
::: tip
180-
The prompt indicators are environment variables that represent the
181-
state of the prompt
182-
:::
229+
The Reedline prompt is configured using a number of environment variables. See [Prompt Configuration](./configuration.md#prompt-configuration) for details.
183230

184231
## Keybindings
185232

book/quick_tour.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ Nushell commands can extend across multiple lines for readability. The above is
155155
ls | sort-by size | reverse | first | get name | cp $in ~
156156
```
157157

158+
See Also: [Multi-line Editing](./line_editor.md#multi-line-editing)
158159
:::
159160

160161
The first three lines are the same commands we used in the second example above, so let's examine the last three:

book/thinking_in_nu.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ echo 50
162162
echo 60
163163
```
164164

165+
See Also: [Multi-line Editing](./line_editor.md#multi-line-editing)
165166
:::
166167

167168
In all of the above:

0 commit comments

Comments
 (0)