如何在gedit中獲得“重複行”熱鍵?

如何在gedit中獲得“重複行”熱鍵?

我想要一個快捷鍵來複製 gedit 中目前選取的行。許多其他編輯器使用Ctrl+DCtrl++來Shift實現D此目的,但 gedit 不同。

這裡是預設行為:

  • Ctrl+ D: 刪除一行
  • Ctrl+ Shift+ D: 開啟 GTK 檢查器

我對目前的兩種行為都很好,只要另一個熱鍵能做我真正想做的事。

所以我看到了這個答案在顯示的地方,您實際上可以修補 gedit 二進位。但是我不想這樣做,因為修補二進位檔案可能是您可以做的最糟糕的解決方法(考慮更新和二進位更改)。此外,在該問題中,僅刪除了「刪除行」快捷方式,並使用外掛程式新增了「重複行」快捷方式,該外掛程式已不存在。

那我怎麼才能將「複製這一行」行為放入gedit呢?

答案1

插入評論中提到,另一個答案最近已更新,安裝後您應該能夠使用Ctrl+Shift+D複製一行或一個選擇。

我已經在 Ubuntu 16.04 上的 gedit 3.18.3 中測試了它,但它應該適用於任何版本>=3.14.0儘管這有點值得懷疑,因為 gedit 開發人員並不羞於在次要版本中引入重大更改(或者他們遵循除語意版本控制)並且似乎沒有關於插件開發的最新文件。

答案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。

相關內容