Complete command from history in ipython - ipython

For reasons outside of my control I'm stuck using python 2.6.6 and IPython 0.10.2. I also normally use the tcsh shell, and have gotten quite used to completing a command from the history using <A-p> (i.e. pressing the ALT key and p). However, this doesn't work in IPython. I know I can press <C-r> and then start typing a command, but what inevitably is happening is that I start a command, press <A-p>, get a colon indicating some weird state, then exit out of that state, delete my command, press <C-r> then search for my command. It's getting rather irritating. Is there any way to make <A-p> complete my already started command by relying on the history?

Ouch, this is an old version of IPython, Python (and pip). The bad news is I don't have much experience with such an old version of IPython, the good new is; it was way simpler at that time.
Most of the shortcut and feature are provided using readline, and the python bindings of stdlib. Meaning that most likely what you are trying to configure is readline itself and not only IPython; so you can find more information on that outside of IPython !
The secret is to grep in the source-code for parse_and_bind, then you'll find the following example configuration, leading me to change the ~/.ipython/ipy_user_conf.py to be like so at around line 99 (all indented an extra 4 space to be in the main() function):
import readline
readline.parse_and_bind('set completion-query-items 1000')
readline.parse_and_bind('set page-completions no')
rlopts = """\
tab: complete
"\C-l": possible-completions
set show-all-if-ambiguous on
"\C-o": tab-insert
"\M-i": " "
"\M-o": "\d\d\d\d"
"\M-I": "\d\d\d\d"
"\C-r": reverse-search-history
"\C-s": forward-search-history
"\C-p": history-search-backward
"\C-n": history-search-forward
"\e[A": history-search-backward
"\e[B": history-search-forward
"\C-k": kill-line
"\C-u": unix-line-discard"""
for cmd in rlopts.split('\n'):
readline.parse_and_bind(cmd)
The repetition of commands make me think that what \C,\M or [e mean might be system dependant. I would bet on \C being Control, and \M being Meta (Alt, Opt), but at least one of these line did the trick for me (and also now tab allows to complete). See also man readline for the list of commands you can bind to what, and enjoy! Hoping you can upgrade to Python 3 and IPython 6 at some point.
[Edit]
See Eric Carlsen second comment under this answer for how it was resolved.

Related

How to execute a Sublime plugin command from the command line?

I use the plugin SimpleSession with Sublime Text 3 (but any plugin could be considered). If I save a session with multiple windows, this creates a .simplesession file. How can I open that session file just by clicking on the file? The goal is to avoid having to launch ST3 and use the Command Palette to run the "Load Session" command. Currently, clicking on the .simplesession file causes ST3 to open it as a regular file.
Sublime doesn't know that a simplesession file is important in any way, so double clicking on one is going to open it the same as Sublime would open any other file.
Since it's a plugin that created the file, that plugin is the only thing that knows that it's special and what to do with it. So what you really need is the way to tell the plugin to take the action for you.
All actions in Sublime (including things as simple as inserting text) are taken by executing a command. Here that would be a command in the plugin that created the file in question, which would tell it that you want to carry out the action you would normally take manually, such as loading a session.
To do that from within Sublime you'd do something like bind a keyboard key to the appropriate command, add it to a menu, the command palette, etc. If you want to take the action from outside of Sublime, then you need to communicate that command to Sublime in order to get it to execute.
In core Sublime you can do this by executing the subl program that ships with Sublime and tell it a plugin command that you would like to execute.
Although it's possible to do this, the solution provided here has the requirement that Sublime already be running due to technical limitations within Sublime itself, but more on that in a moment.
This answer will give you the information that you need to formulate the command line that you need to execute in order to get the plugin command to run and carry out the action that you desire.
If you want to run this command in response to double clicking a file of a particular type (here a simplesession file), how you do that is specific to the operating system and file browser that you're using, and is best asked as a separate question.
Assuming you instead want a level of integration where you just have a desktop shortcut, start menu entry, etc that does this, this is more straight forward because such a shortcut is really just a visual wrapper that executes a command of your choosing.
Again, how you would do that is different depending on your OS, but the important part is knowing what full command line you need to give to the shortcut to be able to run it, which is what this answer tells you how to construct.
Important Note: The specific package in your question implements a load_session command, which prompts you for the session to load from a list of sessions you've previously created.
This command doesn't take any argument that would tell it what session to load without asking you to pick one first. As a result, what you want isn't technically possible without more work because there's no way to directly tell the load_session command the file that you want to open.
In order to more fully automate things in this particular case, the underlying package needs to be modified. In particular either the load_session command would need an optional argument which, when given, would cause it to load that session without prompting first, or
a new command would need to be created to do the same thing.
If you're not comfortable or knowledgeable enough to make such modifications to the package directly, you need to either find someone that will do that for you or (even better) discuss it with the package author, since that is a feature that others would probably enjoy as well.
The first thing you need to know is, "What command in the plugin is the one that I need to execute to do what I want?". In some cases you may already know exactly what command you need to use because it's documented, or you have already made a custom key binding for it, and so on.
If you don't know the command you need to use, check the documentation on the package (if any) to see if it mentions them. In your particular case, the README on the package page specifically mentions a list of commands, of which load_session seems like the most appropriate fit.
Lacking any documentation, the next easiest thing to do would be to ask Sublime directly. To do this, select View > Show Console from the menu or press the keyboard shortcut associated with it, Ctrl+`. In the console that appears, enter the following command and press enter.
sublime.log_commands(True)
Now whenever you do anything, this console is going to show you exactly what command Sublime is executing, along with any arguments that it may be passing to the command. This remains in effect until you use the same command with False or restart Sublime.
With logging turned on, select the appropriate command from the command palette and see what the Console says.
For example, with this package installed, I get output like the following:
>>> sublime.log_commands(True)
command: show_overlay {"overlay": "command_palette"}
command: load_session
This is showing two commands; first I opened the command palette which uses the show_overlay command, and then I selected the SimpleSession: Load command, which is the load_session command with no arguments.
In order to get Sublime to execute the command from the command line, you use the --command command line argument to subl. So in order to get Sublime to run the load_session command, you can enter the following command in a command prompt/terminal in your OS. This is also the command you would set in your desktop shortcut.
subl --command "load_session"
This presumes that you've set up Sublime so that it's in the path (how you do that is OS specific). If running subl in a terminal gives you an error about a missing command, either add the Sublime install directory to the path or use a fully qualified file name in place of subl (e,g. "C:\Program Files\Sublime Text 3\subl" if you're on Windows); either requires you to know what location Sublime is installed in.
If you want to use a command that takes arguments you need to include the arguments in the command as well, in the same way as they were displayed in the console above.
It's important that the command name and the arguments all be considered one command line argument, which requires you to wrap the whole thing in quote characters, since otherwise the spaces will make it appear as multiple arguments.
If you forget this, Sublime will respond by opening files named after the different parts of the command and arguments that you tried to open under the mistaken belief that you're giving it files to open.
As a concrete example, to get Sublime to open the command palette from outside of Sublime, the command to do this would look like the following if you were on Linux/MacOS:
subl --command 'show_overlay {"overlay": "command_palette"}'
Note again that we are passing exactly what the console showed above, but the whole thing, command and arguments, are wrapped in single quotes so that the terminal knows that the entire value is one argument.
This makes things a little tricky on Windows, which doesn't allow single quotes. On that platform you need to use double quotes instead. This requires you to "quote" the internal double quotes with a leading \ character so that the command processor knows that they're part of the argument and not the double quote that ends the argument.
For the case of opening the command palette on Windows, the command thus looks like this:
subl --command "show_overlay {\"overlay\": \"command_palette\"}"
With this information in hand, you can set up something like a desktop shortcut to run the appropriate command, or potentially set up the file explorer that you're using to execute a command specifically when you double click on a file of your choosing.
Again, how you would do that is specific to the operating system that you're using, and so I'm not really covering that in depth here in this answer. Just keep in mind that regardless of the OS in question, the part that remains the same is that you need to use subl command like the above.
Now, in your particular case, if the package that you're using provided a command that would let it load the session directly without prompting you first, the command that you use would need to also include the name of the session file as one of the command arguments.
However, as I mentioned above, this package doesn't currently allow that at the moment.
Now, here is the GIANT CAVEAT with this whole thing; this only works if Sublime is already running.
The subl command talks to an existing running copy of Sublime and gives it commands to open a file, directory, run a command as we're doing here, and so on. If Sublime isn't already running, then subl will start Sublime first and then communicate these details to it.
Sublime starts and makes it's interface available to you to work right away, and then starts to load packages and plugins in the background. This is to get you in and working on your files without having to wait for all packages to load first.
An issue with this is that as soon as Sublime starts, subl passes off the appropriate commands and then quits, and since packages aren't loaded yet, the command that you want to execute doesn't exist yet (hasn't been loaded), so nothing actually happens.
Unfortunately there's not really a satisfactory way around this particular issue if you want to start Sublime and also execute commands.
A potential workaround would be use something like a script or batch file that would check to see if Sublime is already running, and if not Start it and delay a little bit to allow plugins to finish loading, then use subl to run the command.
However this would require you to basically guess how long it takes Sublime to finish loading, which is less than ideal.

Emacs: How to read command help just as one would read Man pages?

It's nice to run M-x man foo for command foo within Emacs. It's then possible to navigate easily within the man page and to find details quickly.
When the help of a command (such as git) produces limited output, one can just use a terminal instead of emacs.
But occasionally, a command (such as aws help—run in a terminal) produces extensive output. Yet the output is not compatible with the emacs Man mode. An option is to use M-x shell within emacs, but that will not display the page at once. It will report "WARNING: terminal is not fully functional" and require pressing a key endlessly until the complete help appears, or, for Emacs 25, "Could not find executable named 'groff'".
What is a good way to read long manual pages produced by commands within emacs?
I just ran into this exact problem a few days ago.
Type escape + ! then type (for example) “aws ec2 help”. This brings the help text into a new buffer called Shell Command Output, with all of the control-h characters, etc.
Switch to the new buffer with control-x then lowercase ‘o’ (for other buffer)
Type escape + lowercase ‘x’ to run an emacs function, then type ‘man’ and hit Enter. It will prompt for man page entry and default to EC2, just hit Enter. In my case, it displays an error in the status line, “error in process sentinel: Can’t find the EC2 manpage”.
However, the “man page” functions are now available, so now (in that buffer)you can type escape + x and run the function Man-fontify-manpage. The page should now look like a nice man page with bold, etc.
You can then rename the buffer (escape + x then something like ec2) so the buffer isn’t replaced if you run another shell command.
I you just want the output in a buffer, you can simply use:
M-! aws help RET
If you want the output in a shell buffer, you can do git help commit | cat (so no more "terminal is not fully functional").
Probably you can do M-! aws help | cat RET also. I do not have aws, but hopefully the piping will remove the escape characters if aws output formatting is done right. You should try also TERM=dumb aws help. Any command should know better than using fancy output when TERM is set to dumb. If aws is dumb that way itself, you could pipe its output to something that filters out control characters -- try this
For forcing man mode in an arbitrary buffer, M-x Man-mode (yes, uppercase). I am not sure if that will play nice with aws's output.
By the way, for git I suppose you know you can do man git-commit (or man git-any_git_command, in general), so you have a nice alternative to git help when using emacs (output of help and man page is the same).

IPython's history-search-backward not working as desired

IPython's history-search-backward feature is one of my favorite features. history-search-backward allows you to type part of a command and then search backward through your readline history for commands that began with that part of the command. By default (I believe) these are bound to UpArrow or Ctrl+P and DownArrow or Ctrl+N (for backward and forward respectively).
They are not working for me. Instead they just go linearly through my history instead of taking into account the characters I've already typed to (allgedly) filter my history.
I'm running IPython 0.13.2 (with Python 2 and 3) on Arch Linux from within XTerm.
If I hit Escape, Ctrl+P, then UpArrow and DownArrow work exactly as I want them to.
Additionally if I change my ipython_config.py to include
c.TerminalInteractiveShell.readline_parse_and_bind = ['"\\e[B": history-search-forward', '"\\e[A": history-search-backward']
then I can just do Escape UpArrow for the desired behaviour. (Here's the rest of my config file.)
Ctrl+V UpArrow produces ^[[A as I expect. I have the python readline library installed (which seems to fix common problems with macs running IPython).
I have these lines in my .bashrc
bind '"\e[A": history-search-backward'
bind '"\e[B": history-search-forward'
and they work exactly as I want them to within bash.
I have no idea what else to try next, so I've come here. Please help, I hope I've included enough information and done enough research.
The culprit was set keymap vi set in my .inputrc. I removed that and IPython history went back to what I expected. Thanks to #Thomas K!

Open new python shell on C-c C-c in python-mode.el

I have a small GTK python application that imports a package (Twisted) that may not be loaded twice.
If I run my application in emacs with python-mode.el and press C-c C-c, the application gets executed in a python shell window.
If I now close the application, the python shell stays up and running. If I now press C-c C-c again, emacs "reuses" the old python process and thus I run into problems because I'm installing a Twisted reactor twice.
Is it possible to have python-mode.el open a new shell window each time I execute a buffer?
python-mode.el comes with a command py-execute-buffer-dedicated,
opening a new and reserved process for it
In python.el, a new inferior process is launched in a new buffer if the python-buffer variable is set to nil. Therefore, it's possible to advise the python-send-buffer function to reset that variable to nil after every invocation, thereby forcing a new Python process to be executed for every subsequent python-send-buffer command. Something like the following should work:
(defadvice
python-send-buffer
(after python-send-buffer-new-proc activate)
(setq python-buffer nil))
(ad-activate python-send-buffer)
I know that your post was asking for help with python-mode.el, but I thought it might be helpful to mention this anyway, as I'd surprised if python-mode.el doesn't use a similar mechanism. If I have time, I'll try to look into it.
Edit: the python-mode.el package uses the command py-shell to initiate a new inferior Python process. I found a mailing list posting in which a user provides an ad hoc function that appears to do what you need.
By the way, it might be worth considering that trying to alter the default behavior of python-mode isn't the best approach to this problem. I don't know what your code does, and I'm not particularly familiar with Twisted, but it seems to me that experiencing major errors when evaluating your code a second time within the same session could be a sign of a more fundamental design problem. I fail to see how it could be a matter of multiple imports of the same module being the issue, as Python modules are only loaded once, with successive import statements having no effect (for that, an explicit reload or execfile() is required). If I'm completely off-base here, I apologize, but I felt this possibility might merit mention.

Would it be possible to jump between prev/next Terminal command prompts?

I'm using zsh in OS X Terminal.app and for quite a while, I've been longing for a way to jump back and forth between prev/next prompts in the terminal's output.
One convenience with this would be to be able to review (and track errors at) the end of each command's output; eg. when you building stuff from source with ./configure; make; make install. Note: I'm obviously not referring to jumping back and forth in the command-history, but for a way to take a peek at the endings of each command's output.
Has anyone heard of such functionality in the *nix (preferrably also Mac) world? Would it require some sort of OS-centric Terminal plugin, or can it be programmatically done via a shell script which can be tied to a keyboard shortcut? Maybe I'm the only one thinking about this? :)
Edit: Here's an example scenario: Let's say I want to compile and install some program (using standard ./configure && make && make install procedure) and after the make command, I run into some errors. Now, the way I understand it (I may be completely wrong), the crucial error causing the make command to fail usually shows up in the last line(s) in the output, no? Anyway, at this point, I might do something like cat INSTALL to read up on the INSTALL document to check whether there's something I've missed. NOW, if I want to go back to see what the error was, that caused my initial make command to fail, I then have to manually scroll up to that position again, since my cat INSTALL command printed a ton of text after it.
I don't know if this scenario is the most elucidative – but there are many other situations where I wish I could just "jump" back to previous prompt lines and check up on previous command output; whether it was a simple ls command, make, git status, or whatever it was – swapping positions in the window by means of using prompt lines as "bookmark" positions seems an interesting idea to me.
command + left or right goes between tabs in iterm. is this what you are asking?
Emacs has a shell-mode that runs a shell inside the Emacs editor, providing a rich environment of additional commands for navigating and working with shell commands. This includes commands for going to the previous/next prompt, and deleting the output from commands so you can "clean up" and issue another command.
If you aren't familiar with Emacs: to start a shell inside Emacs, run emacs from the shell, then type Esc-x (or Meta-x, if you have "Use option as meta key" enabled in Terminal > Preferences > Settings > [profile] > Keyboard). This will ask for a command to execute. Enter shell.
To see a list of commands you can use in Shell Mode, enter Control-h m. Here are the ones for moving the cursor to the previous/next prompt:
C-c C-n comint-next-prompt
C-c C-p comint-previous-prompt
These commands would also be useful:
C-c C-r comint-show-output
C-c C-o comint-delete-output