145 points · by joshearl · 14y ago · 97 comments
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>
Fortunately, other editors are already adopting similar features. Just today I was fiddling around with Ace, a web-based code editor, and noticed that they support multiple cursors too! Just select multiple words while pressing the Ctrl (or Cmd) key.
Live demo: http://ace.ajax.org/build/kitchen-sink.html
The nicest thing is that I only found this by accident because I'm so used to using multiple cursors in ST2. It's a great feeling to be surprised that something works just the way you sub-conciously expected :)
Yup, work's being done to add this to Emacs as well although it is not quite there yet:
I'd love it if ST managed to replace vim. I (very unfortunately!) don't see it happening soon. Vim just has so many features and so many plugins.
NOTE: Half of my plugins in Vim are only there to make vim behave like a decent, modern editor. In fact, half of my plugins in Vim are there to make it behave more like Sublime Text.
But, as you said, multiple cursors is a lot like visual feedback for macros. I miss such a "live" feel sometimes.
You got me interested: I'm looking forward for your Vim plugin.
But there is one use-case of macros that cannot be replaced with multiple cursors -- one where the places being affected by the macro are themselves computed by the macro.
Btw, another place this can't be used is across multiple files, at least the way ST does it.
def qux(bar, baz, qux)
pass
def quuux(grim, gram, grom)
pass
def quuuuuux(hip, hop, zoom)
pass
Delete second argument (of 3+ arg functions): /def<CR> # search
qd # record as 'd'
2f,dF,n # find second arg
q # done recording
@d # again (or n to skip)
Example intentionally simplified/convoluted. I'm doing stuff like this on the spot routinely, and there's no way multiple cursors could do this because I apply verbs to text objects that ST has (unless I'm missing something) no way of knowing about in a visual paradigm without resorting to guessing what I mean.What would be killer is mixing both, i.e visually selecting where to apply a vim macro.
No code duplication. No goto
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.
Not a great example, because this only leads to code duplication. 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? " comentify/uncommentify a line or a visual block"
function! Komment()
if getline(".") =~ '^[ \t]*#'
let hls=@/
s/^\([ \t]*\)#\(.*\)$/\1\2
let @/=hls
else
let hls=@/
s/^\([ \t]*\)\(.*\)$/\1#\2
let @/=hls
endif
endfunction
:vnoremap // :call Komment()<CR>
:nnoremap // :call Komment()<CR>
If you want to use c style comment :
function! Komment()
if getline(".") =~ '^[ \t]*\/\*'
let hls=@/
s/^\(\s*\)\/\*\(.*\)\*\/\(\s*\)$/\1\2\3/
let @/=hls
else
let hls=@/
s/[ \t]*$//
s/^\(\s*\)\(.*\)\(\s*\)$/\1\/\* \2 \*\//
let @/=hls
endif
endfunction
:vnoremap <silent> // :call Komment()<CR>
:nnoremap <silent> // :call Komment()<CR>
edited formatting>> 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.
" 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>
endifIs there some sort of trick you use?
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.
Sublime has the eye candy, that I was talking about. It looks great. The 'birds eye' view is nice, but not particularly useful. There is nothing that it can do that it can do that Emacs/vim can't do or couldn't do with a few minutes of macro-recording/scripting. This is why I am saying it isn't worth the money. The open-source alternatives are superb.
I haven't paid for Sublime (yet), but I'm still much less annoyed by the occasional nag-boxes than by modes. I don't see a future in emacs/vim, but I am anxiously awaiting an intelligent editor such as LightTable.
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.
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 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-| sortI guess that's what I get for not using one editor for everything :p
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.
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. { "keys": ["f8"], "command": "openInBrowser" },
Didn't work on my windows machine when I made it.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.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) ..
CTRL+SHIFT+P - X - Enter
The first part opens the command palette, the second selects XML syntax, and enter applies it.
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.