使用 SublimeText 将行移动到下一部分
Move line to next section with SublimeText
我正在寻找一种使用 SublimeText 将一行移动到 "next section" 的方法。如果它是文本,它可能在 "next paragraph" 中(见下文),如果它是 XML,它可能在 "next node" 中,如果它是代码,它可能在 "next code block" 中,等等。
目前我多次使用 CTRLSHIFTDOWN ARROW 将其移动到下一节,但我认为 SublimeText 可能具有开箱即用的功能。
主要用例示例:
§ion1
abcdef
ghijkl <--- if the cursor is in this line, pressing CTRL+DOWN would move this line...
sqfdsdfqsdfjq
§ion2 <--- ...here! just between "§ion2" and "Lorem".
Lorem ipsum dolor sit amet
&somethingelse <--- one more CTRL+DOWN would move it here
tempor incididunt ut labore et dolore magna aliqua
t enim ad minim veniam
quis nostrud exercitation
我终于设法为此写了一个插件:
# Put this file in "C:\Users\User\AppData\Roaming\Sublime Text 2\Packages\User\"
# Add this to "C:\Users\User\AppData\Roaming\Sublime Text 2\Packages\User\Default (Windows).sublime-keymap":
# { "keys": ["ctrl+up"], "command": "movesection", "args": {"direction": "up"}},
# { "keys": ["ctrl+down"], "command": "movesection", "args": {"direction": "down"}}
import sublime, sublime_plugin
import subprocess
class MovesectionCommand(sublime_plugin.TextCommand):
def run(self, edit, direction):
# select current line, save it and delete it
currentcursor = self.view.sel()[0].begin()
thisline = self.view.substr(self.view.full_line(currentcursor))
self.view.erase(edit, self.view.full_line(currentcursor))
# find the next/previous & and move there
if direction == 'down':
beg = self.view.sel()[0].begin()
end = self.view.size()
else:
beg = 0
end = self.view.sel()[0].begin()
contents = self.view.substr(sublime.Region(beg, end)) #
offset = contents.find('&') if direction == 'down' else contents.rfind('&', 0, contents.rfind('&')) # down: first occurence, up: second-to-last occurence
cursors = self.view.sel()
cursors.clear()
location = sublime.Region(beg+offset, beg+offset)
cursors.add(location)
# move to the next line
(row, col) = self.view.rowcol(self.view.sel()[0].begin()) # go to the next line
self.view.run_command("goto_line", {"line": row+2})
# insert the text here
self.view.insert(edit, self.view.sel()[0].begin(), thisline)
# move to the line currently pasted (because of inserting the line usually including \n, it would go to next line, thus the following code is needed)
(row, col) = self.view.rowcol(self.view.sel()[0].begin())
self.view.run_command("goto_line", {"line": row})
我正在寻找一种使用 SublimeText 将一行移动到 "next section" 的方法。如果它是文本,它可能在 "next paragraph" 中(见下文),如果它是 XML,它可能在 "next node" 中,如果它是代码,它可能在 "next code block" 中,等等。
目前我多次使用 CTRLSHIFTDOWN ARROW 将其移动到下一节,但我认为 SublimeText 可能具有开箱即用的功能。
主要用例示例:
§ion1
abcdef
ghijkl <--- if the cursor is in this line, pressing CTRL+DOWN would move this line...
sqfdsdfqsdfjq
§ion2 <--- ...here! just between "§ion2" and "Lorem".
Lorem ipsum dolor sit amet
&somethingelse <--- one more CTRL+DOWN would move it here
tempor incididunt ut labore et dolore magna aliqua
t enim ad minim veniam
quis nostrud exercitation
我终于设法为此写了一个插件:
# Put this file in "C:\Users\User\AppData\Roaming\Sublime Text 2\Packages\User\"
# Add this to "C:\Users\User\AppData\Roaming\Sublime Text 2\Packages\User\Default (Windows).sublime-keymap":
# { "keys": ["ctrl+up"], "command": "movesection", "args": {"direction": "up"}},
# { "keys": ["ctrl+down"], "command": "movesection", "args": {"direction": "down"}}
import sublime, sublime_plugin
import subprocess
class MovesectionCommand(sublime_plugin.TextCommand):
def run(self, edit, direction):
# select current line, save it and delete it
currentcursor = self.view.sel()[0].begin()
thisline = self.view.substr(self.view.full_line(currentcursor))
self.view.erase(edit, self.view.full_line(currentcursor))
# find the next/previous & and move there
if direction == 'down':
beg = self.view.sel()[0].begin()
end = self.view.size()
else:
beg = 0
end = self.view.sel()[0].begin()
contents = self.view.substr(sublime.Region(beg, end)) #
offset = contents.find('&') if direction == 'down' else contents.rfind('&', 0, contents.rfind('&')) # down: first occurence, up: second-to-last occurence
cursors = self.view.sel()
cursors.clear()
location = sublime.Region(beg+offset, beg+offset)
cursors.add(location)
# move to the next line
(row, col) = self.view.rowcol(self.view.sel()[0].begin()) # go to the next line
self.view.run_command("goto_line", {"line": row+2})
# insert the text here
self.view.insert(edit, self.view.sel()[0].begin(), thisline)
# move to the line currently pasted (because of inserting the line usually including \n, it would go to next line, thus the following code is needed)
(row, col) = self.view.rowcol(self.view.sel()[0].begin())
self.view.run_command("goto_line", {"line": row})