كيف يمكنني مكررة خط كامل في Emacs ؟
سؤال
رأيت هذا السؤال نفسه على همة وقد تم شيء أنا نفسي أريد أن أعرف كيفية القيام به بالنسبة Emacs.في ReSharper أنا استخدم CTRL-D لهذا العمل.ما هو أقل عدد من الأوامر لتنفيذ هذه في Emacs ؟
المحلول
يمكنني استخدام
C-a C-SPACE C-n M-w C-y
التي ينهار
C-a
:تحريك المؤشر إلى بداية السطرC-SPACE
:تبدأ مجموعة ("تعيين علامة")C-n
:نقل المؤشر إلى السطر التاليM-w
:نسخ المنطقةC-y
:لصق ("نثر")
المذكور
C-a C-k C-k C-y C-y
يرقى إلى نفس الشيء (TMTOWTDI)
C-a
:تحريك المؤشر إلى بداية السطرC-k
:قطع ("قتل") خطC-k
:قطع السطرC-y
:لصق ("نثر") (نحن مرة أخرى في مربع واحد)C-y
:لصق مرة أخرى (الآن لدينا نسختين من خط)
هذه هي كل محرج مطول مقارنة C-d
في المحرر الخاص بك, ولكن في Emacs هناك دائما التخصيص. C-d
لا بد أن delete-char
بشكل افتراضي ، فكيف C-c C-d
?فقط إضافة ما يلي إلى .emacs
:
(global-set-key "\C-c\C-d" "\C-a\C- \C-n\M-w\C-y")
(@ناثان elisp الإصدار هو على الأرجح الأفضل ، لأنه لن كسر إذا كان أي من الارتباطات الرئيسية هي تغيير.)
حذار:بعض Emacs وسائط قد استعادة C-c C-d
أن تفعل شيئا آخر.
نصائح أخرى
بالإضافة إلى الأجوبة السابقة كما يمكنك تحديد الخاصة بك وظيفة لتكرار خط.على سبيل المثال ، وضع ما يلي في الخاص بك .emacs الملف سوف تجعل C-d تكرار السطر الحالي.
(defun duplicate-line()
(interactive)
(move-beginning-of-line 1)
(kill-line)
(yank)
(open-line 1)
(next-line 1)
(yank)
)
(global-set-key (kbd "C-d") 'duplicate-line)
ضع المؤشر على الخط ، إن لم يكن في بداية تفعل CTRL-A, ثم:
CTRL-K
CTRL-K
CTRL-Y
CTRL-Y
نسختي من وظيفة إلى تكرار الخط الذي يعمل لطيفة مع التراجع لا تعبث مع موضع المؤشر.كان نتيجة المناقشة في جنو.emacs.مصادر من تشرين الثاني / نوفمبر 1997.
(defun duplicate-line (arg)
"Duplicate current line, leaving point in lower line."
(interactive "*p")
;; save the point for undo
(setq buffer-undo-list (cons (point) buffer-undo-list))
;; local variables for start and end of line
(let ((bol (save-excursion (beginning-of-line) (point)))
eol)
(save-excursion
;; don't use forward-line for this, because you would have
;; to check whether you are at the end of the buffer
(end-of-line)
(setq eol (point))
;; store the line and disable the recording of undo information
(let ((line (buffer-substring bol eol))
(buffer-undo-list t)
(count arg))
;; insert the line arg times
(while (> count 0)
(newline) ;; because there is no newline in 'line'
(insert line)
(setq count (1- count)))
)
;; create the undo information
(setq buffer-undo-list (cons (cons eol (point)) buffer-undo-list)))
) ; end-of-let
;; put the point in the lowest line and return
(next-line arg))
ثم يمكنك تحديد CTRL-D إلى استدعاء هذه الدالة:
(global-set-key (kbd "C-d") 'duplicate-line)
بدلا من kill-line
(C-k
) كما في C-a
C-k
C-k
C-y
C-y
استخدام kill-whole-line
الأمر:
C-S-Backspace
C-y
C-y
مزايا أكثر C-k
وتشمل أنه لا يهم أين نقطة على الخط (على عكس C-k
مما يتطلب أن تكون في بداية السطر) كما يقتل السطر (شيء مرة أخرى C-k
لا تفعل).
هنا هو آخر وظيفة للقيام بذلك.نسختي لا تلمس قتل الخاتم المؤشر ينتهي على الخط الجديد حيث كان على الأصل.وسوف مكررة المنطقة إذا كان نشط (عابر مارك الوضعية) ، أو الافتراضي إلى تكرار الخط خلاف ذلك.فإنه سيتم أيضا جعل نسخ متعددة إذا ما أعطيت بادئة arg و تعليق خارج الخط الأصلي إذا ما أعطيت سلبي البادئة arg (وهذا مفيد لاختبار نسخة مختلفة من الأوامر/بيان مع الحفاظ على القديم).
(defun duplicate-line-or-region (&optional n)
"Duplicate current line, or region if active.
With argument N, make N copies.
With negative N, comment out original line and use the absolute value."
(interactive "*p")
(let ((use-region (use-region-p)))
(save-excursion
(let ((text (if use-region ;Get region if active, otherwise line
(buffer-substring (region-beginning) (region-end))
(prog1 (thing-at-point 'line)
(end-of-line)
(if (< 0 (forward-line 1)) ;Go to beginning of next line, or make a new one
(newline))))))
(dotimes (i (abs (or n 1))) ;Insert N times, or once if not specified
(insert text))))
(if use-region nil ;Only if we're working with a line (not a region)
(let ((pos (- (point) (line-beginning-position)))) ;Save column
(if (> 0 n) ;Comment out original with negative arg
(comment-region (line-beginning-position) (line-end-position)))
(forward-line 1)
(forward-char pos)))))
لقد كان لا بد أن C-c d
:
(global-set-key [?\C-c ?d] 'duplicate-line-or-region)
هذا لا ينبغي أبدا أن تكون إعادة تعيين عن طريق وضع أو أي شيء لأن C-c
تليها واحد (معدلة) رسالة محجوز المستخدم الارتباطات.
ناثان ذلك .emacs الملف هو الطريق للذهاب ولكن يمكن أن تكون مبسطة قليلا عن طريق استبدال
(open-line 1)
(next-line 1)
مع
(newline)
الغلة
(defun duplicate-line()
(interactive)
(move-beginning-of-line 1)
(kill-line)
(yank)
(newline)
(yank)
)
(global-set-key (kbd "C-d") 'duplicate-line)
لا أتذكر تماما كيف خط ازدواج يعمل في أي مكان آخر ، ولكن كما السابق SciTE المستخدم اعجبني شيء واحد عن SciTE-الطريقة:لا تلمس موضع المؤشر!لذلك كل الاكلات أعلاه لم تكن جيدة بما فيه الكفاية بالنسبة لي ، الهبي-الإصدار:
(defun duplicate-line ()
"Clone line at cursor, leaving the latter intact."
(interactive)
(save-excursion
(let ((kill-read-only-ok t) deactivate-mark)
(toggle-read-only 1)
(kill-whole-line)
(toggle-read-only 0)
(yank))))
علما أن لا شيء يحصل في الواقع قتل في العملية ، وترك علامات التحديد الحالي سليمة.
راجع للشغل, لماذا أنتم مولعا جدا من الرجيج المؤشر حول عندما يكون هناك هذا لطيف'n'clean قتل كل سطر ممتزوجات (C-S-backspace)?
تثبيت مكررة-شيء من melpa:
M-x حزمة تثبيت RET مكررة شيء
و هذا إضافة إلى keybinding ملف init :
(العالمية-مجموعة-مفتاح (kbd "م-ج") 'مكررة-شيء)
لأنني لا أعرف, سوف تبدأ هذه الجولة من ملعب مع slowball:
ctrl-k, y, y
'كتبت نسختي الخاصة من duplicate-line
, لأني لا أريد أن أفسد قتل الخاتم.
(defun jr-duplicate-line ()
"EASY"
(interactive)
(save-excursion
(let ((line-text (buffer-substring-no-properties
(line-beginning-position)
(line-end-position))))
(move-end-of-line 1)
(newline)
(insert line-text))))
(global-set-key "\C-cd" 'jr-duplicate-line)
لدي copy-from-above-command
لا بد أن مفتاح واستخدام ذلك.انها قدمت مع XEmacs, ولكن أنا لا أعرف عن GNU Emacs.
`نسخة-من-قبل-القيادة' هو تفاعلية جمعت اللثغة وظيفة
-- تحميلها من "/usr/share/xemacs/21.4.15/lisp/misc.elc" (نسخ-من-قبل-command &اختياري ARG)الوثائق: نسخ الأحرف من السابقة nonblank خط, ابتداء فقط النقطة أعلاه.نسخ ARG الشخصيات ، ولكن لا الماضي نهاية هذا الخط.لو لا حجة معينة ، نسخ بقية كامل من على خط المرمى.الشخصيات نسخها هي إدراجها في المخزن المؤقت قبل نقطة.
C-a C-k C-k C-y C-y
شيء قد تريد أن يكون في حسابك .إيماكس هو
(setq kill-whole-line t)
الأساس الذي يقتل خط كامل بالإضافة إلى السطر كلما كنت تحتج قتل الخط (أيvia C-k).ثم دون رمز إضافية ، يمكنك القيام به فقط C-C-k-C y C-y لتكرار خط.فإنه ينهار
C-a go to beginning of line
C-k kill-line (i.e. cut the line into clipboard)
C-y yank (i.e. paste); the first time you get the killed line back;
second time gives the duplicated line.
ولكن إذا كنت تستخدم هذا في كثير من الأحيان ربما مخصص مفتاح الربط قد يكون فكرة أفضل ، ولكن ميزة فقط باستخدام C-C-k-C y C-y يمكنك تكرار الخط في مكان آخر بدلا من أسفل السطر الحالي.
التخلف الرهيبة هذه.ومع ذلك ، يمكن تمديد Emacs للعمل مثل SlickEdit و تيكستماتي ، أي أن نسخ/قص السطر الحالي عند عدم النص المحدد:
(transient-mark-mode t)
(defadvice kill-ring-save (before slick-copy activate compile)
"When called interactively with no active region, copy a single line instead."
(interactive
(if mark-active (list (region-beginning) (region-end))
(message "Copied line")
(list (line-beginning-position)
(line-beginning-position 2)))))
(defadvice kill-region (before slick-cut activate compile)
"When called interactively with no active region, kill a single line instead."
(interactive
(if mark-active (list (region-beginning) (region-end))
(list (line-beginning-position)
(line-beginning-position 2)))))
مكان أعلاه في .emacs
.ثم نسخ الخط ، M-w
.لحذف الخط ، C-w
.لتكرار خط ، C-a M-w C-y C-y C-y ...
.
أحببت FraGGod الإصدار ، باستثناء أمرين:(1) أنها لا تحقق ما إذا كان المخزن المؤقت هو بالفعل للقراءة فقط مع (interactive "*")
, و (2) أنه فشل في السطر الأخير من المخزن المؤقت إذا كان هذا آخر سطر فارغ (كما كنت لا يمكن أن تقتل الخط في هذه الحالة) ، وترك الخاص بك المخزن المؤقت للقراءة فقط.
وإجراء التغييرات التالية لحل هذا:
(defun duplicate-line ()
"Clone line at cursor, leaving the latter intact."
(interactive "*")
(save-excursion
;; The last line of the buffer cannot be killed
;; if it is empty. Instead, simply add a new line.
(if (and (eobp) (bolp))
(newline)
;; Otherwise kill the whole line, and yank it back.
(let ((kill-read-only-ok t)
deactivate-mark)
(toggle-read-only 1)
(kill-whole-line)
(toggle-read-only 0)
(yank)))))
مع الأخيرة emacs, يمكنك استخدام M-w في أي مكان في خط نسخ.بحيث يصبح:
M-w C-a RET C-y
رأيت حلول معقدة جدا, على أي حال...
(defun duplicate-line ()
"Duplicate current line"
(interactive)
(kill-whole-line)
(yank)
(yank))
(global-set-key (kbd "C-x M-d") 'duplicate-line)
هناك حزمة تسمى Avy وقد أمر avy-نسخ الخط.عند استخدام هذا الأمر كل سطر في الإطار الخاص بك يحصل على خطاب الجمع.ثم عليك أن الجمع بين نوع و يمكنك الحصول على هذا الخط.هذا يعمل أيضا على المنطقة.ثم عليك أن النوع الثاني الجمع.
هنا يمكنك أن ترى واجهة:
@[كيفن كونر]:قريبة جدا, بقدر ما أعرف.الشيء الآخر الوحيد هو أن تنظر تحول على kill-whole-line
إدراج سطر جديد في C-k.
عندما دعا بشكل تفاعلي مع أي منطقة نشطة نسخة (م-ث) سطر واحد بدلا من ذلك :
(defadvice kill-ring-save (before slick-copy activate compile)
"When called interactively with no active region, COPY a single line instead."
(interactive
(if mark-active (list (region-beginning) (region-end))
(message "Copied line")
(list (line-beginning-position)
(line-beginning-position 2)))))
عندما دعا بشكل تفاعلي مع أي منطقة نشطة قتل (C-w) سطر واحد بدلا من ذلك.
(defadvice kill-region (before slick-cut activate compile)
"When called interactively with no active region, KILL a single line instead."
(interactive
(if mark-active (list (region-beginning) (region-end))
(message "Killed line")
(list (line-beginning-position)
(line-beginning-position 2)))))
أيضا ، على صعيد ذات صلة:
(defun move-line-up ()
"Move up the current line."
(interactive)
(transpose-lines 1)
(forward-line -2)
(indent-according-to-mode))
(defun move-line-down ()
"Move down the current line."
(interactive)
(forward-line 1)
(transpose-lines 1)
(forward-line -1)
(indent-according-to-mode))
(global-set-key [(meta shift up)] 'move-line-up)
(global-set-key [(meta shift down)] 'move-line-down)
ctrl-k, ctrl-k, (المركز إلى موقع جديد) ctrl-y
إضافة ctrl-a إذا كنت لا تبدأ في بداية السطر.و 2 ctrl-k هو الاستيلاء على حرف السطر الجديد.فإنه يمكن إزالتها إذا كنت ترغب فقط في النص.
أكتب لي الأفضلية.
(defun duplicate-line ()
"Duplicate current line."
(interactive)
(let ((text (buffer-substring-no-properties (point-at-bol) (point-at-eol)))
(cur-col (current-column)))
(end-of-line) (insert "\n" text)
(beginning-of-line) (right-char cur-col)))
(global-set-key (kbd "C-c d l") 'duplicate-line)
ولكن وجدت هذا سيكون مشكلة عند السطر الحالي يحتوي على أحرف متعددة البايت (مثلا ، الشخصيات كجك).إذا كنت تواجه هذه المشكلة, جرب هذا بدلا من ذلك:
(defun duplicate-line ()
"Duplicate current line."
(interactive)
(let* ((text (buffer-substring-no-properties (point-at-bol) (point-at-eol)))
(cur-col (length (buffer-substring-no-properties (point-at-bol) (point)))))
(end-of-line) (insert "\n" text)
(beginning-of-line) (right-char cur-col)))
(global-set-key (kbd "C-c d l") 'duplicate-line)
هذه الوظيفة يجب أن تتطابق مع JetBrains التنفيذ من حيث تكرار كل من خط أو منطقة ثم ترك نقطة و/ أو المنطقة النشطة كما هو متوقع:
مجرد التفاف حول التفاعلية شكل:
(defun wrx/duplicate-line-or-region (beg end)
"Implements functionality of JetBrains' `Command-d' shortcut for `duplicate-line'.
BEG & END correspond point & mark, smaller first
`use-region-p' explained:
http://emacs.stackexchange.com/questions/12334/elisp-for-applying-command-to-only-the-selected-region#answer-12335"
(interactive "r")
(if (use-region-p)
(wrx/duplicate-region-in-buffer beg end)
(wrx/duplicate-line-in-buffer)))
الذي يدعو هذا ،
(defun wrx/duplicate-region-in-buffer (beg end)
"copy and duplicate context of current active region
|------------------------+----------------------------|
| before | after |
|------------------------+----------------------------|
| first <MARK>line here | first line here |
| second item<POINT> now | second item<MARK>line here |
| | second item<POINT> now |
|------------------------+----------------------------|
TODO: Acts funky when point < mark"
(set-mark-command nil)
(insert (buffer-substring beg end))
(setq deactivate-mark nil))
أو هذا
(defun wrx/duplicate-line-in-buffer ()
"Duplicate current line, maintaining column position.
|--------------------------+--------------------------|
| before | after |
|--------------------------+--------------------------|
| lorem ipsum<POINT> dolor | lorem ipsum dolor |
| | lorem ipsum<POINT> dolor |
|--------------------------+--------------------------|
TODO: Save history for `Cmd-Z'
Context:
http://stackoverflow.com/questions/88399/how-do-i-duplicate-a-whole-line-in-emacs#answer-551053"
(setq columns-over (current-column))
(save-excursion
(kill-whole-line)
(yank)
(yank))
(let (v)
(dotimes (n columns-over v)
(right-char)
(setq v (cons n v))))
(next-line))
ثم لدي هذا لا بد أن الفوقية+shift+d
(global-set-key (kbd "M-D") 'wrx/duplicate-line-or-region)
مع البادئة الحجج ، وما هو (آمل) بديهية السلوك:
(defun duplicate-line (&optional arg)
"Duplicate it. With prefix ARG, duplicate ARG times."
(interactive "p")
(next-line
(save-excursion
(let ((beg (line-beginning-position))
(end (line-end-position)))
(copy-region-as-kill beg end)
(dotimes (num arg arg)
(end-of-line) (newline)
(yank))))))
المؤشر سوف تبقى على خط آخر.بدلا من ذلك, قد ترغب في تحديد بادئة إلى تكرار الأسطر القليلة القادمة في آن واحد:
(defun duplicate-line (&optional arg)
"Duplicate it. With prefix ARG, duplicate ARG times."
(interactive "p")
(save-excursion
(let ((beg (line-beginning-position))
(end
(progn (forward-line (1- arg)) (line-end-position))))
(copy-region-as-kill beg end)
(end-of-line) (newline)
(yank)))
(next-line arg))
أجد نفسي باستخدام كل من الأحيان باستخدام وظيفة المجمع إلى تبديل سلوك البادئة الحجة.
و keybinding:(global-set-key (kbd "C-S-d") 'duplicate-line)
;; http://www.emacswiki.org/emacs/WholeLineOrRegion#toc2
;; cut, copy, yank
(defadvice kill-ring-save (around slick-copy activate)
"When called interactively with no active region, copy a single line instead."
(if (or (use-region-p) (not (called-interactively-p)))
ad-do-it
(kill-new (buffer-substring (line-beginning-position)
(line-beginning-position 2))
nil '(yank-line))
(message "Copied line")))
(defadvice kill-region (around slick-copy activate)
"When called interactively with no active region, kill a single line instead."
(if (or (use-region-p) (not (called-interactively-p)))
ad-do-it
(kill-new (filter-buffer-substring (line-beginning-position)
(line-beginning-position 2) t)
nil '(yank-line))))
(defun yank-line (string)
"Insert STRING above the current line."
(beginning-of-line)
(unless (= (elt string (1- (length string))) ?\n)
(save-excursion (insert "\n")))
(insert string))
(global-set-key (kbd "<f2>") 'kill-region) ; cut.
(global-set-key (kbd "<f3>") 'kill-ring-save) ; copy.
(global-set-key (kbd "<f4>") 'yank) ; paste.
إضافة elisp أعلاه لك الحرف الأول.ش ، و يمكنك الحصول على قطع/نسخ خط كامل وظيفة الآن, ثم يمكنك F3 F4 لتكرار خط.
أبسط طريقة هي كريس كونواي الأسلوب.
C-a C-SPACE C-n M-w C-y
هذه هي الطريقة الافتراضية بتكليف من EMACS.في رأيي أنه من الأفضل استخدام المعيار.أنا دائما الحرص على نحو التخصيص المرء مفتاح الربط في EMACS.EMACS هي بالفعل قوية بما فيه الكفاية ، أعتقد أننا يجب أن نبذل قصارى جهدنا من أجل التكيف مع المفتاح الخاص به-الارتباطات.
على الرغم من انها قليلا طويلة ، ولكن عندما كنت تستخدم ذلك ، يمكنك أن تفعل السريع سوف تجد هذا هو متعة!
وهنا وظيفة تكرار السطر الحالي.مع البادئة الحجج ، فإنه سيتم تكرار الخط عدة مرات.E. g. ، C-3 C-S-o
سيتم تكرار السطر الحالي ثلاث مرات.لا يغير لقتل خاتم.
(defun duplicate-lines (arg)
(interactive "P")
(let* ((arg (if arg arg 1))
(beg (save-excursion (beginning-of-line) (point)))
(end (save-excursion (end-of-line) (point)))
(line (buffer-substring-no-properties beg end)))
(save-excursion
(end-of-line)
(open-line arg)
(setq num 0)
(while (< num arg)
(setq num (1+ num))
(forward-line 1)
(insert-string line))
)))
(global-set-key (kbd "C-S-o") 'duplicate-lines)
أنا لا أصدق كل هذه حلول معقدة.هذا هو اثنين من المفاتيح:
<C-S-backspace>
تشغيل الأمر قتل كل سطر
C-/
تشغيل الأمر التراجع
لذلك <C-S-backspace> C-/
إلى "نسخ" خط كامل (قتل والتراجع).
يمكنك بالطبع الجمع بين هذا مع رقمية السلبية وسائط لقتل خطوط متعددة إما إلى الأمام أو الخلف.
هذا يبدو أكثر طبيعية ، فيما يتعلق اختيار إجابة من قبل كريس كونواي.
(العالمية-تعيين مفتاح "\C-c\C-d" "\C-a\C \C-n\M-w\C-y\C-p\C-e")
هذا يسمح لك لتكرار الخط عدة مرات من قبل ببساطة تكرار \C-c\C-d مفتاح السكتات الدماغية.