8 ms·
Handy text manipulation tricks in Sublime Text 2
- dmarble 14y agoSublime Text 2 – Useful Shortcuts https://gist.github.com/1736542 https://gist.github.com/1736542
- creamyhorror 14y agoI think this is even handier than the original submission. Upvoted.
- jacobolus 14y agoFor whatever it’s worth, all of these are copied directly from TextMate. :-)
- jskinner 14y agoWhile I did try to be compatible with TextMate key bindings where possible, I believe only a handful of the key bindings mentioned in the article are actually common between the two.
- jacobolus 14y agoMost of them are identical. The capitalization and line joining shortcuts are slightly different.
- verisimilidude 14y agoSome of 'em are quite different. For instance, Textmate uses various modifiers on the 'u' key for capitalization.
- daurnimator 14y agoMy favourite feature is definently multiple cursors; Ctrl+D selects the next occurance of your highlighted word, Alt+F3 selects all occurances.
- oconnor0 14y agoDo you know if there's a way to skip the current or next match when doing this?
- cheatercheater 14y agoThat's interesting. Can you edit all selected occurrences at once? I don't think there's an immediate analogue in Vim.
- daurnimator 14y agoyep. I was amazed to find that Copy/Paste even works...
- edanm 14y agoThere is no analogue in Vim. It's the one thing I hate about vim.
- tincholio 14y ago:%s/original/new/g
- JonnieCache 14y agoOh yes. It gives you multiple cursors as discussed upthread, with multiple selections. You could then for example press backspace, leaving all the occurrences gone with the cursors left in their place, ready for you to type something new.
- pashamur 14y agoYou can also hold Ctrl and click to get another cursor
- jskinner 14y agoA few more handy ones: Line Manipulation: Select Line (press again to select subsequent lines): Command+L / Ctrl+L Split multi-line selection into multiple selections, one per line: Command+Shift+L / Ctrl+Shift+L Delete to BOL: Command+Delete / Ctrl+Shift+Backspace Delete to EOL: Control+K / Ctrl+K,Ctrl+K or Ctrl+Shift+Delete Delete Line: Control+Shift+K / Ctrl+Shift+K Insert Newline After: Command+Return / Ctrl+Enter Insert Newline Before: Command+Shift+Return / Ctrl+Shift+Enter Multiple Selections: Add next occurrence of current word to selection: Command+D / Ctrl+D Select all occurrences of current word: Control+Command+G / Alt+F3 Undo through selection changes: Command+U / Ctrl+U
- Kilimanjaro 14y agoI've changed some key bindings to suit my taste, like: Cmd-D: Duplicate line Cmd-L: Delete line Cmd-T: Insert HTML tag Cmd-B: Open in browser Ctrl+Up/Dn: Toggle lines Ctrl+a/e/i/o/u: Accented á é í ó ú Sublime Text 2 has become my favorite editor, fully customizable and plenty of plugins available.
- simonsarris 14y agoCan you share with us how you made these bindings? Specifically open in browser? using { "keys": ["f8"], "command": "openInBrowser" }, Didn't work on my windows machine when I made it.
- Kilimanjaro 14y agoHere is how I did it for Safari, change accordingly. Go to Tools/BuildSystem/NewBuildSystem... Type this in: { "cmd": ["open", "-a", "Safari", "$file"], "selector": "html" } Save it in your sublime/packages/user folder as 'Open in Safari.sublime-build' That's it. It should show up in your Tools/BuildSystem and should be selected for all html files. So when you hit Cmd+B it uses that build system to just show the file in Safari. There are other ways like mapping Cmd+B directly to a python snippet like: import sublime, sublime_plugin, webbrowser class OpenBrowserCommand(sublime_plugin.TextCommand): def run(self,edit): url = self.view.file_name() webbrowser.open_new(url) but for me the first option was simpler.
- mistercow 14y agoSublime Text makes me feel silly for loving a text editor so much. Now if only it would do text completion across multiple files...
- damncabbage 14y agoIs there a CTags plugin you can use?
- mistercow 14y agoPossibly, but that's a lot fancier and less "just works" than I'm interested in. I just want the same extremely simple "what words have I seen before" autocompletion that you get on a file-by-file basis, but as if I had every open file concatenated into one.
- asifskalam 14y agoYup, this - https://github.com/SublimeText/CTags https://github.com/SublimeText/CTags Install from package control.
- seclorum 14y agoMe too. I've been a vi/vim users for 20+ years, and I swear by vim for C/C++-based development - but now that I'm mostly doing Lua (MOAI), SublimeText2 has become my absolutely most loved tool. It is really one of those editors that became an instant hit, the moment I discovered Cmd-R, Cmd-T, and of course everyones favourite, Cmd-D. ;) For some reason, ST2 reminds me of the old PC-Write editor for DOS, in terms of raw features and power - I guess because PC-Write was the last editor I used that had proper column/row selection, multiple-cursors, and so on. I sure do miss that editor (switched to vim full-time as soon as PC-Write was EOL'ed and I moved off DOS to Unix) ..
- sirn 14y agoMy favorite: in HTML editing mode you can press Ctrl-Shift-A to select enclosing tag and modify both open and close tag at the same time. Very handy when editing HTML files (although I'm not sure if this is Sublime's built-in or from any third party package I installed.)
- damncabbage 14y agoVim equivalents if you're feeling jealous (matching the order in the article): J Join line. ddp Swap line with the one below. ddkP Swap line with the one above. dd Delete current line yyp Duplicate line gq} Rewrap a paragraph. Vu Lowercase a line VU Uppercase a line Sort the selection by the Unix `sort` command (eg. sort -rn for reverse numeric order): V <select your lines> :!sort It goes a bit pear-shaped for these ones, though: Title-Case: :s/\<\(\w\)\(\w*\)\>/\u\1\L\2/g Twiddle/Flip Case: function! TwiddleCase(str) if a:str ==# toupper(a:str) let result = tolower(a:str) elseif a:str ==# tolower(a:str) let result = substitute(a:str,'\(\<\w\+\>\)', '\u\1', 'g') else let result = toupper(a:str) endif return result endfunction vnoremap ~ ygv"=TwiddleCase(@")<CR>Pgv I'm a bit stumped on toggling comments, though. Any ideas?
- patrickaljord 14y ago> I'm a bit stumped on toggling comments, though. Any ideas? http://www.vim.org/scripts/script.php?script_id=1218 http://www.vim.org/scripts/script.php?script_id=1218
- slurgfest 14y agoSimpler alternative: tComment http://www.vim.org/scripts/script.php?script_id=1173 http://www.vim.org/scripts/script.php?script_id=1173
- shreyas-satish 14y ago>> J Join line. >> ddp Swap line with the one below. >> ddkP Swap line with the one above. >> dd Delete current line >> yyp Duplicate line >> gq} Rewrap a paragraph. These shortcuts work in Sublime too, with Vintage mode enabled.
- amix 14y agoFor swapping lines I recommend following shortcuts: " Move a line of text using ALT+[jk] or Comamnd+[jk] on mac nmap <M-j> mz:m+<cr>`z nmap <M-k> mz:m-2<cr>`z vmap <M-j> :m'>+<cr>`<my`>mzgv`yo`z vmap <M-k> :m'<-2<cr>`>my`<mzgv`yo`z if has("mac") || has("macunix") nmap <D-j> <M-j> nmap <D-k> <M-k> vmap <D-j> <M-j> vmap <D-k> <M-k> endif
- cageface 14y agoNow that I've gotten used to first class refactoring support in the Jetbrains products any other editor just seems archaic. IntelliJ has all the features mentioned in this article and intelligent code restructuring.
- _casperc 14y agoDoes it allow you to have multiple projects open at the same time yet? That was what prevented me from using it earlier.
- cageface 14y agoYes: http://www.jetbrains.com/idea/webhelp/opening-multiple-projects.html http://www.jetbrains.com/idea/webhelp/opening-multiple-proje...
- _casperc 14y agoSorry, I should have been more clear. I meant multiple projects in the same workspace, like what netbeans or eclipse has (or even Sublime). I really like being able to navigate in all my open projects at the same time. Not having it is a no-go for me.
- scottjad 14y agoNot a complete list, but some Emacs equivalents: M-^ delete-indentation (or join-line) C-x C-t transpose-lines kill-line M-0 C-k kill to beginning of line djcb-duplicate-line (not built-in) M-q fill-paragraph C-c c comment-uncomment M-c capitalize-word... org-move-line-up C-u M-| sort
- yoklov 14y agoM-^ is one of my favorite commands in emacs. I use both sublime and emacs on a daily basis (Sublime has better support for the languages I use at work), and it's a constant source of frustration that C-j in Sublime text joins the line below with the current line, whereas M-^ joins the line above. I guess that's what I get for not using one editor for everything :p
- RegularK 14y agoI often paste into it texts from other sources, and enable syntax highlight quickly using the command palette. For example, if I paste an XML into a new tab, I'd click CTRL+SHIFT+P - X - Enter The first part opens the command palette, the second selects XML syntax, and enter applies it.
- forbes 14y agoI use both Emacs and vim on a daily basis. Emacs is my main editor and vim is for quickly dropping in to make a change in a terminal either locally or on a server. Both are amazing. Both are rock solid. Both are infinitely extensible. Both are free as in beer and as in freedom. I'm not afraid of paying good money for software. I don't think it is necessary to pay for a closed-source text editor when such good open-source options exist that can do everything the commercial product can do and more. TextMate (and now Sublime Text) have been popular in the hacker community, particularly with Rails people. The only advantage I can see is eye-candy. They look great on Macs. I tried TextMate and put it in the trash as soon as I found it had no split windows. Not only that, I couldn't add split windows even if I had the time. If vim or Emacs is missing something, you can add it. End of rant.
- huskyr 14y agoHave you actually tried Sublime? It has split windows. And you can extend is pretty easily using packages. I've read many posts of vi and Emacs hardcore users who actually liked Sublime.
- munchor 14y agoIt's also closed source, meaning we can't fix something if we want to. We can't see what's going on back there. When it comes to a tool that I use every day with personal data, I really want it to be open source. My browser and my text editor definitely need to be open source. Additionally, Emacs is really amazing, and like many others, I've been building this init.el for years (in my case months), meaning Emacs and I are really made for each other right now.
- wccrawford 14y agoI actually did use the fact that MacVim is open source to fix a bug one day. Well, actually, someone else had already figured it out and provided a patch, but it wasn't in the main line yet and I really, really needed it that day. But I was already on the track to fixing it when I found someone else's patch.
- 14y ago
- bemmu 14y agoI'm doing Google App Engine Python stuff with Sublime Text 2. Is there any way to get it to somehow guess at function signatures? So if I start writing do_something(... I could see the args that do_something() expects. Assuming it's just a function that is plainly defined somewhere in my project folder would be enough for me. Another thing is when I am testing the app in localhost and it spits out error messages indicating the line number(s) where I sinned, it would be nice to somehow be able to jump to them.
- aaroneous 14y agoHave you seen CodeIntel? https://github.com/Kronuz/SublimeCodeIntel https://github.com/Kronuz/SublimeCodeIntel
- 1285964 14y agoControl + G will let you jump to a line number. Also, Ctrl + F and then a colon (:) will do the same thing. I use it all the time and it it extremely handy.
- edanm 14y agoAny article that talks about tricks in Sublime Text 2 without mentioning Multiple Cursors is... lacking (no offense to the OP). For anyone that doesn't know it, Multiple Cursors is the ONE BRILLIANT FEATURE that Sublime Text has. Don't get me wrong, tt's great in many other ways too, but Multiple Cursors is, IMO, a feature that should become part of every friggin' text editor in existence, it's that important. And it only exists in Sublime Text. It gives you multiple cursors at seperate places in your document. Each one acts completely independently, moving around with your commands. Everything works as expected, including copy-pasting (it will match the copy-pastes up to your cursors). Most text editors have a column-select mode, except that Multiple Cursors are the same thing on steroids: 1. The selections don't have to be contiguous. So if you have a bunch of: <div> <p> Text And more Text</p> <p> Something </p> </div> You can put the cursor only on the <p>s. 2. Since each cursor acts independently, you can do operations that you simply can't do in column mode. For example, place the cursor on each <p>, select to the end of that </p>, and delete the text. This is even if the contents of the </p> are completely different. 3. Multi Cursor gives the same power as macros, but: They're visual! You see all the changes you're making in realtime. Any long-time vim user has probably recorded a macro, tried playing it back, and discovered that it didn't work. Well, with Multi Cursors, you see the cursor and changes in real-time. Make a wrong command? Undo it instantly. Want an example of another actual use case? I used to have a plugin for Sublime Text that let me split a selection into multiple selections, based on a character. So, let's say I had this code: foo(int a, char b, char c) I could select the paramater list, run my shortcut to split by ",", then I'd get a different selection on each paramater. Then, I could simply multi-select three lines below it inside the actual function, paste, and I've got this code: foo(int a, char b, char c) int a char b char c A dead-simple editing task I do 300 times a day. Another example? Let's say I have two "if" blocks in the top of a function: if (somecondition): return if (anothercondition): return Now, let's say for a second that I just realized I don't want to return, I want to do some processing, then return. This happens a few times a day for me. I just multi-select both "if"s, hit enter, and add the code. If it's two functions and I need to use the function names, I can even go back, copy the function name, and paste it as part of the processing!! Phew. If it seems I'm a fanboy for Multiple Cursors, I totally am. It's a brilliant tool that only exists in Sublime Text. I've unfortunately gone back to Vim, and am too far in Vim's world to manage working in Sublime Text, which is why I miss multiple cursors so much! But I AM working on a Vim plugin that gives Vim Multiple-Cursors. Been working on it for a while, but I think I'm getting near a more-or-less stable version. Hopefully, it will be released soon. </way-too-long-rant>
- warmwaffles 14y agoI delete lines with the cut command.
- mmcnickle 14y agoYou can also rebind these to any key combination you like. Sublime Text 2 is great, and they have a free (unlimited) demo; no excuse to not try it out. Well worth the money for me.
- FootballMuse 14y agoSort is F9 on my machine, not F5.