osx - Shortcut to convert multiple lines to comma seperated values in Sublime

08
2014-07
  • Nitin Gurram

    I am using sublime 3 in Mac OS.

    I want to convert

    1
    2
    3
    

    to 1,2,3 using keyboard shortcuts. or any plugin which can convert vice versa with keyboard shortcut. I don't want to press 10 keys to make this happen and that too much confused keyboad of MacBook.

    Please let me know, how can I configure Sublime/plugin which can do?

  • Answers
  • davidholcer

    In sublime text three, you can do this by selecting all (command+A), Split into lines (command+shift+L), click the right arrow, type a comma, click left arrow twice, click delete, Expand Selection to Line (command+L), click the right arrow, and hit delete. However, you stated that you wanted a keyboard shortcut for this. You can find a copy of a macro I created here. You want to put this file in

    ~/Library/Application Support/Sublime Text 3/Packages/User/

    Once that is there, go back to Sublime and click on the menu item Sublime Text, Preferences, and Key Bindings - User. Paste this into the document that opened and save it.

    [ { "keys": ["command+option+,"], "command": "run_macro_file", "args": {"file": "Packages/User/add commas.sublime-macro"} } ]

    The default key combination I put is command, option, comma. To change the keyboard shortcut, change the text inside the square brackets [ ] keeping the spaces after each combination. Save the file, and go back to the document you wanted converted to CSV. Press your keyboard combination, and you're good to go.
    Hopefully this works, if not, or you have other questions, share them and I will try to answer them.

  • Seasoned Advice (cooking)

    For the number inside the vertical bar, and no space before each new line (e.g)

    |1|
    |2|
    |3|

    you can find a macro for that here.

    For the number inside the vertical bar and a space before the new line (e.g [note the spaces])

    |1| 
    |2| 
    |3| 

    you can find a new macro here. You need to add these to the same place...

    ~/Library/Application Support/Sublime Text 3/Packages/User/

    and once again modify the User's key bindings. This time, the Key Bindings file should look something like this...

    [ { "keys": ["command+option+,"], "command": "run_macro_file", "args": {"file": "Packages/User/add commas.sublime-macro"} }, { "keys": ["command+option+shift+,"], "command": "run_macro_file", "args": {"file": "Packages/User/with spaces.sublime-macro"} }, { "keys": ["command+option+function+shift+,"], "command": "run_macro_file", "args": {"file": "Packages/User/no spaces.sublime-macro"} } ]

    You can find a copy of the file here. This file, as you may have noticed, goes in the same location as the macro files (~/Library/Application Support/Sublime Text 3/Packages/User/). Once you have that there, you can take out some of the macro shortcuts by deleting from "keys" to the }}, part, or you can add or edit the keyboard shortcuts. If you need any other macro, you can create a macro by clicking tools and record macro (control+q), and then stop recording macro (control+q again). Then you can hit save macro and save it in the default location (~/Library/Application Support/Sublime Text 3/Packages/User/). A macro is a single instruction that expands automatically into a set of instructions to perform a particular task. Hopefully this works, and if you have any other questions I'm glad to help.


  • Related Question

    microsoft excel 2010 - Get unique comma separated values from list of comma seperated values
  • MELL

    I have a list of csv in a single cell, and I need to get the unique values from that list and put them in another cell. For example, in one cell I would have:

    DIV-154, FOD-371, UDL-913, DIV-154

    And in another cell I would like to get this as output:

    DIV-154, FOD-371, UDL-913

    I am aware of the Text to Columns and Remove Duplicates functions, but neither of these are quite what I want. Also, this can only be in Function form, it cannot be VBA.

    As a closing note, I have searched for an answer to this question to no avail. If it exists somewhere on this site, I apologize and would appreciate a link to that thread.


  • Related Answers
  • pnuts

    After Text to Columns and assuming data is then in ColumnsA:D starting in Row2, this should work (copied down as appropriate):

    =A2&IF(B2<>A2,", "&B2,)&IF(AND(C2<>B2,C2<>A2),", "&C2,)&IF(AND(D2<>C2,D2<>B2,D2<>A2),", "&D2,)

    It just strings together, with a comma and a space in between, each unique entry for each row.