Python launches shortcut in GEdit

I need a shortcut for GEdit that will run the currently open .py file when I press, say, F5. I have a script that does this through an external terminal window, but I have more problems creating a version that uses an internal output window (shell output, I think), since I cannot find a good way to capture pyenv details from a ~ file. / bashrc. Work with pyenv is required.

Here is what I have through the GEdit External Tools plugin:


UNSOLVED: inner shell method:

I wanted to access the pyenv settings in ~. / Bashrc, so I tried these external script tools:

#!/bin/bash set +m bash -i python $GEDIT_DOCUMENTS_PATH 

This works (thanks -i), but it gives me the warning "bash: no job control in this shell". Running set + m should get rid of this message, but it is not.

So, I moved the appropriate material that I had at the end of ~ / .bashrc to this script, which is not perfect at all:

 #!/bin/bash export PYENV_ROOT="${HOME}/.pyenv" if [ -d "${PYENV_ROOT}" ]; then export PATH="${PYENV_ROOT}/bin:${PATH}" eval "$(pyenv init -)" fi export PYENV_VERSION=3.3.4 export LD_LIBRARY_PATH=~/.pyenv/versions/3.3.4/lib/python3.3/site-packages/PySide-1.2.1-py3.3.egg/PySide/ python $GEDIT_CURRENT_DOCUMENT_NAME 

Problems: this last block is terrible. It is simply copied from ~ / .bashrc, and it should even include PySide data that ~ / .bashrc should take care of. Also, for some reason, using this method always displays the first line of the .py file (say, import sys). Obviously, no input signal () can be set using this method, and output to the GEdit Embedded Terminal seems impossible. In addition, I cannot get rid of the “Finish” message, even using set + m or by running a command in a subshell.


SOLVED: external terminal window method:

 #!/bin/sh gnome-terminal -x $SHELL -ic "python $GEDIT_CURRENT_DOCUMENT_NAME; printf \"\nPress any key to continue.\"; read -n 1 -s" 

or, define a terminal profile called, say, Wait, which sets the Title and Command-> When the terminal exits: Keep the terminal open and do the following:

 #!/bin/sh gnome-terminal --profile=Wait -x $SHELL -ic "python $GEDIT_CURRENT_DOCUMENT_NAME; printf \"\nPress any key to continue.\"" 

This gives the message “status 0”, so another method is better. Both methods use an interactive shell to access ~ / .bashrc.

+7
python ubuntu gedit
source share
2 answers

Steps to add custom keyboard shortcuts and features to GEdit:

1) Open the "Manage external tools".

2) Add tool

3) Give the tool a name.

4) Enter this code:

 #!/bin/sh python $GEDIT_DOCUMENTS_PATH 

5) Give the shortcut as F5 by directly entering the F5 function key in the field.

To run the current file, first save it. Now you can see the result in the Shell Output window that appears when the command is executed either through F5 or manually clicking on the command.

Of course, you can change it to suit your needs.

+5
source share

I wanted the same. After reading your post replies and comments, I tried myself.

To run only an open document, open (I use gedit under Ubuntu 14.04.4 LTS) in Tools> Manage external tools, pressing "+" to add a new tool: and enter the script in the screen for the shell:

 #!/bin/sh # run the current document in python python $GEDIT_CURRENT_DOCUMENT_PATH 

$ GEDIT_DOCUMENTS_PATH will apply it to all folders in the current folder of your document, but you want to run only the current document, right? And other suggestions: I don’t understand why you need to make it unnecessarily complicated and why you need to ask again - because as soon as you press the key that you want this thing to be done, right?

I tried this myself and it works flawlessly.

0
source share

All Articles