How to Quit Swift Repl Without Using Ctrl-D

How do I quit swift repl without using ctrl-d?

This answer complements Ankit Goel's correct :quit answer to also (1) provide an understanding of why the : is needed and (2) other options besides :quit.

Swift REPL works in conjuction with the LLDB debugger.

: is a REPL escape prefix to execute an LLDB command. From within REPL, :help will list the available LLDB commands.

Any of the following will quit both Swift REPL and subsequently LLDB with a single command line.

:exit
:quit
:q

One can also exit REPL into LLDB with just : and, then later quit (or exit) using the LLDB command directly.

sh$ swift
Welcome…
1> print(18 + 4)
22
2> :
(lldb) print "hello"
(String) $R0 = "hello"
(lldb) quit
sh$

Addendum: The LLDB command c or continue can be used to return to the Swift REPL environment.

Howto Exit swift-language terminal mode

Type control-d to exit REPL.

control-d is the end of file character and it's used by various command line tools to mark the end of user input.


Added by a later editor

You can also quit the Swift REPL by typing :quit, :q, or :exit

How to exit the elm repl?

To exit the elm repl you need to type :exit. You can also use Ctrl + d, but I prefer typed commands instead of multi-key holds. I found this answer here as part of the (unofficial) getting started with elm programming guide.

Is there a way to use ctrl-d as forward delete in Scala's REPL?

Here's a very minimal keybinding property file, including your desired ^D:

# CTRL-B: move to the previous character
2: PREV_CHAR

# CTRL-D: delete the previous character
4: DELETE_NEXT_CHAR

# CTRL-F: move to the next character
6: NEXT_CHAR

# BACKSPACE, CTRL-H: delete the previous character
# 8 is the ASCII code for backspace and therefor
# deleting the previous character
8: DELETE_PREV_CHAR

# TAB, CTRL-I: signal that console completion should be attempted
9: COMPLETE

# CTRL-J, CTRL-M: newline
10: NEWLINE

# ENTER: newline
13: NEWLINE

# CTRL-N: scroll to the next element in the history buffer
14: NEXT_HISTORY

# CTRL-P: scroll to the previous element in the history buffer
16: PREV_HISTORY

# CTRL-V: paste the contents of the clipboard (useful for Windows terminal)
22: PASTE

# DELETE, CTRL-?: delete the previous character
# 127 is the ASCII code for delete
127: DELETE_PREV_CHAR

Put it in a file, and call scala like this:

scala -Djline.keybindings=/path/to/keybindings.properties

Or pass it through JAVA_OPTS. You'll have to look up on the Internet what keybindings exist, and try :keybindings from Scala to see what are the defaults (it won't reflect your actual keybindings, though).

How do the Swift REPL and the swiftc compiler interpret the language differently?

In a REPL environment, you'd want to be able to redeclare variables, right? Otherwise, as you keep using the REPL, and you declare more and more variables, let constants, functions, classes, whatever, you'll run out of names to use!

For example, let's say you want to try out string interpolation:

let x = 10
print("x is \(x)!")

And then a while later, you learn about string concatenation, and you want to try that out as well. At this point, you'd want to be able to redeclare a let constant x, right?

let x = "A"
let y = "B"
print(x + y)

You could argue that you could use a or b, but as time goes on, you'll slowly run out of names. REPL is designed like this so that you don't have to restart the REPL very frequently.

So each time you submit something, previously declared symbols that are also declared in the submission will get overwritten. This is documented here.

swiftc has a completely different use case - you usually use it to compile larger programs, with more than just a few lines of code. In these situations, the global scope will have a lot less symbols, and redeclaring variables isn't really practical to implement, because the code execution isn't linear from top to bottom. There could be multiple files as well that talk to each other. How do you figure out what redeclares what? It just doesn't make sense to do this in anywhere other than a REPL.

Other REPLs have this feature too, like csharppad.com for C#, ghci for Haskell, and python, just to name a few. So it's really just a common thing you do when you implement REPLs, not something special to Swift.

In fact, you can reproduce the swiftc behaviour in a REPL by writing the two lines in a function, because now the two lines are in the same submission, and will not overwrite each other.

why can not i finished repl :pas model with ctrl + d

Not sure about the Scala REPL, but according to https://superuser.com/questions/291224/equivalent-to-d-in-bash-for-cmd-exe, the equivalent to CTRL+D in CMD is CTRL+Z (you might have to press ENTER afterwards)



Related Topics



Leave a reply



Submit