Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

S/cc doesn't auto indent on empty line #1017

Open
alnorris opened this issue Oct 31, 2016 · 32 comments
Open

S/cc doesn't auto indent on empty line #1017

alnorris opened this issue Oct 31, 2016 · 32 comments

Comments

@alnorris
Copy link

alnorris commented Oct 31, 2016

When your in Normal mode and your on an empty line and the cursor is at the very left, pressing 'S' in normal Vim auto indents the line and goes into insert mode, but VS Vim doesn't seem to indent at all.

Technical details:

  • VSCode Version: 1.6.1
  • VsCodeVim Version: 0.4.0
  • OS: OSX El capitan

What happened:

  • Typing cc when cursor is at col 0 and in block on empty line: Jumping to input mode, but won't indent correctly
  • Typing cc when cursor is at col 2 (or any col): Removes line and ends up at col 0 with no indentation
  • Typing cc when cursor is at col 0 but there is valid code on line that is inside a block: Removes line as it should do, but cursor ends at col 0 with trailing whitespace
  • Typing cc when cursor is at col >2 (in block, line with valid code, cursor on code): Removes line of code and indents correctly ✅

Here all of those scenarios in a GIF (with tab width of 3 instead of 2)
cc

Contributed by @lumio at #1828.

What did you expect to happen: Typing cc will always clear the line, indents it and cursor jumps to end of line.

How to reproduce it: See above, but typically typing cc on different col positions.

@rebornix
Copy link
Member

Nice catch, if the cursor is at somewhere between first char or last char, S works as expected but doesn't work if it's at the beginning of the file.

@rebornix
Copy link
Member

rebornix commented Nov 1, 2016

Not sure for what reason cursor updating in commands which invoke delete no longer works, I saw this commit change 3dad7ca#diff-7addcd1a8a241060a73b1fe4abb39960R1269 so @johnfn might have an idea about that.

I take a quick look but now I'm confused, take S for example, if we want to control the cursor position after running delete operator, how can we do that? delete operator is now a postponed action.

@johnfn
Copy link
Member

johnfn commented Nov 2, 2016

@rebornix we use PositionDiffs now. We have a "beginning of line" position diff that just tells the cursor to go to the beginning of the line after the transformation is applied. We use that a few times...

@Chillee
Copy link
Member

Chillee commented May 23, 2017

This problem is fixable once the "reindent a selection" command is added, per microsoft/vscode#19847 (comment)

@Chillee Chillee changed the title 'S' doesn't auto indent on empty line S/cc doesn't auto indent on empty line Jun 2, 2017
@xiluo58
Copy link

xiluo58 commented Aug 27, 2017

Seems dd O works perfectly for me.
Is it possible to remap cc to dd O?

@Chillee
Copy link
Member

Chillee commented Aug 27, 2017

@xiluo58 That's an interesting idea actually. To be clear to anybody who was confused as me, he's suggesting ddO.

@knpwrs
Copy link

knpwrs commented Nov 17, 2017

That works for me!

    "vim.insertModeKeyBindings": [{
        "before": ["j", "k"],
        "after": ["<Esc>"]
    }, {
        "before": ["c", "c"],
        "after": ["d", "d", "O"]
    }],

It would still definitely be nice to see the default behavior fixed.

EDIT: Actually it seems like this doesn't work, seeing as though it's an insert mode key binding. I tried otherModesKeyBindings and it doesn't seem to work there.

@alexozer
Copy link

alexozer commented Jan 6, 2018

Any progress on this?

ddO sadly isn't a universal workaround either. For example, it won't work on the last line of a file.

ddO on c:

a
  b
  c

Produces

a
  _
  b

Also doesn't work when single line is indented:

ddO on b:

a
  b
c

Produces:

a
_
c

@asethwright
Copy link

asethwright commented Jan 9, 2018

I agree a proper indentation should be performed of the lines. This temporary fix seems to work for me. But the previous problems by @alexozer still apply.

    "vim.otherModesKeyBindings": [
      {
        "before": ["c", "c"],
        "after": ["d", "d", "d", "O"]
      }
    ],

Not sure why the third "d" is necessary.

@shaunlebron
Copy link

Hitting Tab after cc seems to do the right thing every time.

Is there a way to automate this?

@Chillee
Copy link
Member

Chillee commented Mar 9, 2018

@shaunlebron It doesn't if your cursor starts after the tab position.

@shaunlebron
Copy link

@Chillee do you have an example? would like to reproduce

@Chillee
Copy link
Member

Chillee commented Mar 9, 2018

@shaunlebron Take something like

  asd|f

where there's 2 spaces before the asdf.
Presing cc takes you to

  |

and a tab adds 2 more spaces, which is not the right thing.

@asilvadesigns
Copy link

any updates on this?

@rben01
Copy link

rben01 commented Aug 24, 2018

Another problem with remapping to ddO is that for isolated lines (single lines with whitespace immediately above and below), there is no preceding line to base indentation off of, and so O creates the new line above with no indentation. For instance consider the following Python code:

def f():  # next line intentionally left blank

    ret|urn  # | represents the cursor

In vim, cc would clear the line and place the cursor at the correct indentation (four space from the left margin, in this example). But in VSCode Vim, since there's no line above return to base indentation off of, it clears the line and places the cursor at the beginning of the line (outside the function body).

Python isn't the easiest language to infer desired indentation, but when the desired indentation is already given, it should be respected when using cc -- independent of any VSCode capabilities or settings.

