How to wrap each line in quotes in SublimeText?

36,864

Solution 1

I would use a multiple cursors approach like this:

Windows

  1. Ctrl + A (Select everything)
  2. Ctrl + Shift + L (Split into lines)
  3. End (Put the cursor at the end of the line)
  4. " (Add the quote at the end of the line)
  5. Home (Go to the first character of the line)
  6. Home (Go to the beginning of the line... like if you have tabs or spaces)
  7. " (Add the quote at the beginning of the line)

Mac

  1. Cmd + A (Select everything)
  2. Cmd + Shift + L (Split into lines)
  3. Cmd + (Put the cursor at the end of the line)
  4. " (Add the quote at the end of the line)
  5. Cmd + (Go to the first character of the line)
  6. Cmd + (Go to the beginning of the line... like if you have tabs or spaces)
  7. " (Add the quote at the beginning of the line)

Solution 2

Method 1:

  • no multiple cursors
  • + best performance (use for large files)
  • - slightly clumsy

Replace (.*) with "\1"

Method 2:

  • multiple cursors
  • + best in regards to usability/comfort
  • - slower for bigger files
  • - wont work if the file has empty lines

Ctrl+a, Ctrl+Shift+l, "

Method 3:

  • multiple cursors
  • + close to #2 as usability, but works always
  • - slower for bigger files

Ctrl+a, Ctrl+Shift+l, End, ", Home, "

Solution 3

None of these worked in Sublime Text 3 for a multi-column TSV file with tab spacing.

I found this worked for column 1:

Find: ^\s*\S+
Replace: "$0"
Share:
36,864
Aniket Suryavanshi
Author by

Aniket Suryavanshi

User Interface developer.

Updated on November 09, 2020

Comments

  • Aniket Suryavanshi
    Aniket Suryavanshi over 3 years

    Input:

    boston beach summer figural yellow blue
    boston floral flowers still still-life food pink figural
    boston horse pink purple house flowers floral figural
    

    Expected output:

    "boston beach summer figural yellow blue"
    "boston floral flowers still still-life food pink figural"
    "boston horse pink purple house flowers floral figural"
    

    The actual input file has 600+ lines, and I'm looking at a quick way to wrap each line in quotes? Does the method involve using multiple cursors? How about macros?

  • sergioFC
    sergioFC almost 9 years
    Good answer. You can also find ^|$ and replace for " . This way you don't use capturing groups in find nor $1 in replace, but I really don't know about the performance of this.
  • ndnenkov
    ndnenkov almost 9 years
    @sergioFC, this won't work for the empty lines though. It will insert only one " as there is only one position to match at. But you do have a point that in the cases it does work, it is faster than matching the entire line.
  • sergioFC
    sergioFC almost 9 years
    You are right it won't work if there are empty lines. Good point.
  • Aniket Suryavanshi
    Aniket Suryavanshi almost 9 years
    @ndn: I didn't get your first method. When I search for (.*), I get 'Unable to find (.*)' . May be I'm missing something.
  • ndnenkov
    ndnenkov almost 9 years
    @AniketSuryavanshi, you should enable regex search first if you haven't. For most people it's on by default. You can do that by checking the .* icon left of the search bar or by pressing Alt+r when your search is opened.
  • Maxime
    Maxime almost 9 years
    For me, method 2 is deleting everything and I'm left with only one quote.
  • sscarduzio
    sscarduzio almost 8 years
    cool, but: how in the name of god I exit this multi cursor mode now??
  • Maxime
    Maxime almost 8 years
    Simply click somewhere else. Or use the arrows to go to the bottom or top of the files and get only one cursor. :-)
  • ndnenkov
    ndnenkov almost 8 years
    @sscarduzio or press esc.
  • Devplex
    Devplex over 7 years
    You can do this with fewer steps. After Ctrl+Shift+L just press ". Sublime is smart enough to put quotes on both sides of the selected text.
  • Maxime
    Maxime over 7 years
    @MartijnHeemels If you have empty lines, your method won't work. Sublime is smart... but not enough! ;-)
  • Devplex
    Devplex over 7 years
    @Maxime Thanks, I didn't realize that.
  • Neeraj Gupta
    Neeraj Gupta about 7 years
    Wow. This answer has saved my day from regexing and substituting . thanks
  • Maximus
    Maximus almost 7 years
    You are a god among men. Thanks for saving me a lot of time. But, now that I'm billing my clients hourly I'll need to find some other tasks to do!
  • Matt
    Matt over 6 years
    @MartijnHeemels that doesn't work with US - International layout. The US layout is fine (I guess you use that one, can you confirm that?). I use US - International so that I can make a ü or é easily.
  • Devplex
    Devplex over 6 years
    @Matt It does work, but you'll need to type a <space> after the double-quote. After all, the computer is waiting to see what comes after the double-quote to determine if you meant to type an accented character. Typing <space> tells it you want the double-quote by itself. I use US International too, though I usually switch to US while programming.
  • Matt
    Matt over 6 years
    @MartijnHeemels I know, but then i end up with empty quotes/apostrophes, so it doesn't wrap the selection. Switch is indeed an option, and not to hard either.
  • Devplex
    Devplex over 6 years
    @Matt That's strange. I just tested it before posting my comment and in my case it does.