Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a keyboard shortcut for converting from tabs to spaces in sublimetext2

What is the option in the Key Bindings file that I would use to set up a keyboard shortcut for the menu item

View -> Indentation -> Convert Indentation To Spaces

I've tried the following, but can't figure out how to trigger the option I see in the above mentioned menu

{ "keys": ["super+t"], "command": "convert_to_tabs" },
{ "keys": ["shift+super+t"], "command": "convert_to_spaces" },
like image 935
Neil Holcomb Avatar asked Jan 17 '13 21:01

Neil Holcomb


People also ask

How do you replace tabs with spaces in sublime?

Sublime Text 2 allows you to convert tabs to spaces in existing files manually (View -> Indentation -> Convert indentation to spaces). However, this can be done automatically when saving the file.

How do I switch between tabs in Sublime Text?

Use Cmd (Mac) or Alt (Windows) with the number keys 0–9 to switch between tabs in Sublime Text. For example hit Cmd–2 (Mac) or Alt–2 (Windows) to switch to the second tab. You can also use Ctrl–Tab (Mac and Windows) to cycle through your tabs.

How do you fix inconsistent tabs and spaces in Indentation in sublime?

In Sublime Text, while editing a Python file: Sublime Text menu -> Preferences -> Settings: Syntax-Specific: { "tab_size": 4, "translate_tabs_to_spaces": true } Or, In the bottom right of the editor, click the part of the status bar where it says "Tab size" click "Convert Indentation to Spaces" to correct an existing ...


1 Answers

Simple Version

Convert spaces to tabs (from Eric Muyser):

{ "keys": ["ctrl+shift+x"], "command": "unexpand_tabs", "args": { "set_translate_tabs": false } }

Convert tabs to spaces (from mVChr):

{ "keys": ["ctrl+shift+y"], "command": "expand_tabs", "args": { "set_translate_tabs": true } }

Original Answer Below


You could do it with one command each (expand_tabs and unexpand_tabs), but it would also be a good idea to toggle "Indent Using Spaces". Here is a macro that I saw somewhere (I forget where.)

Macro Version

Tabs -> Spaces

Save as "convert_tabs_to_spaces.sublime-macro" inside of your "Packages/User" folder

[
  {
    "args":
    {
      "set_translate_tabs": true
    },
    "command": "expand_tabs"
  }
]

Add this to your keybindings file:

    { "keys": ["ctrl+shift+x"], "command": "run_macro_file", "args": {"file": "Packages/User/convert_tabs_to_spaces.sublime-macro"} },

Spaces -> Tabs

Similar file name and keybinding

[
  {
    "args":
    {
      "set_translate_tabs": false
    },
    "command": "unexpand_tabs"
  }
]

I used this until I saw that you could left click the indentation setting in the Status Bar and change it from there.

Edit:

Plugin Version

adapted from here

"convert_tabs_to_spaces.py"

import sublime, sublime_plugin

class ConvertTabsToSpaces(sublime_plugin.TextCommand):
    def run(self, edit):
        self.view.run_command('expand_tabs', {"set_translate_tabs": True})

.

keybinding: { "keys": ["ctrl+shift+x"], "command": "convert_tabs_to_spaces"},

"convert_spaces_to_tabs.py"

import sublime, sublime_plugin

class ConvertSpacesToTabs(sublime_plugin.TextCommand):
    def run(self, edit):
        self.view.run_command('unexpand_tabs', {"set_translate_tabs": False})

.

keybinding: { "keys": ["ctrl+shift+y"], "command": "convert_spaces_to_tabs"},

like image 122
d_rail Avatar answered Oct 19 '22 13:10

d_rail