Convert tabs to spaces using Sublime Text and VI macros
Recently while working on a project I accidentally modified my IDE’s spacing settings causing all new files to have inconsistent spacing with old files. Converting the spacing of all those files would be a huge pain. Luckily, with some google fu I was able to find a great answer to convert spaces to tabs using sublime text 2.
Steps to convert a file back to two spaces in sublime text
- View -> indentation -> Tab Width: 2
- View -> indentation -> Convert indentation to Tabs
- View -> indentation -> Convert indentation to Spaces
It’s not perfect since spacing in between lines of text might have more tabs or spaces. To solve this issue you can use VIM mode in sublime to record a macro to fix individual spacing problems.
If you join the lines and then press enter sublime will fix the tab width to match that of your current line.
Steps to record a macro in VIM
- press
q
to start recording - press any character to name the macro so we can refer to it later
- start inputting commands, VI will record it for you
- press
q
to stop recording
To call the macro
- press @ followed by the character you assigned it
Example to join line a line and press enter
- press ‘q’ to start recording
- press
m
to assign macro name to call later - press
shift + j
to join the line - press
i
- press
enter
- press
q
to stop recording
Using the macro
You can use the macro by calling it with the assigned letter
press esc
press @m
You can invoke it multiple times, for example to invoke 5 times
press esc
press 5@m