6 ms·
I can't answer your whole question or we'll be here all day. Here's one important example, which has the advantage of applying in vim as well as emacs: "Common
by mechanical_fish 14y ago
I can't answer your whole question or we'll be here all day. Here's one important example, which has the advantage of applying in vim as well as emacs:
"Common" macros are the least interesting. The most interesting macros are the ones with no name, the one-time ones that you build on the fly. You have a file with ten thousand lines of:
foo.rb
bar.rb
baz.rb
…
… [9997 more lines like these]
and you need to turn the file into something like this:
// Run the foo command
def foo()
end
// Run the bar command
def bar()
end
// Run the baz command
def baz()
end
… [9997 more blocks like these ]
That's 56 keystrokes in emacs: You record a macro that transforms one line appropriately, then execute it 9999 more times, all on the fly in about twenty seconds.
- TorKlingberg 14y agoFor that I would probably have used a regex search and replace in any decent editor, or perhaps a Perl one-liner. cat file | perl -p -e 's-\s(.)\.rb-// Run the $1 command\ndef $1()\nend\n-'
- sophacles 14y agoAs a heavy user of regex one liners, I agree in this case, but as a heavy user of vim, there are lots of times when a macro can be recorded and run over a file long before a proper regex can be constructed, particularly in multi line settings, or those things that would require heavily nested backrefs.
- sophacles 14y agoFor completeness - same task in vim: start recording a macro: qa Then enter this sequence of keystrokes[1]: 0veyO// Run the ^R" command<esc>j0f.C()<cr>end<cr><esc> Then exit macro recording with: q Then enter the command: :g/.rb$/ norm @a to run the macro on every line ending with .rb (allowing comments in the input, just because (of course it would be nice to make that regex in :g look for "lines that start with comments" and ignore them explicitly)). If I counted right, thats 57 keystrokes, and I might have been slightly inefficient in my usage, of course, if that one keystroke is part of a command my fingers just do, there is no effective difference :) [1] I know it was exactly that sequence because I just pasted the macro buffer, which of course can then be edited, saved back to the buffer and run in edited form.
- weaksauce 14y agoI'm an intermediate user of vim but the filtering the macros to only run on those lines is a great tip! I did it a bit different but we had a similar idea.
- christiangenco 14y agoIn Sublime Text 2 using multi cursers: 1. `command + A` to select all 2. `command + shift + L` to split the multi-line selection into multiple single line selections (one curser at the end of each line) 3. `delete delete delete` to get rid of ".rb" on each line 4. `command + shift + ←` to select the identifier 5. `command + C` to copy each identifying word on each line (!!!!) 6. `←` to get to the beginning of the line 7. Type "// Run the " 8. `command + V` to paste each identifier on each line correctly (!!!!) 9. Type " command\ndef " 10. `command + →` to go past the identifier already there 11. Type "()\nend\n" Also took about twenty seconds. This multi curser thing has been completely redefining how I mass edit code - it's quite fantastic, and as far as I know ST2 is the only editor that does it this wonderfully.
- SCdF 14y agoMultiple carots, the minimap and ST2 picking your indentation strategy automatically by looking at the file (I work with badly formatted legacy code a bit, there is no project-level strategy that applies) are the three reasons I bought it.
- silentbicycle 14y agoSome of my coworkers use sublime, and have been very excited about it. I used vim for several years and use emacs now, so probably a hard sell. Still, I get curious when people are Really Excited about other editors (because there may be ideas worth stealing and adding to emacs ;) ). What's the "minimap", the scrollbar that is a shrunken version of the document? The "multiple carets" feature seems handy. Are there any other standout features, compared to vim or emacs (not, say, TextMate)?
- roryokane 14y agoYes, the minimap is what you guessed it was. Other ST features that Vim/Emacs don’t have (by default, at least): • You can use proportional fonts (I use Verdana) • The interface chrome is graphical, not text in a grid. This allows nice details such as a drop-shadow from the minimap when it covers the text and proportional fonts in the file tree’s filenames. • On a Mac with smooth scrolling, you can scroll smoothly pixel-by-pixel, instead of choppily line-by-line. • You can easily rename a variable, by using Command-D repeatedly to select the instances of the names inside multiple carets. But I usually forget to do this until after I’ve already renamed just one instance of the variable, and then I can’t select the other instances without undoing that edit first, so it’s not that useful. • It uses Python as its scripting language, so you can use Python libraries easily, and don’t have to learn an editor-specific language. Similarly, it uses JSON for settings. But Sublime Text doesn’t have nearly as many editing shortcuts by default as Vim. For instance, there isn’t any hot-key to select inside a quoted string – Expand Selection to Scope is the closest, but it’s hard to predict what it will do and so it isn’t very reliable. You can enable Vintage mode, which emulates many Vim key-bindings and thus gives you access to that power. That mode has a few small problems – the biggest one for me is that the Esc shortcut to exit the current mode sometimes conflicts with the Esc shortcut to remove extra carets so you have just one. It’s too bad Sublime Text isn’t open source. Its plugin API doesn’t allow setting tabstops manually, which I would like so I could write a plugin implementing true elastic tabstops. Since I can’t implement it myself, I have no option but to beg the developer to implement that and wait.
- weaksauce 14y agosimilar thing for vim. qrIdef<space><esc>f.Di()<return>end<esc>jq qr ' record a macro into register r I ' insert mode at start of line def<space><esc> ' type def then exit insert mode f. ' find the dot D 'delete the rest of the line i()<return>end<esc> ' type () return end j ' next line down q ' exit macro recording call it with 9999@r This looks unwieldy but it's all done interactively. (also if you mess up and you want to rework the macro you can paste it out with "rp to paste the r register rework it and then put it back into another register. 0"ry$ (start at column 0 then into r register r yank to end of line)