Search code examples
sublimetext3sublimetextsublime-text-plugin

Is there a Sublime Text equivalent to this "piece wise copying of the line above" macro?


I recently switched from gvim to Sublime Text 3. I am looking for an equivalent, or something relatively close to this incredibly useful vim macro:

" Piece-wise copying of the line above the current one
:imap <C-L> @@@<ESC>hhkywjl?@@@<CR>P/@@@<CR>3s

The macro copies the line above, word by word. As you repeat the shortcut, it advances the cursor on the current line.

It is useful in all kind of declarative statements that tend to be aligned over several lines. Here is a crude example of what it would do when editing short CSS declarations. The dollar sign represents the cursor position after using the shortcut:

#foo .bar { color:red; }
#$         <--- I pressed the shortcut 1 time...
#foo$      <--- ...................... 2 times...
#foo .$
#foo .bar$
#foo .bar { $
#foo .bar { color$
#foo .bar { color: $

What a "word" is, is not particularly important. If the macro copies "#foo" in one word or "#" and then "foo", it is still very useful.

PS: in case I end up trying to create it myself, should I look into creating a plugin or do you think a macro would do the trick?


Solution

  • Here is a relatively close equivalent (tested in Sublime Text 3). Designed to work with Vintage mode, macro shortcut to be used while in insert mode.

    The macro doesn't work without Vintage mode and I can't be bothered making a non-Vintage version since it is a relatively obscure question/answer, and this version works for me. If someone wants to make a non-Vintage version, or better yet, a macro that works both ways, that would be a nice contribution to this question.

    Turns out it's not too complicated by recording and saving a macro. I used visual mode because Vintage mode "yank" of a word (yw) didn't seem to record properly.

    Macro steps:

    • Move up
    • ESC (goes back to command mode)
    • v (visual)
    • e (select to end of word)
    • y (yank)
    • Move down
    • p (paste)
    • A (uppercase A > insert mode at end of line)

    Setup

    • Save the following gist into a macro file, for example Piece-wise-copying-line-above.sublime-macro (wherever your Sublime packages/etc are)

    • Add a shortcut to Key Bindings file. For example:

      { "keys": ["ctrl+l"], "command": "run_macro_file", "args": {"file": "res://Packages/User/Piece-wise-copying-line-above.sublime-macro"}},