Editing Code in the Standard REPL
00:00 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.
00:25 These editing features are implemented using the GNU Readline library, which supports several useful styles of editing. First, a quick way to check whether or not you have code editing features available in your current setup is to press Control + P in the REPL’s prompt.
00:43 This shortcut should show the last code instruction in your history. If nothing happens or ^P appears on the screen as seen here, then code editing features aren’t available. Otherwise, you are ready to go.
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.
01:41 Once you find the desired line of code, you can press Enter to confirm your selection and reuse the target instruction. 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.
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, as seen here with the
print() command. You can then complete the line as desired.
02:37 If the search finds more than one match, then you’ll have to press Tab again to get the entire list of matching names. The first time you press Tab, you may hear a beep, depending on your system.
03:15 The code completion system looks at these objects when running its search: Python keywords; built-in function, class, and object names; currently defined names, such as variables, functions, and classes; and imported module and package names.
04:40 These code editing features can improve your productivity and make your coding experience more pleasant. Learning keyboard shortcuts can significantly boost your productivity and efficiency when you’re working in a REPL session. For example, pressing Control + C on the REPL’s primary or secondary prompt cancels the input and returns to the primary prompt.
06:02 The standard REPL provides many other interesting and useful keyboard shortcuts, and the table on-screen shows some of them. These shortcuts will help you be more proficient when typing and editing code in an interactive session.
Become a Member to join the conversation.