2010-02-12 9 views

उत्तर

12

यह वर्तमान रेखा के सामने से 4 रिक्त स्थान हटा देता है (रिक्त स्थान मौजूद हैं)।

(global-set-key (kbd "<S-tab>") 'un-indent-by-removing-4-spaces) 
(defun un-indent-by-removing-4-spaces() 
    "remove 4 spaces from beginning of of line" 
    (interactive) 
    (save-excursion 
    (save-match-data 
     (beginning-of-line) 
     ;; get rid of tabs at beginning of line 
     (when (looking-at "^\\s-+") 
     (untabify (match-beginning 0) (match-end 0))) 
     (when (looking-at "^ ") 
     (replace-match ""))))) 

अपने चर tab-width 4 होता है (और है कि क्या आप "पूर्ववत करें" करना चाहते हैं), तो आप और अधिक सामान्य की तरह (concat "^" (make-string tab-width ?\)) कुछ के साथ (looking-at "^ ") बदल सकते हैं।

इसके अलावा, कोड untabify का उपयोग करके रिक्त स्थान पर टैब के सामने टैब को रूपांतरित करेगा।

+1

अच्छा! लेकिन मैं आईएफ के बजाय WHEN का उपयोग करूंगा, क्योंकि कोई और खंड नहीं है। मेरे लिए – Ken

+1

, यह किसी चयनित क्षेत्र के साथ काम नहीं करता है। केवल पहली पंक्ति के साथ: -/ – Ismael

+0

मेरे लिए, यह बिल्कुल काम नहीं करता है। :( –

40

ऐसा करने के लिए, मैं indent-rigidly कमांड का उपयोग करता हूं, C-x TAB से जुड़ा हुआ है। चयनित क्षेत्र को बाईं ओर चार स्थानों पर बाईं ओर ले जाने के लिए तर्क -4 दें: C-u -4 C-x TAB

http://www.gnu.org/s/emacs/manual/html_node/elisp/Region-Indent.html

+2

उसी मैन्युअल प्रविष्टि के अनुसार, आप' इंडेंट-कोड-कठोर रूप से ' 'क्योंकि यह अकेले टिप्पणियां और मल्टी-लाइन तारों को छोड़ देता है – toolbear

5

मैं अगर आप टैब या Shift + Tab का उपयोग पर निर्भर करता है बाएं या दाएं चार रिक्त स्थान से किसी क्षेत्र के ऊपर टैब के लिए कुछ कार्यों बनाया:

(defun indent-region-custom(numSpaces) 
    (progn 
     ; default to start and end of current line 
     (setq regionStart (line-beginning-position)) 
     (setq regionEnd (line-end-position)) 

     ; if there's a selection, use that instead of the current line 
     (when (use-region-p) 
      (setq regionStart (region-beginning)) 
      (setq regionEnd (region-end)) 
     ) 

     (save-excursion ; restore the position afterwards    
      (goto-char regionStart) ; go to the start of region 
      (setq start (line-beginning-position)) ; save the start of the line 
      (goto-char regionEnd) ; go to the end of region 
      (setq end (line-end-position)) ; save the end of the line 

      (indent-rigidly start end numSpaces) ; indent between start and end 
      (setq deactivate-mark nil) ; restore the selected region 
     ) 
    ) 
) 

(defun untab-region (N) 
    (interactive "p") 
    (indent-region-custom -4) 
) 

(defun tab-region (N) 
    (interactive "p") 
    (if (active-minibuffer-window) 
     (minibuffer-complete) ; tab is pressed in minibuffer window -> do completion 
    ; else 
    (if (string= (buffer-name) "*shell*") 
     (comint-dynamic-complete) ; in a shell, use tab completion 
    ; else 
    (if (use-region-p) ; tab is pressed is any other buffer -> execute with space insertion 
     (indent-region-custom 4) ; region was selected, call indent-region 
     (insert " ") ; else insert four spaces as expected 
    ))) 
) 

(global-set-key (kbd "<backtab>") 'untab-region) 
(global-set-key (kbd "<tab>") 'tab-region) 

संपादित: करने के लिए जोड़ा गया Maven का कोड जांचें कि मिनीबफर में, साथ ही विशिष्ट बफर (खोल) में टैब पूर्ण होने के लिए, उदाहरण के लिए)।

+1

अच्छा, स्वीकार्य उत्तर केवल 1 लाइन के लिए काम कर रहा था, न कि क्षेत्रों –

+1

परफेक्ट। क्षेत्रों और रेखाओं के लिए काम करता है। अन्य सामान्य संपादक की कार्यक्षमताओं को दोहराता है। – mythicalcoder

1

मैंने स्टेनली बेक के जवाब पर सुधार किया है। मेरे लिए, यह वैश्विक कुंजी बाध्यकारी मिनीबफर पूरा करने में गड़बड़ कर रहा था।

इसलिए मैंने मिनीबफर के लिए भी एक मामला शामिल किया है।

संपादित करें: indent-region ->indent-region-custom का नाम बदलें।

indent-region मौजूदा कमांड के साथ संघर्ष कर रहा है और इंडेंट के लिए त्रुटि (सहेजने से पहले हुक) और कुछ अन्य महत्वपूर्ण संयोजनों पर त्रुटि देता है।

(defun indent-region-custom(numSpaces) 
    (progn 
    ;; default to start and end of current line 
    (setq regionStart (line-beginning-position)) 
    (setq regionEnd (line-end-position)) 
    ;; if there's a selection, use that instead of the current line 
    (when (use-region-p) 
     (setq regionStart (region-beginning)) 
     (setq regionEnd (region-end)) 
    ) 

    (save-excursion ; restore the position afterwards 
     (goto-char regionStart) ; go to the start of region 
     (setq start (line-beginning-position)) ; save the start of the line 
     (goto-char regionEnd) ; go to the end of region 
     (setq end (line-end-position)) ; save the end of the line 

     (indent-rigidly start end numSpaces) ; indent between start and end 
     (setq deactivate-mark nil) ; restore the selected region 
    ) 
    ) 
) 

(defun untab-region (N) 
    (interactive "p") 
    (indent-region-custom -4) 
) 

(defun tab-region (N) 
    (interactive "p") 
    (if (active-minibuffer-window) 
     (minibuffer-complete) ; tab is pressed in minibuffer window -> do completion 
    (if (use-region-p) ; tab is pressed is any other buffer -> execute with space insertion 
     (indent-region-custom 4) ; region was selected, call indent-region-custom 
     (insert " ") ; else insert four spaces as expected 
    ) 
    ) 
) 

(global-set-key (kbd "<backtab>") 'untab-region) 
(global-set-key (kbd "<tab>") 'tab-region) 
संबंधित मुद्दे