MCPcopy Create free account
hub / github.com/ipython/ipython / history

Method history

IPython/core/magics/history.py:99–219  ·  view source on GitHub ↗

Print input history (_i variables), with most recent last. By default, input history is printed without line numbers so it can be directly pasted into an editor. Use -n to show them. By default, all input history from the current session is displayed. Ranges of h

(self, parameter_s = '')

Source from the content-addressed store, hash-verified

97 @skip_doctest
98 @line_magic
99 def history(self, parameter_s = ''):
100 """Print input history (_i<n> variables), with most recent last.
101
102 By default, input history is printed without line numbers so it can be
103 directly pasted into an editor. Use -n to show them.
104
105 By default, all input history from the current session is displayed.
106 Ranges of history can be indicated using the syntax:
107
108 ``4``
109 Line 4, current session
110 ``4-6``
111 Lines 4-6, current session
112 ``243/1-5``
113 Lines 1-5, session 243
114 ``~2/7``
115 Line 7, session 2 before current
116 ``~8/1-~6/5``
117 From the first line of 8 sessions ago, to the fifth line of 6
118 sessions ago.
119
120 Multiple ranges can be entered, separated by spaces
121
122 The same syntax is used by %macro, %save, %edit, %rerun
123
124 Examples
125 --------
126 ::
127
128 In [6]: %history -n 4-6
129 4:a = 12
130 5:print a**2
131 6:%history -n 4-6
132
133 """
134
135 args = parse_argstring(self.history, parameter_s)
136
137 # For brevity
138 history_manager = self.shell.history_manager
139
140 def _format_lineno(session, line):
141 """Helper function to format line numbers properly."""
142 if session in (0, history_manager.session_number):
143 return str(line)
144 return "%s/%s" % (session, line)
145
146 # Check if output to specific file was requested.
147 outfname = args.filename
148 if not outfname:
149 outfile = sys.stdout # default
150 # We don't want to close stdout at the end!
151 close_at_end = False
152 else:
153 if os.path.exists(outfname):
154 try:
155 ans = io.ask_yes_no("File %r exists. Overwrite?" % outfname)
156 except StdinNotImplementedError:

Callers

nothing calls this directly

Calls 8

parse_argstringFunction · 0.90
_format_linenoFunction · 0.85
ask_yes_noMethod · 0.80
searchMethod · 0.45
get_tailMethod · 0.45
get_range_by_strMethod · 0.45
get_rangeMethod · 0.45
closeMethod · 0.45

Tested by

no test coverage detected