50

Basically I want to copy (Ctrl+C) only the code portions from multiple cells without also copying the output or the In[1]: and Out[1]:

What is the easiest way to do so?

Vic
  • 825
  • 2
  • 8
  • 12
  • 4
    Have you tried clicking one of the cells, and then shift + clicking another cell to get the cell range? I was able to successfully copy and paste those lines of code using this method – BenWS Sep 28 '17 at 00:58

9 Answers9

43

When you are on a cell in Command mode(blue color mode), simply press Shift + DownArrow or Shift + UpArrow to select multiple cells. Press ctrl + C. And that's it. You have copied your entire selected code at once. It doesn't affect whether you have cell outputs.

Command mode: The Jupyter Notebook has two different keyboard input modes. Edit mode allows you to type code or text into a cell and is indicated by a green cell border. Command mode binds the keyboard to notebook level commands and is indicated by a grey cell border with a blue left margin.

Lahiru Karunaratne
  • 2,020
  • 16
  • 18
  • 2
    Much easier to understand and follow - thanks for providing this alternative answer after two years! – Johannes Ranke May 05 '20 at 07:14
  • 3
    I tried this. Unfortunately when you do this, and paste them, they all get merged into one cell – KansaiRobot Sep 12 '20 at 02:21
  • Thank goodness for this answer -- I have wasted so much time copying/pasting individual cells. This should really be part of the standard documentation for notebooks. @KansaiRobot this does not merge cells for me (as of jupyter-notebook version 6.3). – eric May 01 '21 at 18:08
  • @KansaiRobot: See an answer below: Now the most important one: make sure the jupyter file you want to copy the cells into is ALSO in blue/command mode. If this is not the case, you will copy all the cells into a single cell. – M_D Jan 10 '23 at 15:51
7

In jupyter you can copy several cells or the content of one cell. If you follow @BenWS comment you can copy several cells, and if you do kernel > restart & clear outputs beforehand you woult not get the [out]. Shortcut is C for copy cell and V shift + V to paste below / above.

However if you intend to copy several cells content, you should merge then before by select them and shift + M and then you can copy paste with ctrl + C.

MCMZL
  • 1,078
  • 9
  • 21
  • 2
    it is difficult to understand what did you mean. Command mode? Or Edit mode? – doubts Jul 15 '18 at 20:09
  • All the shortcuts are in command mode, you can enter command mode with ESC. Except for the last one ctrl + c you have to be inside the cell – MCMZL Jul 16 '18 at 12:00
  • Shift + M will merge the cells you have selected. Should've at least given a heads up! Now I'm stuck with multiple merged cells that I need to break down again. – ahmadPH Aug 11 '22 at 16:58
  • @ahmadPH I mentioned it will merge them in my comment. You can always press Z in command mode if you want to revert – MCMZL Aug 12 '22 at 10:15
5

What worked for me is the following:

  1. update jupyter notebook within a cell using:

    pip install -U jupyter notebook

  2. go in command mode by clicking to the left of a cell. If you click inside of a cell, it will be green.

  3. Use shift+down/up to select the cells you want to copy and use ctrl+c

  4. Now the most important one: make sure the jupyter file you want to copy the cells into is ALSO in blue/command mode. If this is not the case, you will copy all the cells into a single cell.

Vega
  • 27,856
  • 27
  • 95
  • 103
deheerbeer
  • 51
  • 1
  • 1
3

In the latest version of JupyterLabs:

File > Export Notebook As > Executable Script

Gives you the code as a text file.

flying_fluid_four
  • 704
  • 1
  • 6
  • 14
1

Just do: File > Export Notebook As > Export Notebook to Asciidoc and it will be easy to copy paste. This is what an Asciidoc file looks like:

+*In[ ]:*+
[source, ipython3]
----
import pandas as pd

df = pd.read_csv("data/survey_results_public.csv")

df.tail(10)

df.shape

pd.set_option("display.max_columns", 85)
pd.set_option("display.max_rows", 85)

schema_df = pd.read_csv("data/survey_results_schema.csv")

schema_df.head(10)


----
0

Open notebook dir as project in PyCharm, and then open the wanted ipynb file, select and copy all the source code, past into notepad++, replace "\r\n#%%\r\n\r\n" by null with extended search mode.

Frank
  • 505
  • 5
  • 14
0

For jupyterlab after Shift + UpArrow or Shift + select with mouse on multiple cells. Right click on cells for copy(C) and paste(P).

0

For old Jupyter users File>Print Preview> ctrl + a

0

if you are using a jupyter notebook in VS Code, then click the three dots "..." in the top-right corner of your notebook,then select "Export" and choose "Python Script". VS Code will automatically create a new python file with all the code (and comments) in it. enter image description here

stas g
  • 1,503
  • 2
  • 10
  • 20