IPython Magic Commands in Jupyter Notebook (2026) - Part 5 | Step-by-Step Guide
Magic Commands¶
In Jupyter Notebook, Magic Commands are special built-in commands that provide additional functionality beyond normal Python commands.
They help users perform tasks such as file handling, timing code execution, managing variables, and controlling the notebook environment more easily.
Magic commands usually start with % for line magics and %% for cell magics.
They are designed to make working in Jupyter Notebook faster and more interactive.
1. %dirs¶
Shows the current directory stack (folders navigated using pushd/popd).
Useful for tracking directory navigation history in Jupyter.
%dirs
[]
%pushd
['~\\Projects\\Magic-Commands']
%popd
C:\Users\schol\Projects\Magic-Commands popd -> ~\Projects\Magic-Commands
pwd
'C:\\Users\\schol\\Projects\\Magic-Commands'
2. %dhist¶
Displays history of all directories visited in the session.
Useful for quickly returning to previous working folders.
%dhist
Directory history (kept in _dh) 0: C:\Users\schol\Projects\Magic-Commands 1: C:\Users\schol\Projects\Magic-Commands
3. %pushd¶
Changes directory and saves the current one in a stack.
Useful for temporarily switching folders and coming back later.
%dirs
[]
%pushd C:\Users\schol\Projects
C:\Users\schol\Projects
['~\\Projects\\Magic-Commands']
%dirs
['~\\Projects\\Magic-Commands']
%pwd
'C:\\Users\\schol\\Projects'
4. %popd¶
Removes current directory from stack and returns to previous directory.
Useful for back-navigation in folders.
%dirs
['~\\Projects\\Magic-Commands']
%popd
C:\Users\schol\Projects\Magic-Commands popd -> ~\Projects\Magic-Commands
%dirs
[]
%pwd
'C:\\Users\\schol\\Projects\\Magic-Commands'
5. %recall¶
Recalls a previous command from history for editing or reuse.
Useful for quickly reusing past commands.
%history
%dirs %pushd %dirs %popd %dirs pwd %dhist %dirs %pushd C:\Users\schol\Projects %dirs %pwd %dirs %popd %dirs %pwd %history
%recall 1
%dirs
[]
6. %rerun¶
Re-executes a previous command exactly as it was.
Useful for repeating analysis or experiments.
%history
%dirs %pushd %dirs %popd %dirs pwd %dhist %dirs %pushd C:\Users\schol\Projects %dirs %pwd %dirs %popd %dirs %pwd %history %recall 1 %dirs %history
%rerun 1
=== Executing: === %dirs === Output: ===
[]
7. %tb¶
Displays full traceback of the last error.
Very useful for debugging exceptions in detail.
a=1
b=0
c=a/b
--------------------------------------------------------------------------- ZeroDivisionError Traceback (most recent call last) Cell In[21], line 3 1 a=1 2 b=0 ----> 3 c=a/b ZeroDivisionError: division by zero
%tb
--------------------------------------------------------------------------- ZeroDivisionError Traceback (most recent call last) Cell In[21], line 3 1 a=1 2 b=0 ----> 3 c=a/b ZeroDivisionError: division by zero
8. %xdel¶
Deletes a variable completely from memory and removes references.
Useful for freeing memory in large computations.
data=1
data
1
%xdel data
data
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[26], line 1 ----> 1 data NameError: name 'data' is not defined
9. %%markdown¶
Turns a cell into Markdown format for formatted text.
Useful for writing headings, notes, and documentation in notebooks.
%%markdown
# Hello World
Hello World¶
10. %%javascript¶
Runs JavaScript code inside a Jupyter Notebook cell.
Useful for UI effects or notebook customization.
Comments
Post a Comment