Sublime Forum

How to set up key bindings to change file type

#1

I would like to learn whether it’s possible to set up a key binding in Sublime Text 2 to change a file type. To clarify further, I am not trying to alter a particular file type settings via a key binding but change the file type itself.

When I open Sublime Text 2, my default file type is plain text. This works for my needs. But I would like to create a shortcut (key binding) to be able to quickly switching to a different file type (e.g. Java or SQL). As of right now, I have to make the selection via mouse.

0 Likes

#2

Check package apply sintax, enable logging of commands, write a custom keybinding

0 Likes

#3

Thanks for your reply tito. I am aware of the keybindings option in sublime text as I have set up a couple of simple key bindings.

E.g. I have this one in my user-key binding.

{ “keys”: “ctrl+shift+f”], “command”: “reindent” , “args”: { “single_line”: false } }

However, I am not sure what the syntax for changing file type would look like.

Would it be something like this?

{ “keys”: “ctrl+shift+j”], “command”: “switch_to_java_file_type” , “args”: { “???”: ??? } }

Can you please advise what the syntax for this is?

Thank you!

0 Likes

#4

okeIi, just rename it via a command then:

[code]{ “keys”: “ctrl+shift+j”], “command”: “switch_to_file_type” , “args”: { “extension”: “sql” } }

[/code]


class switch_to_file_type(sublime_plugin.TextCommand):
    def run(self, edit, extension):

        view = sublime.active_window().active_view()
        name = view.file_name()
        if name:
            import re
            view.retarget(re.sub('\.^\.]+$', '.'+extension, name))

            import os
            os.rename(name, re.sub('\.^\.]+$', '.'+extension, name))

            if extension == 'html':
                view.settings().set('syntax', 'Packages/HTML/HTML.tmLanguage')
            elif extension == 'css':
                view.settings().set('syntax', 'Packages/CSS/CSS.tmLanguage')
            elif extension == 'javascript':
                view.settings().set('syntax', 'Packages/JavaScript/JavaScript.tmLanguage')
            elif extension == 'json':
                view.settings().set('syntax', 'Packages/JavaScript/JSON.tmLanguage')
            elif extension == 'xml':
                view.settings().set('syntax', 'Packages/XML/XML.tmLanguage')

0 Likes

#5

Thanks so much tito. I apologize for my thickness as I am not very familiar with how Sublime Text’s guts work.
Can you please let me know where to put the class switch_to_file_type(sublime_plugin.TextCommand)… code?

Thank you!

0 Likes

#6

Main menubar -> Preferences -> Browse PAckages

./User/switch_to_file_type.py

0 Likes

#7

Hi tito,

Thanks for your reply. I added the codes in following files:

…\AppData\Roaming\Sublime Text 2\Packages\User\Default (Windows).sublime-keymap"

{ "keys": "ctrl+shift+j"], "command": "switch_to_file_type" , "args": { "extension": "sql" } }

…\AppData\Roaming\Sublime Text 2\Packages\User\switch_to_file_type.py


class switch_to_file_type(sublime_plugin.TextCommand):
    def run(self, edit, extension):

        view = sublime.active_window().active_view()
        name = view.file_name()
        if name:
            import re
            view.retarget(re.sub('\.^\.]+$', '.'+extension, name))

            import os
            os.rename(name, re.sub('\.^\.]+$', '.'+extension, name))

            if extension == 'html':
                view.settings().set('syntax', 'Packages/HTML/HTML.tmLanguage')
            elif extension == 'css':
                view.settings().set('syntax', 'Packages/CSS/CSS.tmLanguage')
            elif extension == 'javascript':
                view.settings().set('syntax', 'Packages/JavaScript/JavaScript.tmLanguage')
            elif extension == 'json':
                view.settings().set('syntax', 'Packages/JavaScript/JSON.tmLanguage')
            elif extension == 'xml':
                view.settings().set('syntax', 'Packages/XML/XML.tmLanguage')
			elif extension == 'sql':
                view.settings().set('syntax', 'Packages/SQL/SQL.tmLanguage')

I restarted sublime text 2 and opened a new file (default file type) -> “Plain text”.
I typed a few words and pressed “Ctrl+Shift+J”. I don’t see any file type change.

Any tips what I am doing wrong? Thanks!

0 Likes

#8

dl.dropboxusercontent.com/u/930 … -error.txt

0 Likes

#9

Maybe try renaming that class to SwitchToFileTypeCommand?

0 Likes

#10

Also you don’t need to use a plugin for this. If you open your console and type in “sublime.log_commands(True)”, and then switch syntax settings, you can see exactly what command is being run and then just bind that to the key.

0 Likes

#11

Isn’t this core functionality of the Command Palette? That is, after opening Sublime, I can type Ctrl+Shift+P followed by “ssql” (short for “Set Syntax: SQL”) to change the syntax to SQL.

This may not be as fast as a single key binding, but it seems like having a specific binding for each file type would be less than ideal.

0 Likes

#12

@adzenith & @tito - thanks guys for your replies.

I’ve changed the command name but the issue persists.
I set the console log to True (sublime.log_commands(True)) and this is the output that I get when I prest Ctrl+Shift+j.

command: SwitchToFileTypeCommand {“extension”: “xml”}

@QED1224 - I wasn’t aware that the Command Palette allows to switch file type (syntax) so easily. I just never noticed it.
Your tip will certainly do the trick for my needs.

Thanks you all guys for your time and help with this. Please, consider my issue as resolved! Thanks again!

0 Likes