Search code examples
pythonsublimetext3sublime-text-plugin

Sublime Text and Python plugin: How to incorporate a python code with a for loop in sublime text editor


I am relatively new to using python scripts as plugins in Sublime Text (Note: I am not a developer). However, I am in a situation in which I always end up with the same certain events and I need to automate some operations on a string through a script.

For example, suppose I have the following values in a text file:

@article{jentsch2010multiple,
      title={The multiple hybrid bootstrap—Resampling multivariate linear processes},
      author={Jentsch, Carsten and Kreiss, Jens-Peter},
      journal={Journal of Multivariate Analysis},
      volume={101},
      number={10},
      pages={2320--2345},
      year={2010},
      publisher={Elsevier}
    }

    @article{buhlmann1997sieve,
      title={Sieve bootstrap for time series},
      author={B{\"u}hlmann, Peter},
      journal={Bernoulli},
      volume={3},
      number={2},
      pages={123--148},
      year={1997},
      publisher={Bernoulli Society for Mathematical Statistics and Probability}
    }


@article{kreiss2011bootstrap,
  title={Bootstrap methods for dependent data: A review},
  author={Kreiss, Jens-Peter and Paparoditis, Efstathios},
  journal={Journal of the Korean Statistical Society},
  volume={40},
  number={4},
  pages={357--378},
  year={2011},
  publisher={Elsevier}
}

What I want is to make each in a line i.e.

@article{jentsch2010multiple,   title={The multiple hybrid bootstrap—Resampling multivariate linear processes},   author={Jentsch, Carsten and Kreiss, Jens-Peter},   journal={Journal of Multivariate Analysis},   volume={101},   number={10},   pages={2320--2345},   year={2010},   publisher={Elsevier} }
@article{buhlmann1997sieve,   title={Sieve bootstrap for time series},   author={B{"u}hlmann, Peter},   journal={Bernoulli},   volume={3},   number={2},   pages={123--148},   year={1997},   publisher={Bernoulli Society for Mathematical Statistics and Probability} }
@article{kreiss2011bootstrap,   title={Bootstrap methods for dependent data: A review},   author={Kreiss, Jens-Peter and Paparoditis, Efstathios},   journal={Journal of the Korean Statistical Society},   volume={40},   number={4},   pages={357--378},   year={2011},   publisher={Elsevier} }

Currently, what I do is that I copy everything to the text editor (i.e. Sublime Text 3) and then I paste it in as a string value in Python to execute the code.

Here's what I have:

A='''
@article{jentsch2010multiple,
  title={The multiple hybrid bootstrap—Resampling multivariate linear processes},
  author={Jentsch, Carsten and Kreiss, Jens-Peter},
  journal={Journal of Multivariate Analysis},
  volume={101},
  number={10},
  pages={2320--2345},
  year={2010},
  publisher={Elsevier}
}

@article{buhlmann1997sieve,
  title={Sieve bootstrap for time series},
  author={B{\"u}hlmann, Peter},
  journal={Bernoulli},
  volume={3},
  number={2},
  pages={123--148},
  year={1997},
  publisher={Bernoulli Society for Mathematical Statistics and Probability}
}

@article{kreiss2011bootstrap,
  title={Bootstrap methods for dependent data: A review},
  author={Kreiss, Jens-Peter and Paparoditis, Efstathios},
  journal={Journal of the Korean Statistical Society},
  volume={40},
  number={4},
  pages={357--378},
  year={2011},
  publisher={Elsevier}
}
'''

print(
    '\n'.join(
        [' '.join(para.splitlines()) for para in A.split('\n\n')]
    )
)

However, I noticed it is possible to do all of this in Sublime Text so I do not have to copy and past the entire string and then execute the script.

The link is here. However, the link says explicitly that documentation on this topic is scarce. Can someone help me with this?

All I know so far about the topic is the following procedure:

1) Select Tools | Developer | New Plugin in the menu. This will open the following:

 import sublime
    import sublime_plugin


    class ExampleCommand(sublime_plugin.TextCommand):
        def run(self, edit):
            self.view.insert(edit, 0, "Hello, World!")
  1. How could I adjust this code to add my code?

Thank you so much!


Solution

  • Create the following plugin as mentioned in the question i.e. Tools ---> Developer ---> New Plugin ... and save it any name you wish with .py at the very end.

    Plugin:

    import sublime, sublime_plugin, re, string   #import the required modules
    
    class ConvertCommand(sublime_plugin.TextCommand): #create Text Command
        def run(self, edit):   #implement run method
            for region in self.view.sel():  #get user selection
                if not region.empty():  #if selection not empty then
                    s = self.view.substr(region)  #assign s variable the selected region
                    replace = '\n'.join([' '.join(para.splitlines()) for para in s.split('\n\n')])
                    self.view.replace(edit, region, replace) #replace content in view
    
    
    class UndoCommand(sublime_plugin.TextCommand): #create Text Command
        def run(self, edit):   #implement run method
            for region in self.view.sel():  #get user selection
                if not region.empty():  #if selection not empty then
                    s = self.view.substr(region)  #assign s variable the selected region
                    replace = '\n'.split([' '.join(para.splitlines()) for para in s.split('\n\n')])
                    self.view.replace(edit, region, replace) #replace content in view
    

    This has created a new command called Convert and Undo

    Menu view

    Then create a sublime-menu that will create a new command called convert this is saved as the id name

    Remark: SUBLIME-MENU files are used to control the main and context menu in Sublime Text.

    Menu: Save as context.sublime-menu

    [
    { 
        "caption": "Convert", 
        "id": "convert", 
        "children":
        [
        {
        "caption": "Convert",
        "command": "convert"
        },
        {
        "caption": "Undo Convert",
        "command": "undo"
        }
        ]
    }
    ]
    

    Select the text -> Right Click Convert

    Keybindings

    Preferences ---> Key Bindings
    

    Then write this

    [
    {"keys": ["alt+enter"], "command": "convert"},
    ]
    

    This will allow you to select and simply press on your keyboard alt+enter will allow you to do the task.