2011-02-28 18 views
11

प्रारंभिक इंडेंट करने के लिए टैब का उपयोग करने के लिए विम को मजबूर करने का कोई तरीका है, लेकिन जब रीटैब चला रहा है! आदेश टैब के साथ आंतरिक शब्द अंतर को प्रतिस्थापित नहीं करता है?बल वी/विम केवल प्रमुख टैब का उपयोग करने के लिए retab पर!

" Retab spaced file, but only indentation 
command! RetabIndents call RetabIndents() 

" Retab spaced file, but only indentation 
func! RetabIndents() 
    let saved_view = winsaveview() 
    execute '%[email protected]^\(\{'.&ts.'}\)\[email protected]\=repeat("\t", len(submatch(0))/'.&ts.')@' 
    call winrestview(saved_view) 
endfunc 

फिर आप उपयोग कर सकते हैं:

:RetabIndents 

टैब के रूप में सभी प्रमुख रिक्त स्थान को बदलने के लिए, लेकिन टैब प्रभावित करने के लिए नहीं

उत्तर

11

मेरे अनुभव में यह करने के लिए सबसे अच्छा तरीका है एक कस्टम समारोह के साथ है अन्य पात्रों के बाद। यह मानता है कि 'ts' सही ढंग से सेट है। फ़ाइलों को आप कुछ इस तरह करने से बेहतर गलत संरेखित कर रहे हैं बनाने के लिए एक लंबा रास्ता तय कर सकते हैं:

:set ts=8  " Or whatever makes the file looks like right 
:set et  " Switch to 'space mode' 
:retab  " This makes everything spaces 
:set noet  " Switch back to 'tab mode' 
:RetabIndents " Change indentation (not alignment) to tabs 
:set ts=4  " Or whatever your coding convention says it should be 

आप एक फ़ाइल जहां सभी प्रमुख खाली स्थान के टैब ताकि लोगों को जो कुछ भी प्रारूप में इसे देख सकते हैं के साथ खत्म हो जाएगा वे चाहते हैं, लेकिन जहां सभी पिछला सफेद स्थान रिक्त स्थान है ताकि सभी अंतराल की टिप्पणियां, टेबल आदि किसी भी टैब चौड़ाई के साथ ठीक से लाइन हो जाएं।

संपादित

'exe' लाइन का स्पष्टीकरण:

execute '%[email protected]^\(\{'.&ts.'}\)\[email protected]\=repeat("\t", len(submatch(0))/'.&ts.')@' 

" Execute the result of joining a few strings together: 

%s    " Search and replace over the whole file 
@[email protected]@  " Delimiters for search and replace 
^    " Start of line 
\(...\)   " Match group 
\{...}   " Match a space the number of times in the curly braces 
&ts    " The current value of 'tabstop' option, so: 
       " 'string'.&ts.'string' becomes 'string4string' if tabstop is 4 
       " Thus the bracket bit becomes \(\{4}\) 
\+    " Match one or more of the groups of 'tabstop' spaces (so if 
       " tabstop is 4, match 4, 8, 12, 16 etc spaces 
@    " The middle delimiter 
\=    " Replace with the result of an expression: 
repeat(s,c)  " Make a string that is c copies of s, so repeat('xy',4) is 'xyxyxyxy' 
"\t"    " String consisting of a single tab character 
submatch(0)  " String that was matched by the first part (a number of multiples 
       " of tabstop spaces) 
len(submatch(0)) " The length of the match 
/'.&ts.'   " This adds the string "/4" onto the expression (if tabstop is 4), 
       " resulting in len(submatch(0))/4 which gives the number of tabs that 
       " the line has been indented by 
)    " The end of the repeat() function call 

@    " End delimiter 
+0

बढ़िया जवाब। मुझे उस 'निष्पादन' कथन में खोदना होगा क्योंकि यह कुछ गहरे अंधेरे जादूगरों की तरह दिखता है। –

+0

खुशी है कि यह सहायक है। मैंने 'execute' कथन क्या कर रहा है इसका एक स्पष्टीकरण जोड़ा है। – DrAl

+0

मुझे इस फ़ंक्शन का उपयोग करके कोई सफलता नहीं मिली है। जब मैं इसका उपयोग कर चलाता हूं: RetabIndents, मुझे मिलता है: फ़ंक्शन प्रोसेसिंग करते समय त्रुटि मिली: लाइन 2: ई 486: पैटर्न नहीं मिला:^\ (\ {4} \) \ + –

0

यह अन्य (*) सवाल का मेरा उत्तर से:

इस छोटे से समस्या मैं सिफारिश निपटान करने के लिए एक खोज, retab के बजाय।

:%s/^\(^I*\)␣␣␣␣/\1^I/g 

यह खोज किसी भी लाइनों के लिए पूरी फ़ाइल में देखने टैब में चाहे जितने, 4 रिक्त स्थान के द्वारा पीछा के साथ शुरू, और टैब में चाहे जितने यह पाया प्लस वन के लिए यह विकल्प होगा।

से आप के साथ @: या [email protected]:

इसका बेहतर लिंक bellow में विस्तार से बताया खोज फिर से करना की जरूरत है।

(*) How can I convert spaces to tabs in Vim or Linux?