Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiline paste in jshell

I was trying out jshell and couldn't find option to paste multiple line expressions. Is it even possible to paste multiple lines in jshell. Similar to what scala offers with paste mode.

like image 640
Kunal Kanojia Avatar asked Jan 24 '17 16:01

Kunal Kanojia


People also ask

How do I paste a multiline statement into the REPL?

Before entering your multiline statement, type the :paste command into the REPL: When you do this, the REPL prompts you to paste in your command — your multiline expression — and then press [Ctrl] [D] at the end of your command. In this example I paste in my complete multiline if statement, and then press [Ctrl] [D]:

How do I paste multiple lines in a text file?

As for pasting multiple lines: well, you can paste multiple lines, but it only seems to evaluate the first one. What you can instead do is nest those multiple lines in a function, and then paste it.

How do I paste a multi line statement in Scala?

Before entering your multiline statement, type the :paste command into the REPL: scala> :paste // Entering paste mode (ctrl-D to finish) When you do this, the REPL prompts you to paste in your command — your multiline expression — and then press [Ctrl][D] at the end of your command.

Is there a way to paste multiple lines in ActivePython?

I found ActivePython lacking in features (dedent region, indent region, comment region, uncomment region) yet containing no new (useful) ones. As for pasting multiple lines: well, you can paste multiple lines, but it only seems to evaluate the first one. What you can instead do is nest those multiple lines in a function, and then paste it.


2 Answers

So if you have code like this:

 int c = 2;
 int j = 4;
 int x = 5; 

Copy and paste into jshell, only the first two statements are processed.

But if you have code like this:

  int c = 2; int j = 4; int x = 5;

And paste into jshell:

jshell> int c = 2; int j = 4; int x = 5;
        c ==> 2
        j ==> 4
        x ==> 5 

Even more lines of code like this:

HashMap<Integer, Integer> map2 = new HashMap<>(); for (int i = 0; i < 15; ++i) { map2.put(i, i);map2.put(i, i); } System.out.println(map2);

will actually work.

Why? Me don't know.

The only way I know that copy/paste will work is via (type it in jshell) :

/edit

and you can paste as much as you want.

like image 59
Eugene Avatar answered Oct 13 '22 23:10

Eugene


Just in case people still end up here, a small tweak to paste in an entire code block to jshell is to wrap it around with braces {} as:

{
 int c = 2;
 int j = 4;
 int x = 5; 
 // access these only in this scope though
 System.out.println("c : " + c + ", j : " + j + ", x = " + x);
}

Sample screen:

enter image description here

like image 32
Naman Avatar answered Oct 13 '22 23:10

Naman