That said, I think a better temporary keybinding for a single cc would be ^C. However that's not easily repeatable -- 5cc is not achievable by repeating ^C

@aldanor
Copy link

aldanor commented May 18, 2019

Has anyone here managed to figure it out?.. Seems to be an issue still.

@casio
Copy link

casio commented Nov 2, 2019

Sorry to revive such an old thread - but is this really not solved yet?
In python it's really annoying and given the love python gets elsewhere in vscode, this appears a bit strange.

@rben01 s workaround of using ^ C works best for me.
So much that I'd like to remap it actually, but that doesnt work either:

  "vim.normalModeKeyBindings": [
    {
      "before": [
        "c",
        "c"
      ],
      "after": [
        "^",
        "C"
      ]
    }
  ]

Somebody got a better idea?

@J-Fields
Copy link
Member

J-Fields commented Dec 30, 2019

There's nothing we can do about this - it's an upstream problem. After a command like cc, we run editor.action.reindentselectedlines, which requires indentation rules to be specified for the current language to work correctly.

Seems like these are relevant issues. Go upvote them please!
Python: microsoft/vscode-python#8996
C++: microsoft/vscode-cpptools#883

@Raikiri
Copy link

Raikiri commented Jan 28, 2020

How about using some kind of workaround until it's fixed properly upstream? It's a very annoying problem and I don't think it's good for the end user to just shove it under the rug waiting for it to potentially be fixed someday somewhere else. Like, O action for example could be implemented just by doing end+enter macro or something like that.

@zeroxia
Copy link

zeroxia commented Apr 4, 2020

Hi I have Visual Studio Code 1.43.2, vim extension 1.13.1.
I cannot remap cc, is it also a known bug?
I've included the following chunk in my settings.json, "leader, m" works, but the cc remap does not.

    "vim.normalModeKeyBindings": [
      {
        "before": [ "c", "c" ],
        "after": [ "^", "C" ],
      },
      {
        "before": [ "<leader>", "m" ],
        "commands": [ "bookmarks.toggle" ]
      },
      {
        "before": [ "<leader>", "b" ],
        "commands": [ "bookmarks.list" ]
      }
    ]

This is frustrating, especially when I see this issue was raised in the year of 2016, and now it's 2020.

@Spirarel
Copy link

Spirarel commented Apr 6, 2020

@zeroxia see #4464 (comment)

I agree, it's frustrating, but then this is open source...

@ColdSpirit0
Copy link

Have that problem with C#. "Reindent selected lines" does not work for it, but indent adds when use "o". So I made that workaround. But it only works if upper line not empty too.

    "vim.normalModeKeyBindingsNonRecursive": [
        {
            "before": ["S"],
            "after": ["d", "d", "g", "k", "o"],
        },
    ],

@MrHate
Copy link

MrHate commented May 2, 2020

Such kind of key binding works well indeed.
However, it now makes u confuse me. It considers the combination as the combination itself, so I need to undo for 3 times.
I think an update to implement a real S should be better indeed.

@gaoshan0621
Copy link

gaoshan0621 commented Jun 24, 2020

offer a workaround for "o", which I use frequently:

    "vim.normalModeKeyBindingsNonRecursive": [
        {
            "before": [
                "o"
            ],
            "after": [
                "o", "_",
            ],
            "commands":[
                "editor.action.formatSelection",
                "deleteLeft",
            ]
        },

@sql-koala
Copy link
Contributor

I have noticed something. "cc" does not automatically indent, while "o" and "O" do, at least most of the time.
Therefore, a fix should be possible without upstream/vscode, or not?

@berknam
Copy link
Contributor

berknam commented Dec 3, 2020

@sql-koala Is this still an issue? Because I can't reproduce this. Both cc and S auto indent for me, even on empty lines.

@J-Fields
Copy link
Member

J-Fields commented Dec 3, 2020

Indentation after both o (editor.action.insertLineAfter) and cc (editor.action.reindentselectedlines) is determined by the language support, but they're specified differently, so some language plugins support the former better than the latter. Maybe some supports the latter better than the former, but none that I've found.

I write C++ and python for my job and o works great while cc doesn't in both of these. Typescript and rust, however, both work as expected.

@J-Fields J-Fields changed the title S/cc doesn't auto indent on empty line S/cc doesn't auto indent on empty line Oct 15, 2021
@fwcd
Copy link

fwcd commented Jun 27, 2022

@J-Fields Which part of the language support handles this indentation? Is it the TextMate grammar, the formatter, the language-configuration.json or even something else?

There are many languages (like Swift) which currently aren't supported very well by S/cc either and I would like to help improve support for those.

@Clev7
Copy link

Clev7 commented Aug 19, 2022

remapping cc to ddko seems to work. It seems that o formats differently from O. There might be exceptions though.

@CalebMDMI
Copy link

CalebMDMI commented Nov 9, 2023

I've been looking for answers to this for a while, and it's still broken. There are workarounds that get close, but no perfect solutions yet. The main thing I want is for cc<escape> to remove any indentation as it does in Vim, and none of the workarounds I've found achieve this.

@rben01
Copy link

rben01 commented Nov 9, 2023

I've been looking for answers to this for a while, and it's still broken. There are workarounds that get close, but no perfect solutions yet. The main thing I want is for cc<escape> to remove any indentation as it does in Vim, and none of the workarounds I've found achieve this.

There is always https://github.com/vscode-neovim/vscode-neovim

@J-Fields
Copy link
Member

@fwcd If I remember correctly, it's indentation rules documented here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests