Vim 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?
>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>
Unless I'm misunderstanding the purpose of your 'TwiddleCase' function, you can simply highlight the text that you want to flip and hit ~ to invert the case.
For 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
Vim commands are like a language - you don't route learn combinations, only the individual "words", and you string them together dynamically, like you would do with English words in a sentence.
It was a very good read. But not what I was expecting. If vim is indeed speaking a language this is what I would expect a vim tutorial to have.
1. Basic commands.
2. Syntax to chain basic commands.
3. Ability to form other commands from basic commands.
4. Way to write macros.
Instead most vim tutorials I have read present with a list of several commands to memorize and then a cheat sheet to navigate you through a jungle. Often they are letters, and not meaningful words. So they are difficult to put into ones brain. Couple that with vim's modes. And you are set with a perfect recipe for disaster in case of a new bie.
If vim is indeed speaking a language than manuals and tutorials teaching it, must explain how to speak that language.
This is 2012 and the need to memorize commands, and read cheat sheets to edit text doesn't belong to our times.
What I seem to get is vim aims to provide sed, grep, tr, cut etc kind of tools inside a text editor in form of some interplay of letters and words. How I haven't figured out till today's date.
Every time I've tried to learn vim, I've quit trying to memorize 'yy P...' kind of commands. That happens as I first can't memorize random arrangement of characters. Secondly if its not random, I've not understood how to arrange and use those letters to make meaningful commands.
1. "Basic commands" are what you know after 30 minutes of
$ vimtutor
2. "Syntax to chain basic commands" is what you get when you think about what you are going to do. Like in any spoken or written language: "copy this line and paste duplicate it 10 times above" would be
yy copy this line
10P paste above, 10 times
You have verbs (yank, paste, delete, join, move down…), direct objects (this word, this line, this html tag, this function, this paragraph…), indirect objects (up to this word, between line y and line x…) and, well… "counts" to do all that multiple times.
Vim's language is a lot more natural and intuitive and efficient than any variant of Cmd+Left - Shift+Ctrl+Right - Right - Cmd+c - Down - Cmd+v - Down - Cmd+v - Down - Cmd+v - Down - Cmd+v - Down - Cmd+v - Down - Cmd+v - Down - Cmd+v - Down - Cmd+v - Down - Cmd+v - Down - Cmd+v
Saying it's too arcane/complex/hard is just lying or being lazy.
3. "Ability to form other commands from basic commands" is achieved by writing small (or big) functions and bind them to your own commands.
4. "Way to write macros" is by not writing macros. You record a sequence of commands/keystrokes and run it later:
qa record a macro in register a
yypj duplicate this line and move to the line below
q stop recording
10@a apply the macro 10 times
No need for a cheat sheet if you are commited and don't rush it.
#3 is just using the commands in sequence. They aren't new commands.
#4 is advanced and I've never had a need for it yet.
Let's pick some apart:
ddp:
dd - delete line
p - paste line below
This swaps a line because when you delete a line, the line below it moves up and you end up on that line. When you paste an entire line, it pastes to the next line.
ddkP:
dd - delete line
k - move up 1 line
P - paste line above. (Shift usually means 'opposite direction', so it pastes above instead.)
I don't think of these as commands as such. I just think, I want to delete this line, and then I want to place it here. He's providing these as commands to show that Vim can handle the same features as Sublime Text, but he's doing it in a non-Vim way, as far as I'm concerned.
You don't memorize non-basic commands. You just use basic commands until you get what you need, and it happens that the advanced commands for most other editors can be done with just the basic commands of Vim.
He's providing these as commands to show that Vim can handle the same features as Sublime Text, but he's doing it in a non-Vim way, as far as I'm concerned.
As I said, learning the basic commands and using them. There's no need to remember a key combo for 'swap lines' because it's just 2 basic commands used in succession. I never even think of it as 'swap lines' because what I want to do is remove a line, then place it somewhere else. And that's what the commands are in Vim to make it happen.
Why would that be, vim seems to be difficult to use because a good manual that actually teaches how to use vim isn't available yet.
In fact it was surprising to me when I learned vim commands are actually a form of some terse language. Because I was only of an assumption that most vim commands are random string of characters you need to memorize. And when you memorize and practice enough you become productive.
I would be definitely well suited for vim, because I like terse language paradigms in unix like sed, awk, perl. I think if I had known about these aspects of vim a while back. I would have tried to write a few essays myself to understand using vim effectively.
> Why would that be, vim seems to be difficult to use because a good manual that actually teaches how to use vim isn't available yet.
What manuals have you tried? Have you tried `vimtutor`(if vim is installed, just run vimtutor from the sell).
> In fact it was surprising to me when I learned vim commands are actually a form of some terse language. Because I was only of an assumption that most vim commands are random string of characters you need to memorize. And when you memorize and practice enough you become productive.
You need to know a lot of words before you can form intelligible sentences. There is no not-knowing `h,j,k,l,esc,d,x,p,b,w,i,I...`. So yes, you do need to memorize basic operations before you can chain them. Most of the things in this cheatsheet are important to know http://tnerual.eriogerg.free.fr/vimqrc.pdf
And once you have learnt the basic, there is no manual to tell you how to combine them, because there is no syntax to do it - you just run them in sequence. It has already been pointed out in the SO thread, and various other threads here.
> I feel tempted to give vim a try now.
Your temptation will last all of 1 day. If you give up, vim isn't for you. It takes a minimum of 15 days to a month to s
top missing whatever editor you are using right now, then another month to actually start liking it.
I am emacs user. The problem isn't patience with vim. The problem really is deciphering the `h,j,k,l,esc,d,x,p,b,w,i,I...` sequence.
So as long I can't make sense of what random stream of characters mean, any amount of practice to put them into finger memory is going to be a exercise in frustration.
> The problem really is deciphering the `h,j,k,l,esc,d,x,p,b,w,i,I...` sequence.
> So as long I can't make sense of what random stream of characters mean, any amount of practice to put them into finger memory is going to be a exercise in frustration.
That wasn't a sequence - that was basic movements and operations you need to compose sequences.
d2w2jp - delete 2 words, move 2 lines up and paste(what was deleted)
d2w2j^p - delete 2 words, move 2 lines up, go to beginning of line(^) and paste(the deleted words)
df)/test$F{p
delete till ) (df)), search forward for test (/test), go to end of line where search is found($), search backward for { (F{), and paste(p)
They aren't random stream of characters if you know basic movements and operations.
As far as emacs goes, I find vim to be superior when it comes to navigation. When I am using emacs, I have a minimal configuration file for a few things I absolutely need:
"M-x goto-line line-number <CR>" is just too much typing for going to a specific line.
What emacs wins at is external tools integration. Vim doesn't support async command integration, and per the developers, never will. Vim developers believe vim is a text editor, and it has no business running shells; a stance I am ok with. But that means there never will be a slime for vim, at least not the way it works for Vim i.e you can't execute chunk of code, and be dropped in a debugger if an exception occurs. And there will never be a debugger which seamlessly integrates with vim. I do miss these things sometimes, but there are decent alternatives - I love my zsh, don't need it in an editor; slime, sans debugger is just configuring vim to send selection to repl etc etc.
I 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.
Have 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.
Yes I have. My comment about split windows was about TextMate, which 4 years later still doesn't have them. (Coming in TextMate 2, which will be released sometime after Half Life 3.)
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 don't want to need a cheat sheet to use my text editor, I already need them for the programming language, the regexes, mathematical notation, etc. I don't want to spend a few minutes doing macros/scripting, I want to spend my minutes on the problem domain I'm actually working on. I don't like modes (see NO MODES in the Bret Victor talk), and I like positioning my cursor with a mouse.
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.
Remembering shortcuts for Sublime would be no different to remembering shortcuts for Emacs/vim/whatever. I agree with the 'no modes' sentiment which is why I find Emacs more natural than vim for the bulk of my work.
If you forget the short-cut for a command in Emacs, you can hit M-x, type in the name of the command (with completion to help you) and once you execute the command Emacs will TELL you the shortcut for next time (if there is one assigned). Best feature ever.
It'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.
I 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.
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-^ 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
I'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.
Now 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.
Oh 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.
My 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.)
Any 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.
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.
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 :)
I guess you are aware Sublime Text 2 has a Vim mode. As someone who's gone "far in Vim's world", how do you think this compares to regular Vim? I'm starting to learn Vim commands in ST2 but I don't know if "proper" Vim is required to get fully into Vim.
I think it's one of the best vim-emulations I've seen so far. It's not 100% but it has things like text-objects (cit, etc.), named buffers and bookmarks (which most of the other emulations miss).
I'll be brutally honest. I think it's a great effort, and almost moved back to Sublime Text w Vintage mode several times. And each time, I'll run a command that Vintage mode doesn't have yet, and leave.
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.
I agree multiple cursors are awesome and largely can replace macros with real-time visible changes.
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.
/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.
Actually, this is a perfect example of using Multiple Cursors to get the job done more easily.
I'd select all three "def"s simultaneously, then move by word three/four times using ctrl+right, or plain old "w" if using ST's vim mode. When I'm on the second param, I'll delete forward to the next ",". I could also just ctrl+right my way until the next word, which would be the same.
Just to recap the keys I'd press:
/def (select first def)
ctrl+d * 2 (select using multi-cursors the next two defs
ctrl+right * 4 (to get to the second param)
ctrl+shift+right (to select the entire second param)
delete
And if I mess up at any point, I instantly see it. If I did something that only works for the first case but not the others, I see it immediately.
Note: Using vintage mode, Sublime Text has vim shortcuts. Without it, Multiple Cursors still rock, but are missing a LOT of their power (exactly because of situations like this).
In fact, in Sublime Text 1, for which vim mode didn't exist, I actually implemented a plugin to emulate the "f" and "t" movements because they were so good.
> When I'm on the second param, I'll delete forward to the next ","
you can't do that, else you'll delete not enough ('gra') or
too much ('baz,')
> ctrl+shift+right
It fails the moment one of the second arguments has a space or a dash. e.g default value:
def qux(bar, baz = {}, qux)
or in some languages (CSS, HTML, LISP)
<div class="foo bar-baz quux" data-behavior>
The interesting reason is why it fails: you say you want to delete 'to the next ","' but do word movement with w or ctrl+arrows. Indeed this is solved by Vintage mode, but I unfortunately found it too inconsistent with vim, and lacking a few critical features (e.g vim-surround and indent-object) for me.
Also, multiple cursors rock anyway, but if I want to apply them to a number of elements responding to a criteria and decide on a case by case basis if I want to apply then it does not work. Also it does not work when there's no code locality (i.e affected code is not visible in the same view, or spread around multiple files). The major difference with macros is that they're repeatable, while multiple cursors do the same thing at once, and once.
Also, when one vim user uses f/t too often, one should look into an object describing what he wants to really do (here [0]).
How do you make multiple cursors that are at the exact locations you need?
That's something that a macro is useful for. His macro is less useful because for some reason he did his search outside of the macro. Maybe the search is not for "def", but for "def prefix", so ctrl+d is not applicable to form the multiple cursors.
It could be nice if you could record your movements to form the "next" cursor and immediately see all future generated cursors given that movement (similarly to the "recursive draw" web app). Then you could perhaps truncate the infinite list of cursors at some finite point somehow, and then operate your multiple cursor operations.
"How do you make multiple cursors that are at the exact locations you need?"
There are a few simple tricks.
In this case, I hit "ctrl-d", which does the following:
1. If nothing is selected, it will select the word the cursor is on.
2. If something is selected, it will select the next match of this word, using another cursor.
In other words, I'll put my cursor on "def", hit ctrl-d, and "def" will be selected. Hit ctrl-d again, and the next "def" will be selected. Etc. Technically, in Sublime Text, every search makes a multiple cursor. I could hit alt-f3 (IIRC) to select all matches of "def", and they'd all be multi-selected.
But like I said, in this case it's really easy.
There are more advanced tricks - what if the word "def" appears inside the text? Well, you can select not only the def, but also the whitespace before the def, and it will selected the next "def" which is at that exact indentation. And like I mentioned in another comment, I even wrote a plugin to make multi-selections more easily, by splitting on a certain character. So that a list like this: (int a, int b, char c) can easily be split into 3 selections, around the comma.
Having switched to Vim, multiple cursors are the only reason I ever use Sublime Text anymore. They are insanely, amazingly awesome and powerful. I can't wait to see your efforts at porting the idea to Vim.
One note on your point about changing return behaviour: That's the ONE case a goto is IMO warranted. If I have to choose between a dummy function, code duplication and a goto, I choose goto.
well to each his own. In case of the cleanup phase before return I find it to be much cleaner with goto, since usually it's sub 10 lines and you'd have to program a function that gets pointers or objects passed that then get freed/destroyed, which I find ugly as hell. I look at such an outer procedure as an 'owner' of these objects and therefore I want to see the allocations and deallocations in plain text right there.
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.
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:
Me 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) ..
Possibly, 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.
I 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.
While 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.
Control + 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.