Julia – 87 – Interagire con Julia – 1

Continuo da qui, copio qui.

Julia comes with a full-featured interactive command-line REPL (read-eval-print loop) built into the julia executable. In addition to allowing quick and easy evaluation of Julia statements, it has a searchable history, tab-completion, many helpful keybindings, and dedicated help and shell modes. The REPL can be started by simply calling julia with no arguments or double-clicking on the executable:

To exit the interactive session, type ^D – the control key together with the d key on a blank line – or type quit() followed by the return or enter key. The REPL greets you with a banner and a julia> prompt.

Io ho l’alias con l’opzione -q per non visualizzare il banner.

I differenti tipi di prompt

il modo Julian
The REPL has four main modes of operation. The first and most common is the Julian prompt. It is the default mode of operation; each new line initially starts with julia>. It is here that you can enter Julia expressions. Hitting return or enter after a complete expression has been entered will evaluate the entry and show the result of the last expression.

There are a number useful features unique to interactive work. In addition to showing the result, the REPL also binds the result to the variable ans. A trailing semicolon on the line can be used as a flag to suppress showing the result.

In Julian mode, the REPL supports something called prompt pasting. This activates when pasting text that starts with julia> into the REPL. In that case, only expressions starting with julia> are parsed, others are removed. This makes it is possible to paste a chunk of code that has been copied from a REPL session without having to scrub away prompts and outputs. This feature is enabled by default but can be disabled or enabled at will with Base.REPL.enable_promptpaste(::Bool). If it is enabled, you can try it out by pasting the code block above this paragraph straight into the REPL. This feature does not work on the standard Windows command prompt due to its limitation at detecting when a paste occurs.

modo Help
When the cursor is at the beginning of the line, the prompt can be changed to a help mode by typing ?. Julia will attempt to print help or documentation for anything entered in help mode:

Macros, types and variables can also be queried:

Help mode can be exited by pressing backspace at the beginning of the line.

modo Shell
Just as help mode is useful for quick access to documentation, another common task is to use the system shell to execute system commands. Just as ? entered help mode when at the beginning of the line, a semicolon (;) will enter the shell mode. And it can be exited by pressing backspace at the beginning of the line.

modi Search
In all of the above modes, the executed lines get saved to a history file, which can be searched. To initiate an incremental search through the previous history, type ^R – the control key together with the r key. The prompt will change to (reverse-i-search)`':, and as you type the search query will appear in the quotes. The most recent result that matches the query will dynamically update to the right of the colon as more is typed. To find an older result using the same query, simply type ^R again.

con qualche ^R, notare che l’history è memorizzata, attraverso le sessioni.

Just as ^R is a reverse search, ^S is a forward search, with the prompt (i-search)`':. The two may be used in conjunction with each other to move through the previous or next matching results, respectively.

Key bindings
The Julia REPL makes great use of key bindings. Several control-key bindings were already introduced above (^D to exit, ^R and ^S for searching), but there are many more. In addition to the control-key, there are also meta-key bindings. These vary more by platform, but most terminals default to using alt- or option- held down with a key to send the meta-key (or can be configured to do so).

Keybinding         Description
Program control  
^D                 Exit (when buffer is empty)
^C                 Interrupt or cancel
^L                 Clear console screen
Return/Enter, ^J   New line, executing if it is complete
meta-Return/Enter  Insert new line without executing it
? or ;             Enter help or shell mode (when at start of a line)
^R, ^S             Incremental history search, described above

Cursor movement  
Right arrow, ^F    Move right one character
Left arrow, ^B     Move left one character
Home, ^A           Move to beginning of line
End, ^E            Move to end of line
^P                 Change to the previous or next history entry
^N                 Change to the next history entry
Up arrow           Move up one line (or to the previous history entry)
Down arrow         Move down one line (or to the next history entry)
Page-up            Change to the previous history entry that matches the
                   text before the cursor
Page-down          Change to the next history entry that matches the text 
                   before the cursor
meta-F             Move right one word
meta-B             Move left one word

Backspace, ^H      Delete the previous character
Delete, ^D         Forward delete one character (when buffer has text)
meta-Backspace     Delete the previous word
meta-D             Forward delete the next word
^W                 Delete previous text up to the nearest whitespace
^K                 "Kill" to end of line, placing the text in a buffer
^Y                 "Yank" insert the text from the kill buffer
^T                 Transpose the characters about the cursor
^Q                 Write a number in REPL and press ^Q to open editor at
                   corresponding stackframe (da me non fa nulla)

Personalizzare le keybindings
Julia’s REPL keybindings may be fully customized to a user’s preferences by passing a dictionary to REPL.setup_interface(). The keys of this dictionary may be characters or strings. The key '*' refers to the default action. Control plus character x bindings are indicated with "^x". Meta plus x can be written "\\Mx". The values of the custom keymap must be nothing (indicating that the input should be ignored) or functions that accept the signature (PromptState, AbstractREPL, Char). The REPL.setup_interface() function must be called before the REPL is initialized, by registering the operation with atreplinit(). For example, to bind the up and down arrow keys to move through history without prefix search, one could put the following code in .juliarc.jl:

import Base: LineEdit, REPL

const mykeys = Dict{Any,Any}(
    # Up Arrow
    "\e[A" => (s,o...)->(LineEdit.edit_move_up(s) || 
         LineEdit.history_prev(s, LineEdit.mode(s).hist)),
    # Down Arrow
    "\e[B" => (s,o...)->(LineEdit.edit_move_up(s) || 
         LineEdit.history_next(s, LineEdit.mode(s).hist))

function customize_keys(repl)
    repl.interface = REPL.setup_interface(repl; extra_repl_keymap = mykeys)


Users should refer to base/LineEdit.jl to discover the available actions on key input.

No, non ci provo 😐


Post a comment or leave a trackback: Trackback URL.



Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:

Logo WordPress.com

Stai commentando usando il tuo account WordPress.com. Chiudi sessione / Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione / Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione / Modifica )

Google+ photo

Stai commentando usando il tuo account Google+. Chiudi sessione / Modifica )

Connessione a %s...

%d blogger hanno fatto clic su Mi Piace per questo: