Lukasz Madon
Lukasz Madon

Reputation: 14994

Insert an empty line without indentation

I'm writing some python and I would like to insert an empty line without any indentation e.g. Pressing Enter or Enter+cmd

if foo == "foo":
    foo.bar()
    foo.bar()
    foo.bar()
    # cursor is here 
    foo.baz()
    foo.baz()
    foo.baz()
    foo.baz()

I'd like it to be

if foo == "foo":
    foo.bar()
    foo.bar()
    foo.bar()
# cursor is here 
    foo.baz()
    foo.baz()
    foo.baz()
    foo.baz()

I believe this is similar to Visual Studio CTRL + Enter

In sublime I have created a command

[
    {
        "args":
        {
            "characters": "\n"
        },
        "command": "insert"
    },
    {
        "args":
        {
            "to": "hardbol"
        },
        "command": "move_to"
    },
    {
        "args":
        {
            "file": "res://Packages/Default/Delete to Hard EOL.sublime-macro"
        },
        "command": "run_macro_file"
    }
]

Upvotes: 3

Views: 1650

Answers (2)

Shaun Luttin
Shaun Luttin

Reputation: 141442

What you're looking for is a combination of two commands:

  • ctrl+enter editor.action.insertLineAfter
  • ctrl+k ctrl+x editor.action.trimTrailingWhitespace

There is a feature request for macro like keybindings. In the meantime you can try the macros extension, use the shortcuts one after another, or...

Add the following to your keybindings.json file, which essentially provides a ctrl+enter ctrl+m shortcut that does exactly what you need.

[
    {
        "key": "ctrl+m",
        "command": "editor.action.trimTrailingWhitespace",
        "when": "editorTextFocus && !editorReadonly"
    }
]

Upvotes: 3

Kenneth M Burling Jr
Kenneth M Burling Jr

Reputation: 19

What is it that you are trying to achieve?

Python uses line indention, rather than brackets, to create a block of code. The code presented above can be done in literally any text editor, but will throw an exception, because Python will be expecting instructions.

If your intent is to have an empty statement that does nothing, then try:

if foo == 'foo':
     pass

Pass tells the system explicitly to continue on without doing anything.

If you are looking to do this for stylistic reasons, then you are barking up the wrong tree. In order to function Python MUST have the next line after an if statement be indented. When the indentation goes away, that tells python that it's done with that block and leaves the if statement.

Upvotes: -1

Related Questions