1

So, if I have text like this:

int x() {
int a = 1;

return a;
}

Trying to indent the inner part of the function does not indent the blank line. (Using visual mode and >)

How can I change the behavior of vim so that the blank line is also indented?

nijoakim
  • 930
  • 10
  • 25
  • 9
    Are you saying you *want* a line with only white spaces? – Peter Rincker Sep 29 '15 at 16:09
  • 4
    What exactly does an indented blank line look like, other than unnecessary whitespace bytes bloating your files? – twalberg Sep 29 '15 at 16:19
  • @Peter Rincker: Yes that is correct! – nijoakim Sep 30 '15 at 14:26
  • @twalberg: It shows saner indentation guides if I have tab show as e.g. pipes. For example: `:set list` and `:set listchars=tabs:|\ `. Also, it doesn't mess up my cursor's position when going up and down and it makes it easier to create a new line with the correct indentation. (Pressing `o` on a completely empty line produces a new completely unindented line.) – nijoakim Sep 30 '15 at 14:30
  • 1
    @nijoakim `o`/`O` and `S`/`cc` both work correctly. It is more than likely that you do not have `filetype plugin indent on` in your `vimrc`. Also using white space as you wish will affect useful paragraph motions like `{` & `}` and `ip` & `ap`. Your cursor position should also be fine as vim stores the virtual column so going from a long line to a short line back to a long line does not alter the cursor column. You may want to look into `'virtualedit'` if wish to strictly keep the cursor position. See `:h 'virtualedit'`. – Peter Rincker Sep 30 '15 at 15:10
  • @PeterRinckner: Yes, your suggestions solves all my problems but one. Indentation guides are still displayed with gaps in them. Reading other discussions (http://stackoverflow.com/questions/5917956/why-is-indentation-in-empty-lines-bad, http://stackoverflow.com/questions/2727988/python-indentation-in-empty-lines) has convinced me that it may be worth the trade off, though. Thank you for your suggestions. – nijoakim Sep 30 '15 at 20:25
  • Add `set smartindent` in your .vimrc. – NeoZoom.lua Apr 14 '21 at 06:24

1 Answers1

1

I am trying to offer just a very simple way how to do it. You can try to use blockwise Visual mode with CTRL-V (or CTRL-Q on Windows) to choose a block you wish to indent (start at column one). Then (you need to have +visualextra feature; use :version to find out if you have it) you can press I (I mean Shift-i) CTRL+TEsc. Or you can do it by mapping

vnoremap <Leader><Key> I<c-t><Esc>

More on these :h v_b_I:h i_CTRL-T.

But I really think you should not use a line with only white spaces.

ryuichiro
  • 3,765
  • 1
  • 16
  • 21