2012-06-21 11 views

उत्तर

23

आप CUA चयन मोड सक्षम करते हैं:

एम एक्सcua-selection-modeआरईटी

या स्थायी रूप से अपने init फाइल में:

(cua-selection-mode 1) 

तो आप अपने उन्नत आयत संपादन की सुविधा का उपयोग कर सकते हैं।

(या यदि आप एक cua-mode उपयोगकर्ता हैं, तो आप ऐसा करने की जरूरत नहीं है।)

  • सी आरईटी एक कोने चिह्नित करने के लिए।
  • विपरीत कोने पर बिंदु ले जाएं।
  • एम-आरregexp चिह्नित आयत के भीतर प्रतिस्थापित करने के लिए।
  • सी-आरईटी आयताकार संपादन से बाहर निकलने/निकालने के लिए।
दस्तावेज़ीकरण के लिए

, "CUA आयत समर्थन" एमएक्समें टिप्पणी में बढ़ रहा है के लिए देखो find-libraryआरईटीcua-baseआरईटी

आप के लिए कुआ आयत सुविधाओं का उपयोग नहीं करना चाहते हैं कुछ कारण (शायद अगर आपको वास्तव में replace-string की आवश्यकता है), apply-on-rectangle का उपयोग कर एक कस्टम फ़ंक्शन एक साथ रखने के लिए काफी आसान होगा।

संपादित: असल में, थोड़ा और अधिक जटिल की तुलना में मैं उम्मीद थी, लेकिन कोड के सबसे इंटरैक्टिव कल्पना और 'सीमांकित' उपसर्ग तर्क व्यवहार के लिए समर्थन (दोनों replace-string के आधार पर) है।

संपादित 2: मैंने तय कर लिया है कि यह एक अधिक पूरी तरह से विशेषताओं ढंग से कर रही लायक था:

निम्नलिखित Cxआरएम% और Cxआरमुख्यमंत्री प्रदान करता है -%, जो (उम्मीद है) जिस तरह से आप उम्मीद करेंगे उस पर कार्य करें। प्रतिस्थापन में आयत कुआ करने के लिए 'एम आर' के लिए बाध्य

