问题
When using other editors, most of them have a function about "new line". For example, the Sublime Text, use
Ctrl + enter
to enter new line directly whatever cursor is at end of line or not. After checked all the setting about spyder, have no idea about this.
Thus i want to add new feature about "new line" with
Ctrl + enter
To enter a new line without the cursor need to move to tail
Would you have some ideas to share me ? Thank you.
回答1:
I'm just a Spyder user, but yes you can add such a functionality yourself. For this you need to modify the file codeeditor.py
, which in Windows is under \spyderlib\widgets\sourcecode\codeeditor.py
.
WARNING: Backup the codeeditor.py file before modifying it.
What you need to do is to modify the keyPressEvent
function that starts at line 2129 (in my Spyder 2.3.1).
You'll see that one of the first conditionals takes care of enter/return keypresses: line 2139, if key in (Qt.Key_Enter, Qt.Key_Return):
The option ctrl+Enter
is already taken... so without messing too much with the options, I'm just going to put here an option for ctrl+shift+Enter
, which is free to use. Is this ok?
So what you want to do is to add, after the option if not shift and not ctrl:
, another option with shift and control pressed; i.e. in my case I would add this at line 2153, with indentation to match the if not shift and not ctrl:
line:
elif shift and ctrl: # Insert newline functionality
cursor = self.textCursor()
startpos = cursor.position() # Remember where the cursor is
self.stdkey_end(False, False) # Go to the end of the line
self.insert_text(self.get_line_separator()) # Add a newline
cursor.setPosition(startpos) # Go back to the initial position
self.setTextCursor(cursor)
Now close Spyder, and reopen it. Try the ctrl
+ shift
+ enter
combination in the editor, and you should get a new line just below the line you're in, as you wanted.
If you don't mind rebinding or eliminating the "Run cell" functionality in your own copy of Spyder, you could even play around and put this code under the elif ctrl:
condition, then you'd have the ctrl
+enter
binding just like in Sublime Text.
回答2:
If you want to continue writing in the new line (move the cursor there) instead of only adding a new line and keep the cursor where it is, just ignore the related lines in the accepted answer:
elif shift and ctrl: # Insert newline functionality
cursor = self.textCursor()
# startpos = cursor.position() # optional
self.stdkey_end(False, False) # Go to the end of the line
self.insert_text(self.get_line_separenter code hereator()) # Add a newline
# cursor.setPosition(startpos) # optional
# self.setTextCursor(cursor) # optional
I'am going to do a pull request on this, since I guess many others can profit from this keybinding as well. Thanks for your answer.
回答3:
A simple way to help you: use right shift key as End+Enter
First : sudo apt-get install xdotool
In settings, open keyboard and add a custom shortcut
name : New_line
command : xdotool key End KP_Enter
then click the 'disabled' and type R_shift key
Now you can use right shift to achieve what you want !
来源:https://stackoverflow.com/questions/28182566/how-to-add-new-line-keyboard-shortcuts-in-spyder