Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sublime Text 2: How to make Sublime run a command (fold code) when opening a file by default?

I would like to make it so that whenever I open a file in Sublime it will automatically do "Fold Level 2" Coding which is command shortcut Ctrl-K,Ctrl-2 (or CMD-K, CMD-2). I use both mac and pc.

I don't want to enter that shortcut everytime, instead I would like Sublime to automatically run that on opening a file. Please let me know if there a way to do that.

like image 670
Risha Avatar asked Jan 11 '13 22:01

Risha


People also ask

How do I run a shortcut code in Sublime Text?

You can use the shortcut key Ctrl+Shift+Q on Windows and Cmd+Shift+Q for Mac, to play a macro recorded by the user in Sublime Text.

How do I run a program in Sublime Text 2?

To run code in Sublime Text, go to Tools > Build System, and select the language for your code (Sublime comes with support for various languages like Python, Ruby, Bash, and more). Next, press Cmd+B on Mac or Ctrl+B on Windows to run your code.

How do I run a Sublime Text file?

To run the code, press Command B or go to Tools -> Build. As you can see, my Sublime Text is running Python 2.7. To change this to Python 3.7, we have to add a “Build System.”


1 Answers

I think that the best solution to your problem is Buffer Scroll plugin. It remembers and restores a lot of things, folding included.

If you don't want to install that plugin, you can create your own:

  1. Create new plugin Tools / New Plugin...
  2. Insert code
import sublime, sublime_plugin

class Folding(sublime_plugin.EventListener):
    def on_load(self, view):
        view.run_command("fold_by_level", {"level": 2})
  1. Save it in your User directory with the filename you prefer.

This will set folding level to 2, for every file you open.

like image 87
Riccardo Marotti Avatar answered Sep 19 '22 12:09

Riccardo Marotti