我想要一个快捷键来复制 gedit 中当前选定的行。许多其他编辑器使用Ctrl+D或Ctrl++来Shift实现D此目的,但 gedit 不同。
这里是默认行为:
- Ctrl+ D: 删除一行
- Ctrl+ Shift+ D: 打开 GTK 检查器
我对当前的两种行为都很好,只要另一个热键能做我真正想做的事情。
所以我看到了这个答案在显示的地方,您实际上可以修补 gedit 二进制文件。但是我不想这样做,因为修补二进制文件可能是您可以做的最糟糕的解决方法(考虑更新和二进制更改)。此外,在该问题中,仅删除了“删除行”快捷方式,并使用插件添加了“重复行”快捷方式,该插件已不存在。
那么我怎样才能将“复制这一行”行为放入gedit中呢?
答案1
答案2
你还在寻找答案吗?我想我有正确的,虽然我不确定,因为我不熟悉Python。
1.您应该编辑duplicateline.py文件gedit3 插件这边走:
import gettext
from gi.repository import GObject, Gtk, Gio, Gedit
ACCELERATOR = ['<Alt>d']
#class DuplicateLineWindowActivatable(GObject.Object, Gedit.WindowActivatable):
class DuplicateLineAppActivatable(GObject.Object, Gedit.AppActivatable):
__gtype_name__ = "DuplicateLineWindowActivatable"
app = GObject.Property(type=Gedit.App)
def __init__(self):
GObject.Object.__init__(self)
def do_activate(self):
#self._insert_menu()
self.app.set_accels_for_action("win.duplicate", ACCELERATOR)
self.menu_ext = self.extend_menu("tools-section")
item = Gio.MenuItem.new(_("Duplicate Line"), "win.duplicate")
self.menu_ext.prepend_menu_item(item)
def do_deactivate(self):
#self._remove_menu()
self.app.set_accels_for_action("win.duplicate", [])
self.menu_ext = None
#self._action_group = None
#def _insert_menu(self):
#manager = self.window.get_ui_manager()
# Add our menu action and set ctrl+shift+D to activate.
#self._action_group = Gtk.ActionGroup("DuplicateLinePluginActions")
#self._action_group.add_actions([(
#"DuplicateLine",
#None,
#_("Duplicate Line"),
#"d",
#_("Duplicate current line, current selection or selected lines"),
#self.on_duplicate_line_activate
#)])
#manager.insert_action_group(self._action_group, -1)
#self._ui_id = manager.add_ui_from_string(ui_str)
#def _remove_menu(self):
#manager = self.window.get_ui_manager()
#manager.remove_ui(self._ui_id)
#manager.remove_action_group(self._action_group)
#manager.ensure_update()
def do_update_state(self):
#self._action_group.set_sensitive(self.window.get_active_document() != None)
pass
class DuplicateLineWindowActivatable(GObject.Object, Gedit.WindowActivatable):
window = GObject.property(type=Gedit.Window)
def __init__(self):
GObject.Object.__init__(self)
self.settings = Gio.Settings.new("org.gnome.gedit.preferences.editor")
def do_activate(self):
action = Gio.SimpleAction(name="duplicate")
action.connect('activate', self.on_duplicate_line_activate)
self.window.add_action(action)
def on_duplicate_line_activate(self, action, user_data=None):
doc = self.window.get_active_document()
if not doc:
return
if doc.get_has_selection():
# User has text selected, get bounds.
s, e = doc.get_selection_bounds()
l1 = s.get_line()
l2 = e.get_line()
if l1 != l2:
# Multi-lines selected. Grab the text, insert.
s.set_line_offset(0)
e.set_line_offset(e.get_chars_in_line())
text = doc.get_text(s, e, False)
if text[-1:] != '\n':
# Text doesn't have a new line at the end. Add one for the beginning of the next.
text = "\n" + text
doc.insert(e, text)
else:
# Same line selected. Grab the text, insert on same line after selection.
text = doc.get_text(s, e, False)
doc.move_mark_by_name("selection_bound", s)
doc.insert(e, text)
else:
# No selection made. Grab the current line the cursor is on, insert on new line.
s = doc.get_iter_at_mark(doc.get_insert())
e = doc.get_iter_at_mark(doc.get_insert())
s.set_line_offset(0)
if not e.ends_line():
e.forward_to_line_end()
text = "\n" + doc.get_text(s, e, False)
doc.insert(e, text)
2. Alt+D复制一行。您可以更改热键 - 根据需要编辑 3d 行“ACCELERATOR = ['<Alt>d']”。
3. 至少,它适用于 gedit v. 3.14.3。