Issue
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.
Solution
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.
Answered By - Roberto
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.