(require 'rect) 

(defun my-search-replace-in-rectangle 
    (start end search-pattern replacement search-function literal) 
    "Replace all instances of SEARCH-PATTERN (as found by SEARCH-FUNCTION) 
with REPLACEMENT, in each line of the rectangle established by the START 
and END buffer positions. 

SEARCH-FUNCTION should take the same BOUND and NOERROR arguments as 
`search-forward' and `re-search-forward'. 

The LITERAL argument is passed to `replace-match' during replacement. 

If `case-replace' is nil, do not alter case of replacement text." 
    (apply-on-rectangle 
    (lambda (start-col end-col search-function search-pattern replacement) 
    (move-to-column start-col) 
    (let ((bound (min (+ (point) (- end-col start-col)) 
         (line-end-position))) 
      (fixedcase (not case-replace))) 
     (while (funcall search-function search-pattern bound t) 
     (replace-match replacement fixedcase literal)))) 
    start end search-function search-pattern replacement)) 

(defun my-replace-regexp-rectangle-read-args (regexp-flag) 
    "Interactively read arguments for `my-replace-regexp-rectangle' 
or `my-replace-string-rectangle' (depending upon REGEXP-FLAG)." 
    (let ((args (query-replace-read-args 
       (concat "Replace" 
         (if current-prefix-arg " word" "") 
         (if regexp-flag " regexp" " string")) 
       regexp-flag))) 
    (list (region-beginning) (region-end) 
      (nth 0 args) (nth 1 args) (nth 2 args)))) 

(defun my-replace-regexp-rectangle 
    (start end regexp to-string &optional delimited) 
    "Perform a regexp search and replace on each line of a rectangle 
established by START and END (interactively, the marked region), 
similar to `replace-regexp'. 

Optional arg DELIMITED (prefix arg if interactive), if non-nil, means 
replace only matches surrounded by word boundaries. 

If `case-replace' is nil, do not alter case of replacement text." 
    (interactive (my-replace-regexp-rectangle-read-args t)) 
    (when delimited 
    (setq regexp (concat "\\b" regexp "\\b"))) 
    (my-search-replace-in-rectangle 
    start end regexp to-string 're-search-forward nil)) 

(defun my-replace-string-rectangle 
    (start end from-string to-string &optional delimited) 
    "Perform a string search and replace on each line of a rectangle 
established by START and END (interactively, the marked region), 
similar to `replace-string'. 

Optional arg DELIMITED (prefix arg if interactive), if non-nil, means 
replace only matches surrounded by word boundaries. 

If `case-replace' is nil, do not alter case of replacement text." 
    (interactive (my-replace-regexp-rectangle-read-args nil)) 
    (let ((search-function 'search-forward)) 
    (when delimited 
     (setq search-function 're-search-forward 
      from-string (concat "\\b" (regexp-quote from-string) "\\b"))) 
    (my-search-replace-in-rectangle 
    start end from-string to-string search-function t))) 

(global-set-key (kbd "C-x r M-%") 'my-replace-string-rectangle) 
(global-set-key (kbd "C-x r C-M-%") 'my-replace-regexp-rectangle) 
+0

धन्यवाद! दुर्भाग्यवश, ऐसा लगता है कि मैं केवल एमएआई में 'सी-आरईटी' के साथ एक कोने को चिह्नित कर सकता हूं, टर्मिनल मोड में नहीं। – user545424

+0

यह आपके टर्मिनल के कारण उस अनुक्रम को नहीं भेज रहा होगा। आप एक xterm अधिक सफल पा सकते हैं (यह भी देखें http://stackoverflow.com/questions/4337837/send-c-to-emacs-in-vt100-xterm-terminal-mac-os-xs-terminal)। मैंने गैर-सीयूए दृष्टिकोण के लिए अधिक उत्तर-विशेषीकृत कोड के साथ अपना उत्तर भी अपडेट किया है। – phils

+0

फ़ीचर अनुरोध: आयताकार खोजें। मेरे पास एक बड़ा कॉलम है और मुझे यह जांचने की ज़रूरत है कि इसमें कुछ और है लेकिन 'डीपीपी' है। मैं कुछ जटिल regexp का उपयोग करता हूं - और यदि कॉलम वास्तव में बड़ा है - मुझे regexp स्टैक ओवरफ़्लो मिलता है। – Adobe

0

आप CUA मोड में हैं, तो आप उपयोग कर सकते हैं।

+0

लेकिन यह केवल चीज़ की पहली घटना को प्रतिस्थापित करता है (डिफ़ॉल्ट vim replace-regexp व्यवहार)। – Adobe

0

बुरे उपयोगकर्ताओं के लिए, आप query-replace और replace-regexp को बुरा दृश्य ब्लॉक के अंदर प्राप्त करने के लिए अपने पैकेज evil-visual-replace का उपयोग कर सकते हैं।

3

हाल Emacs (जैसे Emacs 25) में आप M-% (query-replace) या C-M-% (query-replace-regexp) का उपयोग कर बॉक्स से बाहर कर सकते हैं।

आयताकार क्षेत्र बनाने के लिए M-x rectangle-mark-mode का उपयोग करें। यदि आवश्यक हो तो C-x C-x का उपयोग करें, चिह्न से पहले बिंदु डालने के लिए। फिर बस क्वेरी-प्रतिस्थापित करें।

हालांकि, वे replace-string के लिए Emacs 25 में समान परिवर्तन नहीं करते थे। यदि आप चाहें तो ऐसा कर सकते हैं, बस region-noncontiguous-p तर्क जोड़कर query-replace में किया गया है। कोड सरल है:

(defun replace-string (from-string to-string &optional delimited start end backward 
         region-noncontiguous-p) 
    "..." 
    (declare (interactive-only 
      "use `search-forward' and `replace-match' instead.")) 
    (interactive 
    (let ((common 
      (query-replace-read-args 
      (concat "Replace" 
        (if current-prefix-arg 
         (if (eq current-prefix-arg '-) " backward" " word") 
        "") 
        " string" 
        (if (use-region-p) " in region" "")) 
      nil))) 
    (list (nth 0 common) (nth 1 common) (nth 2 common) 
      (if (use-region-p) (region-beginning)) 
      (if (use-region-p) (region-end)) 
      (nth 3 common) 
      (if (use-region-p) (region-noncontiguous-p))))) 
    (perform-replace 
    from-string to-string nil nil delimited nil nil start end backward region-noncontiguous-p)) 

वैकल्पिक रूप से, तुम सिर्फ पुस्तकालय replace+.el डाउनलोड करने और वहाँ से replace-string के संस्करण का उपयोग कर सकते हैं। यह वही करता है जो आप चाहते हैं।

Fwiw, मैं सिर्फ पाने के लिए Emacs bug#27897 दायर किया है तो इस सुविधा, replace.el ही पुस्तकालय में replace-string और कई अन्य आदेशों को जोड़ा गया।

+0

इस से संबंधित एक एंजू बग भी है (जो 'क्वेरी-प्रतिस्थापन' के आपके संस्करण को प्रतिस्थापित कर सकता है): https://github.com/syohex/emacs-anzu/issues/69 –

संबंधित मुद्दे