IPython Magic Commands in Jupyter Notebook (2026) - Part 9 | 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. %copy¶
%copy is an IPython alias that executes the operating system's copy command.
It is used to create a duplicate of a file from one location to another.
%copy test\copy.py
1 file(s) copied.
2. %ddir¶
%ddir is an IPython alias that displays the directories (folders) in the current location.
It is commonly mapped to the Windows command dir /ad /on.
It helps users view and navigate available folders in a directory.
%ddir
Volume in drive C is Windows-SSD
Volume Serial Number is 565A-BBE5
Directory of C:\Users\schol\Projects\Magic-Commands
06/25/2026 01:23 <DIR> .
05/25/2026 16:01 <DIR> ..
06/25/2026 01:16 <DIR> .ipynb_checkpoints
06/01/2026 14:58 <DIR> __pycache__
06/25/2026 01:14 <DIR> Test
0 File(s) 0 bytes
5 Dir(s) 133,328,834,560 bytes free
3. %doctest_mode¶
In Jupyter Notebook/IPython, %doctest_mode enables or disables doctest-friendly behavior.
Adjusts prompts and output formatting to match Python documentation examples.
Useful when creating or testing doctest examples.
%doctest_mode
Exception reporting mode: Plain Doctest mode is: ON
4. %echo¶
In IPython, %echo is a magic command used to display text on the screen.
Similar to the shell echo command.
Useful for printing messages without using print().
%echo Hello World
Hello World
5. %killbgscripts¶
In IPython, %killbgscripts is a magic command used to stop all background scripts started with %%script.
Helps manage long-running background tasks.
Frees system resources by terminating background jobs.
%killbgscripts
All background processes were killed.
6. %ldir¶
In IPython, %ldir is a magic command similar to dir().
Lists attributes of an object in a more readable, line-by-line format.
Useful for exploring object methods and properties.
%ldir
Volume in drive C is Windows-SSD
Volume Serial Number is 565A-BBE5
Directory of C:\Users\schol\Projects\Magic-Commands
06/25/2026 01:23 <DIR> .
05/25/2026 16:01 <DIR> ..
06/25/2026 01:16 <DIR> .ipynb_checkpoints
06/01/2026 14:58 <DIR> __pycache__
06/25/2026 01:14 <DIR> Test
0 File(s) 0 bytes
5 Dir(s) 133,328,965,632 bytes free
7. %less¶
In IPython, %less is a magic command used to display the contents of a file using a pager.
Similar to the Unix/Linux less command.
Useful for viewing large files without opening an editor.
%less demo.py
print("Hello")
Comments
Post a Comment