2

I'm fairly new to Vim, but have now used it quite a bit for a month or two. I'm using a couple of different plugins, but are now stuck at a problem...

Whenever I'm coding in Python, I like to have :set autoindent on, so I don't have to make all the indentations, whenever I make a new line. BUT! ... If I, for example, write this code:

def foo(self):
   pass

   def __init__(self):
      var = 42

And hit 'Enter' in Vim after '42', then my autoindent will automatically make two tab-indentations, which I have automatically converted to 6 spaces (I can't remember if it's the :set tabstop=3 or if it's the :set shiftwidth=3 that does it). But let's say, that I'm regretting that new line, and want to delete it. I'm used to other IDEs (like Eclipse or Dreamweaver, where I could just backspace once, which would delete the new line and take me to the end of the line above.

I know, that I could just press 'dd', and delete the line... But I'm in 'Insert-mode', when I do it. So in order to do that, I would have to exit Insert-mode, press 'dd', and then enter Insert-mode again, which in my opinion, is quite a hassle for such a simple operation.

Oh Holy Vim-masters in the sky! Hear my prayers and help me enhance my Vim-mechanics!

3 Answers 3

5

Here's options that I use:

  • Ctrl + D: Dedent one level (does not delete content of the line)
  • 0, Ctrl + D: Dedent all (does not delete content of the line)
  • Ctrl + U: Delete all characters before (left) the cursor, and move cursor at the beginning of the line.

Try :help i_ctrl-d / :help i_0_ctrl-d / :help i_ctrl-u

Sign up to request clarification or add additional context in comments.

1 Comment

That's awesome! Now I can achieve what I wanted with only two actions, regardsless if number of indentations (by typing CTRL-U + backspace). Ideally, I would like to have something I could insert in my .vimrc-file, that made automatically deleted all spaces left of the cursor and the new-line, if the line was blank. I'll leave the question open, to see if someone can come up with that - but if not, then I'll mark your answer as the solution.
1

Undoing a premature enter press is a simple task, you shouldn't be forced to fall back to normal mode for that. What falsetru suggested works, but I prefer to do <c-w><c-w>('ww' holding ctrl), which stands for deleting the last word you typed (the word to the left of your cursor). You could map <c-w> to a function that does <c-w><c-w> on empty lines, but frankly I don't believe that's worth it just to avoid a simple double tap.

Comments

1

You can use insert-normal mode, which drops you into normal mode for one command, and immediately back into insert mode.

  • C-o (insert-normal)
  • dd or d$

if you want to re-use the line...

  • C-r" (paste from default register)

You can use insert-normal for a lot of other things as well.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.