I'm a novice in Vim and configuring it for writing python scripts comfortably.
Is the a way to automatically put four spaces after I jump onto the first line of while True:
loop ?
Vi and Vim Stack Exchange is a question and answer site for people using the vi and Vim families of text editors. It only takes a minute to sign up.
Sign up to join this communityI'm a novice in Vim and configuring it for writing python scripts comfortably.
Is the a way to automatically put four spaces after I jump onto the first line of while True:
loop ?
You want to have this setting:
" ---- Minimal configuration:
set smartindent " Do smart autoindenting when starting a new line
set shiftwidth=4 " Set number of spaces per auto indentation
set expandtab " When using <Tab>, put spaces instead of a <tab> character
" ---- Good to have for consistency
" set tabstop=4 " Number of spaces that a <Tab> in the file counts for
" set smarttab " At <Tab> at beginning line inserts spaces set in shiftwidth
" ---- Bonus for proving the setting
" Displays '-' for trailing space, '>-' for tabs and '_' for non breakable space
set listchars=tab:>-,trail:-,nbsp:_
set list
Minimal configuration
smartindent
is the option for automatically indenting when starting a new line. Note that a new line after a line starting with a keyword from cinwords
is automatically indented. That means, whatever the file type is, a new indentation is done after lines starting by while
,for
...
shiftwidth
sets the number of "spaces" when doing a smart indentation. Note that it will add as much tab characters as possible then the number of spaces needed (so if you set tab as eight space long and shiftwidth
to 12, it will put one tab character and four spaces). In your case you want to set it to 4.
expandtab
is the option for putting space instead of tab characters.
See:
:h smartindent
:h cinwords
:h shiftwidth
:h expandtab
Good to have for consistency
tabstop
sets the number of "spaces" a tab corresponds. By default it is set to 8 (GNU convention). You want to consider a tab as 4 spaces.
smarttab
corresponds to the behaviour when you insert a tab in a empty line. If it is set on: it will insert blanks according to shiftwidth
, when off: according to tabstop
or softtabstop
. You want it to be set on.
See:
:h tabstop
:h softtabstop
:h smarttab
Bonus
listchars
defines the display for different type of characters. In our case: tab are displayed >-
(-
is repeated for filling the size the tab takes), trailing characters are displayed -
and non-breakable space are displayed as _
. With this settings, you can see the difference between tabs and spaces. list
is set to activate the option.
See:
:h listchars
:h 'list'
Further reading
set autoindent
andset cindent
to your vimrc should do the trick. – statox♦ Jul 25 '17 at 11:08