Sublime Forum

Moving Line Auto Indent

#1

When you move a line up or down, it doesn’t indent. Let me give an example.
If I have a for loop, and I wish to move a line down into that for loop, the line doesn’t auto-indent when entered inside the for loop (meaning that it’s on the same level as the for loop declaration and not on the level of the block of code as it should be. It seems like it should auto indent, and this would make the feature a lot more useful as other code editors already have this.

Example:

var nums = [1, 2, 3];
console.log(nums[count]);
for(var count = 0; count < nums.length; count++) {
    // Do something useful
}

// Move line down currently
var nums = [1, 2, 3];
for(var count = 0; count < nums.length; count++) {
    // Do something useful
console.log(nums[count]);
}

// How it should be
var nums = [1, 2, 3];
for(var count = 0; count < nums.length; count++) {
    // Do something useful
    console.log(nums[count]);
}

Although I realize indenting it doesn’t take that long, it would be great if Sublime Text would take care of that for us.

1 Like

#2

Right now you can enable auto-indenting from line to line along with auto alignment of code…

Are you specifically asking for the system to automatically put your cursor at the proper indent level when you click on a line, or press the up or down arrows?

You could do this with AutoHotkey - Add a Hotkey for Up and Down, and have it non-blocking ( use * or ~ modifier ) then have it SendInput End ( so you don’t up-arrow into the middle of code ) then press Tab - have this inside of an if statement checking that SublimeText.exe is not only running but active…

If you have the correct auto-indenting settings, pressing TAB will auto-Align instead of indenting further… I can’t recall which setting this was, but it was quite annoying for me to recover the freedom I prefer with tabs… You may want to do the opposite of me when it comes to tabs ( and quotes, brackets, etc… I hated it auto adding them and then when I tried ending with, or whatever it threw me out of whack… )

I could also be thinking of TamperMonkey in-Browser Editor ( It lets you SPAM the tab button and it’ll ensure it never indents more than appropriate - ie it simply jumps to the indent location – You may be able to bind TAB to have this behavior - if not it shouldn’t be difficult to write a script for it )… This type of functionality I do like… because it makes coding more efficient without getting in the way ( like the bracket, quote, etc… auto placement system and the auto-complete system is annoying as heck but nice to have if enter doesn’t trigger it, which you can set in config - additionally the one thing I still haven’t solved is using TABS between objects - it keeps triggering unrelated autocomplete data, and shift-tab doesn’t even work inside of a word because it unindents, etc… )

I digress… Apologies…

If you’re suggesting the first part - up / down arrow jump to end plus exact tab location - there should be a way to do it - if not an AutoHotkey script or Addon should be able to accomplish this ( especially if part 2 - tab locking is enabled )

If suggesting part 2 - the tab locking where you can spam the key and it jumps to the appropriate spot without getting in your way - again: also should be possible with a script, maybe even with a current keybind…

I’d love to know how to do either of these ( and correct the other tab issues related to jumping and autocomplete the latter is for another thread )…

Regardless, I would support the suggestion as long as the behavior is configurable ( moving up / down through text while inside, or where-ever is incredibly useful when lining up text, variables / data… but having a quick way to jump around without leaving the primary typing surface and out of the way ( home / end is out of the way but the arrows are just within range )

Can you please clarify the exact behavior you’re looking for?

0 Likes

#3

So what I was trying to ask is when I’m swapping lines via the shortcuts, how can I get the cursor to auto-indent if it’s lets say inside a block of code versus outside the block of code. Is there a way to do that currently? I’m not aware of it.

0 Likes

#4

Try creating a macro:
swap_line_down_and_indent.sublime-macro:

[
  {
    "command": "swap_line_down"
  },
  {
    "command": "reindent",
    "args": {
      "single_line": true
      }
  }
]

and
swap_line_up_and_indent.sublime-macro:

[
  {
    "command": "swap_line_up"
  },
  {
    "command": "reindent",
    "args": {
      "single_line": true
      }
  }
]
  • Save the files in Packages/
    User.
  • Next, update
    your custom keymap with the
    following:
{
  "keys": ["ctrl+shift+up"],
  "command": "run_macro",
  "args":
    {
      "file": "res://Packages/Uses/swap_line_up_and_indent.sublime-macro"
    }
},
{
  "keys": ["ctrl+shift+down"],
  "command": "run_macro",
  "args":
    {
      "file": "res://Packages/User/swap_line_down_and_indent.sublime-macro"
    }
}
2 Likes

#5

beware of any typos, theres one in the file argument :Uses ==> User, and i’m not sure wether the command is run_macro or run_macro_file.
Sorry i didn’t correct them myself, it took me alot of time to write the reply using my nokia c2

0 Likes

#6

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}]
    ],
  },
}
1 Like