2

当您在 Sublime 中选择多个单词(通过按住 Ctrl 键)并将它们粘贴到一个位置时,默认情况下每个单词与下一个单词之间用 \n 分隔。我想将其更改为逗号,并且可以选择在每个单词周围加上引号。我想知道是否有一种简单的方法可以做到这一点。任何帮助表示赞赏。

4

1 回答 1

1

Here is what I would do with some build-in shortcuts:

  1. somehow select the lines you just paste (eg. expand_selection_to_paragraph, which is mapable)
  2. ctrl+shift+l to make them into multi-cursor
  3. ctrl+j to join lines
  4. move cursor to end of each selection by moving right, insert comma, which will be between each selection.

If you want to write a plugin to simplify it (and customise to your exact need). Here is a very crude one I use to make a selection into a python list object. Feel free to modify and map to a key.

import sublime, sublime_plugin

class PythonListifyCommand(sublime_plugin.TextCommand):
    """Change a selected text into a Python list structure.

    Basically this is done by replacing space, comma, tab with comma.  
    Then Python list brackets are attached to both start and end to make 
    a Python list.

    If ther are multiple selections, then multiple lists will be created.
    If any selection is of zero length, then that particular line will 
    be listified.  There will always be a comma at the end of each line, 
    this is to make sure multiple line list can connect up.  Python does 
    not mind extra comma at the end of a list inside bracket.
    """
    def listify(self,line):
        import re
        no_leading = re.sub('^[ ,\t]+','',line)
        no_leading = re.sub('\n[ ,\t]+','\n',no_leading)
        in_line = re.sub('[ ,\t]+',',',no_leading)
        end_line_comma = re.sub('\n',',\n',in_line)
        return end_line_comma

    def run(self, edit):
        sel = self.view.sel()
        view = self.view

        for s in sel:
            if s.a == s.b:
                r = view.line(s)
            else:
                r = s
            rep = '[' + self.listify(view.substr(r)) + ']'
            if rep[-1] is ']' and rep[-2] is '\n':
                rep = rep[:-2] + ']\n'
            view.replace(edit, r, rep)
于 2014-05-28T21:58:18.753 回答