
Editing Code in the Standard REPL
Editing Code in the Standard REPL 관련


Most versions of the Python interpreter support code editing when running in interactive mode. These editing features include code history and basic code completion. If you usually work on Unix-like systems, such as Linux and macOS, then you may be familiar with some of these features, which are present in the Bash and Korn shells.
These editing features are implemented using the GNU Readline library, which supports several useful styles of editing.
Note
A quick way to check whether you have code editing features available in your current setup is to press Ctrl+P in the REPL’s prompt. This shortcut should show the last code instruction in your history. If nothing happens or ^P
appears on the screen, then the code editing features aren’t available. Otherwise, you’re ready to go!
In the following sections, you’ll learn how to use code history and code completion in a Python interactive session when you’re using the standard REPL.
Code History
The standard REPL logs a complete history of all the code that you’ve typed and run while working in interactive mode. This history is saved to a file called .python_history
, typically located in your home directory.
While in interactive mode, you can browse this history by using the arrow keys on your keyboard. With the Up key, you can go back in history. With Down, you can go forward in history.
Once you find the desired line of code, you can press Enter to confirm your selection and reuse the target instruction:
Note how the code in your REPL’s history immediately becomes available for reuse. To navigate the code history, use the Up and Down keys until you find the desired snippet.
The navigation starts from the end of the history file and goes up. Every time you press the Up key, you jump to the previous line of code in your history.
Once you find the line of code that you want to reuse or modify, press Enter to accept it. Remember that every line in your code history keeps the original indentation that you used when you first typed the code, which is convenient and saves time.
Code Completion
The standard REPL provides basic completion capabilities for variable, object, and module names. This feature is automatically enabled when you start an interactive session. To invoke the available code completions, you can type the starting characters of a given name and press the Tab key. This action triggers the completion search.
If the search finds a match, then the name will automatically be completed for you. If the search finds more than one match, then you’ll have to press Tab again to get the entire list of matching names. If nothing appears after pressing Tab twice, then your search didn’t find any result.
The code completion system looks at the following objects when running its search:
- Python keywords
- Built-in function, class, and object names
- Currently defined names, such as variables, functions, and classes
- Imported module and package names
Here’s an example of how the REPL’s code completion system works in practice:
When you type the first character or characters of your target name and press Enter, you get an automatic completion if the REPL finds a unique match. When the completion system finds more than one name that matches your search, you need to press Tab again to get the list of matching names.
You can type a few more characters and press Tab again, or you can just type the complete name or keyword.
When it comes to accessing attributes with dot notation, like in obj.attr
, the code completion system will suggest completions from the target object’s attributes and methods:
Once you type the name of an object followed by a dot, you can press Tab to quickly inspect the list of attributes and methods of that object. Then you can start typing the name of your target attribute or method and press Tab again for completion.
The standard REPL provides a rather limited set of code editing features. However, they can be pretty useful when you need to use an interactive session and don’t have a more advanced REPL. These code editing features can improve your productivity and make your coding experience more pleasant.
Useful Keyboard Shortcuts
Leaning useful keyboard shortcuts can significantly boost your productivity and efficiency when you’re working in a REPL session. For example, pressing Ctrl+C on the REPL’s primary or secondary prompt cancels the input and returns to the primary prompt:
print(
#
# KeyboardInterrupt
if True:
#
# KeyboardInterrupt
When you press Ctrl+C on the primary or secondary prompt, the interpreter raises a KeyboardInterrupt
exception and immediately returns to the primary prompt.
If you press Ctrl+C while some piece of code is running, then the interpreter raises a KeyboardInterrupt
exception, interrupting the code’s execution and returning to the primary prompt.
This latter behavior is useful when you launch a long-running task that you’re not willing to complete or when you accidentally run into an infinite loop:
while True:
print("Hello!")
#
# Hello!
# Hello!
# Hello!
# ^CTraceback (most recent call last):
# File "<stdin>", line 2, in <module>
# KeyboardInterrupt
# Hello!
This example presents an infinite while
loop. You can break the loop by pressing Ctrl+C on your keyboard. After this key combination, you’re again back to the primary prompt, and your REPL session is ready for new input.
The standard REPL provides many other interesting and useful keyboard shortcuts. The table below shows some of them:
Keyboard Shortcut | Description |
---|---|
Ctrl+L | Clears the screen, reprinting the current line at the top |
Ctrl+D | Exits the current REPL session |
Ctrl+C | Raises a KeyboardInterrupt exception and loops back to the primary prompt |
Ctrl+B | Moves the cursor back one character |
Ctrl+F | Moves the cursor forward one character |
Del or Backspace | Deletes the character to the right or left of the cursor, respectively |
Ctrl+D | Deletes the character underneath the cursor |
Ctrl+A | Moves the cursor to the start of the line |
Ctrl+E | Moves the cursor to the end of the line |
Ctrl+K | Kills, or deletes, the text from the current cursor position to the end of the line |
Ctrl+W | Kills from the cursor to the previous whitespace |
These keyboard shortcuts will help you be more proficient when typing and editing code in an interactive session. You’ll find a few more keyboard shortcuts in the official documentation of the GNU Readline library.