To merge cells in Jupyter Notebook, select the adjacent cells you want to combine, then choose the merge command from the Edit menu. In classic Notebook, you can usually press Shift+M while the selected cells are in command mode. The result is one cell containing their source; check its cell type and content before running it.
Merging is useful when related code or Markdown has been split across several cells and you want it together. It does not join separate notebooks, combine spreadsheet-style table cells, or guarantee that code will run correctly after the merge. The steps below cover classic Notebook, JupyterLab, and the newer Notebook interface, whose menus can vary slightly by version.
What You’ll Need
- A notebook open in a browser, using classic Jupyter Notebook, JupyterLab, or Notebook 7.
- At least two adjacent cells to merge. The command works on a consecutive selection, not on scattered cells.
- Any important code or Markdown saved. If the notebook has unsaved changes, save before you begin.
- For code cells, a working kernel is helpful if you plan to run the merged result. You can merge cells without starting a kernel.
Step-by-Step
- Open the notebook and locate the cells. Find the consecutive cells that belong together. For example, you might have an import statement in one code cell and a related setup statement in the next, or a Markdown explanation split into two cells. Cells that are not adjacent cannot be merged in one operation; move them next to each other first if needed, or merge them in separate operations.
- Save a checkpoint before changing the layout. Use File > Save Notebook, or the save button, before merging. This is especially useful if the cells contain substantial code or carefully formatted Markdown. A merge changes the notebook source and may be inconvenient to reverse after you make other edits. You can often undo immediately with Ctrl+Z on Windows or Linux, or Command+Z on Mac, but do not rely on undo as your only backup.
- Switch to command mode. In classic Notebook, press Esc or click the cell’s outer border so the cell is selected rather than being edited. A selected cell has a border or highlight; an active cell in edit mode shows a text cursor inside its source. In JupyterLab, click the cell itself, outside its text editor, to select it. If a cursor is still visible inside the source, press Esc before selecting the next cell.
- Select the full consecutive range. Click the first cell, then hold Shift and click the last cell in the range if your interface supports range selection. You can also select a cell and extend the selection with the keyboard: in classic Notebook, use the command-mode cell navigation keys and their Shift combinations. In JupyterLab, use the cell selection controls or keyboard navigation available in your version. Confirm that every cell you mean to include is highlighted. Selecting only one cell, or selecting non-adjacent cells, will not produce the intended result.
- Use the merge command in the menu. In classic Notebook, open Edit > Merge Cells. In JupyterLab, open Edit > Merge Selected Cells. Notebook 7 uses a JupyterLab-style interface, but the exact wording may differ by release. If you do not see a merge option, check that multiple cells are selected and look in the Edit menu again. The menu is the most dependable method because it does not depend on a keyboard shortcut being enabled.
- Optionally use the keyboard shortcut. In classic Notebook, select the cells in command mode and press Shift+M. The shortcut is available in many JupyterLab setups too, but keyboard bindings can be customized, so use the Edit menu if it does nothing. On Mac, Shift+M means hold Shift and press M; there is no Command-key replacement for this shortcut. Do not type the shortcut while the cursor is active inside a cell, because it may insert a character rather than run the command.
- Inspect the merged cell before running it. The selected source is combined into one cell, generally in the order the cells appeared from top to bottom. Check that statements or paragraphs have not run together in a confusing way. Code lines normally retain line breaks, but review indentation, comments, and blank lines. With a mixed selection of code and Markdown, inspect the resulting cell type and content carefully; the first selected cell’s type commonly determines how the merged content is treated, and the exact behavior can depend on the interface and version.
- Run or render the result only after checking it. For a code cell, press Shift+Enter to run it and move to the next cell, or use the Run button. For Markdown, press Shift+Enter to render it. If the merged cell is not the type you intended, select it and change its type using the cell-type control in the toolbar, or use the Cell menu’s cell-type choices where available. Before executing merged code, verify that combining the statements has not changed their meaning.
- Save again and check the notebook around it. Use File > Save Notebook after confirming the result. Look at the cells immediately before and after the merge to ensure the notebook still reads in the intended order. If the merged cell behaves incorrectly, use undo immediately if available, or restore the saved version and repeat the operation with a smaller selection.
Common Mistakes
Trying to merge while editing the source. A cursor inside a cell means you are editing its contents, not selecting cells as notebook objects. Press Esc, select the cells, and then choose the merge command. This is a common reason Shift+M appears to do nothing or adds an unexpected character.
Selecting the wrong range. A highlighted cell can be easy to miss, particularly in a long notebook. Check both ends of the selection before merging. If you accidentally include an unrelated cell between two intended cells, that content will be included too. Jupyter merges a consecutive block; it does not skip intervening cells.
Combining code and Markdown without checking the type. A Markdown heading inserted into a code cell is not a Python comment, and Python code placed into Markdown will display as text rather than execute. Keep different kinds of content separate unless you deliberately plan to edit the merged source and set the final cell type yourself.
Expecting outputs to become source code. A cell’s output, such as a printed result, chart, or error message, is separate from its source. Merging cells does not turn displayed output into code or Markdown. Output handling can vary by interface and operation, so save important results separately and rerun the final code cell if you need fresh output.
Assuming a merge changes execution order or fixes dependencies. Cells run when you execute them, not simply because they appear together. A merged cell can still depend on variables created elsewhere, and running cells out of order can produce errors. Merging is an editing action, not a substitute for checking the notebook’s execution sequence.
Tips
Merge cells when their contents form one logical unit, such as a short function with its helper lines or a single Markdown section. Avoid making a very large cell just to reduce the number of visible cells. Smaller cells make it easier to run one step at a time, find errors, and explain the notebook’s workflow.
For code, look at the boundary between each pair of cells before merging. A newline between two statements is usually appropriate; two expressions placed on the same line may be invalid or may behave differently. Preserve blank lines between logical blocks, and make sure comments still describe the code that follows them. If a line uses continuation syntax, confirm that the combined indentation and punctuation remain valid Python.
For Markdown, inspect headings, lists, links, and code fences. A heading should start on its own line, and a list should retain its line breaks and indentation. If two Markdown cells each contain a fenced code example, check that every opening triple-backtick fence still has a matching closing fence after the merge. Press Shift+Enter to render the merged Markdown and catch formatting problems.
If your goal is to reorganize a notebook rather than create a single larger cell, consider moving cells instead. In Jupyter, cell movement commands or the notebook’s drag controls can change order without combining source. That keeps each code or explanation block independently runnable. If you only want to place a heading above code, a separate Markdown cell is often clearer than mixing both types.
Use the menu label shown by your own installation as the authority. Classic Notebook and JupyterLab share many concepts, but menu names, toolbar controls, and shortcuts can differ between releases or customized environments. If you work in a managed service, an administrator may also have changed keyboard bindings. The merge command in the Edit menu avoids most shortcut uncertainty.
Troubleshooting
The merge option is disabled or missing: Confirm that at least two adjacent cells are selected and that you are in command mode. If only one cell is selected, select a range. If the menu label is not obvious, inspect the Edit menu for Merge Cells or Merge Selected Cells. Some versions also provide a command search or palette; search for “merge cells” there.
Shift+M does not work: First press Esc, then try again with the intended cells selected. If the shortcut still has no effect, use the Edit menu. Your environment may use a different binding, or a browser extension or customized JupyterLab setting may intercept the keys. Check the keyboard shortcuts or settings for your particular interface rather than assuming the shortcut is universal.
The result contains unexpected text or errors: Review the merged source from top to bottom. Separate lines that became joined, restore missing indentation, and confirm the cell type. If code and Markdown were mixed, change the type or undo the merge and keep them separate. Run the cell only after reviewing it; a syntax error after merging usually comes from the combined source, not from the merge command itself.
The output disappeared or looks stale: Outputs are not a dependable record of a merged cell’s current behavior. Run the resulting code cell again to regenerate output, if it is safe to do so. If the notebook’s execution state matters, save important output or copy it elsewhere before editing, then check the result after running.
You merged too many cells: If you have not made further edits, use Edit > Undo or Ctrl+Z on Windows/Linux; use Command+Z on Mac where supported. If undo is no longer available, restore from the checkpoint or reopen the last saved version, then select a smaller consecutive range and merge again.
Frequently Asked Questions
Can I merge cells in Jupyter Notebook without a keyboard shortcut?
Yes. Select the adjacent cells and use the Edit menu’s merge command. The menu is a good choice when shortcuts are unfamiliar or have been changed.
Does merging cells combine their outputs?
No. A cell’s source and its displayed output are separate. Output may not be retained as you expect after editing, so rerun the merged code cell to generate current results.
Can I merge cells that are not next to each other?
Not directly in a single merge. Select a consecutive range; any cells between the first and last selected cells are included. Move cells first if you need a different order.
Can I merge a code cell with a Markdown cell?
You can select mixed cell types in some Jupyter interfaces, but the combined result may not work as intended. Check the resulting type and source, and usually keep executable code and explanatory Markdown in separate cells.
Will merging cells change the order in which they run?
No. It combines their source in the order they appear, but it does not automatically run the cell or resolve dependencies. Run the notebook in a sensible order and confirm the merged code works.
Is the procedure the same in JupyterLab and Notebook 7?
The basic process is the same: select cells and choose the merge command. Menu wording and shortcuts can differ by version, so use the Edit menu in your interface if the keyboard shortcut is unavailable.
Related
- Group Rows in Smartsheet: Sheets, Reports, and Views
- Numbers Text to Columns: Split Your Data Correctly
- Hayward Salt Cell Split Nut: Fit, Failure, and Replacement
Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.
More in How-to basics
- Insert a Personal Signature in Word Online: Steps and TipsYes. In Word Online, the most reliable way to insert a personal signature is to add it as…
- Google Sheets: Get a Percentage of a NumberTo get a percentage of a number in Google Sheets, multiply the number by the percentage: if A1…
- How to Make a Flowchart in Word Easily, Step by StepTo make a flowchart in Microsoft Word, choose Insert > Shapes, select a shape from the Flowchart section,…
- Google Sheets Filter by Color: Steps and LimitationsYes. In Google Sheets in a web browser, create a filter, open the filter menu on the column…
- Delete Blank Rows in Excel: Keyboard Shortcut GuideTo delete blank rows quickly in Excel for Microsoft 365 on Windows, select a column that should contain…
- Create an Outlook Distribution List From a CSVTo create an Outlook distribution list from a CSV, first import the CSV’s people as Outlook contacts, then…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.