Replacing space indentation with tabs

Asked
Active3 hr before
Viewed126 times

5 Answers

replacingspace
90%

hmm, the third line as I wrote it was 4 spaces, a tab and 4 more spaces however this doesn't appear to have come out in the stackoverflow question. Either way the regex stopped the tab and the four trailing spaces remain – Lucas Feb 1 '17 at 14:40 ,This was an example of what I had originally, using the regex given the expression works perfectly with regards to the conversion but for the fact that the 4 spaces between the ----><---- are replaced by a tab. I am really looking to have text after indentation unaltered.,For clarity what I am trying to do is change the the start of text indentation from spaces to tabs, I really don't understand why this is confusing to anybody.,If you want a performant but hard to understand or tweak one-liner instead, the solutions in the comments from the regex-gurus above should work :)

If you're not a regular expression guru, this will probably make most sense to you and be easier to adapt to similar use cases (this is not the most efficient code, but it's the most "readable" imho):

// replace all regex matches with the result of applying
// a given anonymous function to a $matches array
function tabs2spaces($s_with_spaces) {
   // before anything else, replace existing tabs with 4 spaces
   // to permit homogenous translation
   $s_with_spaces = str_replace("\t", '    ', $s_with_spaces);
   return preg_replace_callback(
      '/^([ ]+)/m',
      function($ms) {
         // $ms[0] - is full match
         // $ms[1] - is first (...) group fron regex

         // ...here you can add extra logic to handle
         // leading spaces not multiple of 4

         return str_repeat("\t", floor(strlen($ms[1]) / 4));
      },
      $s_with_spaces
   );
}

// example (using dots to make spaces visible for explaining)
$s_with_spaces = << < EOS
no indent
   ....4 spaces indent
   ........8 spaces indent
EOS;
$s_with_spaces = str_replace('.', ' ');
$s_with_tabs = tabs2spaces($s_with_spaces);
load more v
88%

Then to correct indentation of the entire file, you may try: gg=G. Check: Re-indenting badly indented code for more details.,Replace all sequences of white-space containing a <Tab> with new strings of white-space using the new tabstop value given. If you do not specify a new tabstop size or it is zero, Vim uses the current value of 'tabstop'. [...] With 'expandtab' on, Vim replaces all tabs with the appropriate number of spaces.,Vim provides !retab command which will replace all sequences of <Tab> with new strings of white-space using the new tabstop (e.g. :set tabstop=2) value given, but all tabs inside of strings can be modified (e.g. in a C program, you should use \t to avoid this)!,This can produce unexpected side-effects in some scenarios, and it's even more of an issue when changing spaces to tabs!

So this (where is a tab character):

if :;
do⇥ echo "⇥hello"
end

gets changed to (where is a space character):

if :;
do␣␣ echo "␣␣hello"
end

So, I wrote a little function to change only tabs/spaces at the start of the line:

" :retab changes *everything*, not just start of lines
fun!Retab(expandtab)
let l: spaces = repeat(' ', & tabstop)

" Replace tabs with spaces
if a: expandtab
silent!execute '%substitute#^\%('.l: spaces.
'\)\+#\=repeat("\t", len(submatch(0)) / &tabstop)#e'
" Replace spaces with tabs
else
   silent!execute '%substitute#^\%(\t\)\+#\=repeat("'.l: spaces.
'", len(submatch(0)))#e'
endif
endfun
load more v
72%

python has forced indent. A lot of the time you will seee the error because of indent or space. So it is very necessary to make the all the code indented by space or tab. This is how to replace tab by space or replace spaces by tab in linux., Python ,Python: Spaces to Tabs?,or run this in command line

sed - e 's/    /\t/g'
test.py > test.new.py
load more v
65%

Do you use tabs or spaces for code indentation?,iusenotabsorspacesanywhere,You’ve obviously misunderstood the whole concept of using tabs for indentation,Also, who uses 3 spaces??? Usually indentation is either 2 or 4 spaces.

My solution is to newline the stuff I want to align. Took a while to get used to, but I think it’s optimum:


if (longComplexThing && otherComplexThing && finalComplexThing) {

if (longComplexThing && otherComplexThing && finalComplexThing) {
load more v
75%

Since tabs effectively group spaces together, you may be tempted to work with tabs rather than spaces and change individual lines selectively. To easily change a tab-based indent to use spaces instead when 'noexpandtab' is set, you can temporarily set 'expandtab' and use :retab with a range. For example, to convert only the current line to use spaces, use :.retab. ,After the 'expandtab' option is set, all the new tab characters entered will be changed to spaces. This will not affect the existing tab characters. To change all the existing tab characters to match the current tab settings, use: ,To control the number of space characters that will be inserted when the tab key is pressed, set the 'tabstop' option. For example, to insert 4 spaces for a tab, use: ,To insert space characters whenever the tab key is pressed, set the 'expandtab' option:

To insert space characters whenever the tab key is pressed, set the 'expandtab' option:

: set expandtab
load more v

Other "replacing-space" queries related to "Replacing space indentation with tabs"