3

I recently gave atom a try. Despite being too slow, there was one thing I liked: the way auto indent worked, basically everywhere.

I found here how to make Paste + Indent the default in sublime text.

But I'd also like that the:

{ "keys": ["ctrl+shift+up"], "command": "swap_line_up" },
{ "keys": ["ctrl+shift+down"], "command": "swap_line_down" },

become swap_line_up_and_indent so that if I have:

console.log('hello');
function() {

}

and I ctrl+shift+down, I get:

function() {
  console.log('hello'); // indented yai!!
}

Any chance?

2

So, the easiest way to do this (aside from writing a rather extensive plugin) is by macro. Here is swap_line_down_and_indent.sublime-macro:

[
    {
        "command": "swap_line_down"
    },
    {
        "command": "indent"
    }
]

and swap_line_up_and_indent.sublime-macro:

[
    {
        "command": "swap_line_up"
    },
    {
        "command": "move",
        "args":
        {
            "by": "lines",
            "forward": true
        }
    },
    {
        "command": "indent"
    }
]

Save the files in Packages/User where Packages is the folder opened when selecting Preferences → Browse Packages…. Next, edit your custom keymap with the following:

{ 
    "keys": ["ctrl+shift+up"], 
    "command": "run_macro_file", 
    "args": 
    {
        "file": "res://Packages/User/swap_line_up_and_indent.sublime-macro"
    } 
},
{ 
    "keys": ["ctrl+shift+down"], 
    "command": "run_macro_file", 
    "args": 
    {
        "file": "res://Packages/User/swap_line_down_and_indent.sublime-macro"
    } 
}

and you should be all set. There are a couple caveats, though. These macros will only indent one level, so you may need to use Ctrl] to indent the line further. Also, it is assumed that the line to be indented is the lower line after the swap_line_[up|down] command is run.

|improve this answer|||||
  • 2
    Thanks for beautiful answer. The macro itself did not work as suggested, but I fixed it by doing: [ { "command": "swap_line_down" }, { "command": "reindent", "args": {"single_line": false} } ]. Same for going up. – Augustin Riedinger Jun 17 '16 at 8:44
  • @AugustinRiedinger that worked perfectly for me – Aᴄʜᴇʀᴏɴғᴀɪʟ Apr 7 '17 at 9:01
0

You can do this as well by using the 'Chain of Command' package. Install the package and just add the following to the user key bindings file:

{
   "keys": ["ctrl+super+up"], 
   "command": "chain", 
   "args": {
     "commands": [
       ["swap_line_up"],
       ["reindent", {"single_line": false}]
     ],
   },
 },
{
  "keys": ["ctrl+super+down"], 
  "command": "chain", 
  "args": {
    "commands": [
      ["swap_line_down"],
      ["reindent", {"single_line": false}]
    ],
  },
}
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.