如何在 spyder 中添加 "new line" 键盘快捷键
How to add "new line" keyboard shortcuts in spyder
在使用其他编辑器时,大多都有关于“换行”的功能。
比如Sublime Text,使用
Ctrl + enter
无论光标是否在行尾,都可以直接输入新行。
查了spyder的所有设置后,对这个一无所知
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
你有什么想法可以分享给我吗?谢谢。
我只是一个 Spyder 用户,但是是的,您可以自己添加这样的功能。为此,您需要修改文件 codeeditor.py
,该文件在 Windows 中位于 \spyderlib\widgets\sourcecode\codeeditor.py
.
下
警告:在修改 codeeditor.py 文件之前备份它。
你需要做的是修改从第2129行开始的keyPressEvent
函数(在我的Spyder 2.3.1中)。
您会看到第一个条件语句处理 enter/return 按键:第 2139 行,if key in (Qt.Key_Enter, Qt.Key_Return):
选项 ctrl+Enter
已经被采用...所以在不对选项进行过多改动的情况下,我将在这里放置一个 ctrl+shift+Enter
的选项,它可以免费使用。这个可以吗?
所以你想要做的是添加,在选项if not shift and not ctrl:
之后,另一个按下shift和control的选项;即在我的例子中,我会在第 2153 行添加它,缩进与 if not shift and not ctrl:
行相匹配:
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)
现在关闭 Spyder,然后重新打开它。在编辑器中尝试 ctrl
+ shift
+ enter
组合,你应该会在你所在的行下方得到一个新行,如你所愿。
如果您不介意在您自己的 Spyder 副本中重新绑定或删除 "Run cell" 功能,您甚至可以尝试将此代码置于 elif ctrl:
条件下,那么您会具有 ctrl
+enter
绑定,就像在 Sublime Text 中一样。
如果您想继续在新行中写入(将光标移到那里)而不是只添加新行并将光标保持在原处,只需忽略已接受答案中的相关行:
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
我将对此进行拉取请求,因为我猜很多其他人也可以从这个键绑定中获益。谢谢你的回答。
一个简单的方法来帮助你:
使用右移键作为 End+Enter
首先:sudo apt-get install xdotool
在设置中,打开键盘并添加自定义快捷键
姓名:New_line
command : xdotool key End KP_Enter
然后单击 'disabled' 并键入 R_shift 键
现在您可以使用右移来实现您想要的!
在使用其他编辑器时,大多都有关于“换行”的功能。 比如Sublime Text,使用
Ctrl + enter
无论光标是否在行尾,都可以直接输入新行。 查了spyder的所有设置后,对这个一无所知
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
你有什么想法可以分享给我吗?谢谢。
我只是一个 Spyder 用户,但是是的,您可以自己添加这样的功能。为此,您需要修改文件 codeeditor.py
,该文件在 Windows 中位于 \spyderlib\widgets\sourcecode\codeeditor.py
.
警告:在修改 codeeditor.py 文件之前备份它。
你需要做的是修改从第2129行开始的keyPressEvent
函数(在我的Spyder 2.3.1中)。
您会看到第一个条件语句处理 enter/return 按键:第 2139 行,if key in (Qt.Key_Enter, Qt.Key_Return):
选项 ctrl+Enter
已经被采用...所以在不对选项进行过多改动的情况下,我将在这里放置一个 ctrl+shift+Enter
的选项,它可以免费使用。这个可以吗?
所以你想要做的是添加,在选项if not shift and not ctrl:
之后,另一个按下shift和control的选项;即在我的例子中,我会在第 2153 行添加它,缩进与 if not shift and not ctrl:
行相匹配:
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)
现在关闭 Spyder,然后重新打开它。在编辑器中尝试 ctrl
+ shift
+ enter
组合,你应该会在你所在的行下方得到一个新行,如你所愿。
如果您不介意在您自己的 Spyder 副本中重新绑定或删除 "Run cell" 功能,您甚至可以尝试将此代码置于 elif ctrl:
条件下,那么您会具有 ctrl
+enter
绑定,就像在 Sublime Text 中一样。
如果您想继续在新行中写入(将光标移到那里)而不是只添加新行并将光标保持在原处,只需忽略已接受答案中的相关行:
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
我将对此进行拉取请求,因为我猜很多其他人也可以从这个键绑定中获益。谢谢你的回答。
一个简单的方法来帮助你: 使用右移键作为 End+Enter
首先:sudo apt-get install xdotool
在设置中,打开键盘并添加自定义快捷键
姓名:New_line
command : xdotool key End KP_Enter
然后单击 'disabled' 并键入 R_shift 键
现在您可以使用右移来实现您想要的!