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

  1. View -> indentation -> Tab Width: 2
  2. View -> indentation -> Convert indentation to Tabs
  3. 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

  1. press q to start recording
  2. press any character to name the macro so we can refer to it later
  3. start inputting commands, VI will record it for you
  4. press q to stop recording

To call the macro

  1. press @ followed by the character you assigned it

Example to join line a line and press enter

  1. press ‘q’ to start recording
  2. press m to assign macro name to call later
  3. press shift + j to join the line
  4. press i
  5. press enter
  6. 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 [email protected]