I am currently using vim and wish to autoindent every time I need to (such as after a brace in javascript or after a colon in Python). For some reason, I tried autoindent and smartindent, but every new line gave me 2 tabs instead of one. Why is it doing this? I thought it would only insert one tab.
My current ~/.vimrc file has:
set ts=4
set autoindent
set smartindent
filetype plugin indent on
you need to set up as well:
:set shiftwidth=4 softtabstop=4
tabstop is only for the number of columns a real "\t" tab takes:
:he shiftwidth
Number of spaces to use for each step of (auto)indent. Used for
|'cindent'|, |>>|, |<<|, etc.
When zero the 'ts' value will be used.
:he softtabstop
Number of spaces that a <Tab> counts for while performing editing
operations, like inserting a <Tab> or using <BS>. It "feels" like
<Tab>s are being inserted, while in fact a mix of spaces and <Tab>s is
used.
whereas tabstop:
:he tabstop
Number of spaces that a <Tab> in the file counts for. Also see
|:retab| command, and 'softtabstop' option.
As a bonus, here's a few mappings I've set up for that, when I need to work on projects that do not use my favorite default of expand tabs with 4 spaces indent:
nmap <Leader>t2 :set expandtab tabstop=2 shiftwidth=2 softtabstop=2<CR>
nmap <Leader>t4 :set expandtab tabstop=4 shiftwidth=4 softtabstop=4<CR>
nmap <Leader>t8 :set expandtab tabstop=8 shiftwidth=8 softtabstop=4<CR>
nmap <Leader>T2 :set noexpandtab tabstop=2 softtabstop=2 shiftwidth=2<CR>
nmap <Leader>T4 :set noexpandtab tabstop=4 softtabstop=4 shiftwidth=4<CR>
nmap <Leader>T8 :set noexpandtab tabstop=8 softtabstop=8 shiftwidth=8<CR>
I use like this in my .vimrc
filetype plugin indent on
set smartindent
set autoindent
set shiftwidth=4
set expandtab
set tabstop=4
set softtabstop=4
The expandtab will replace the tabs with spaces, I think this will be perfect for coding.
Related
This is weird that I used to make it work with my vimrc setting to replace every tab with two spaces, but it doesn't work now.
My old setting was like this:
syntax on
filetype on
set softtabstop=2
set shiftwidth=2
set tabstop=2
set expandtab
set smartindent
But it suddenly didn't work so I have tried this:
syntax on
filetype plugin indent on
set softtabstop=2
set shiftwidth=2
set tabstop=2
set expandtab
set smartindent
with no success(it forces 4 spaces).
How can I make it work to replace tabs with 2 spaces?
Just do this much
syntax on
filetype plugin indent on
set smarttab
set shiftwidth=2
set tabstop=2
set shiftround
" converting tabs to space before writing hte a file
set expandtab
autocmd! bufwritepre * set expandtab | retab! 2
This is not a very simple question.
I found that the "shiftwidth" for python files are set to 4, while I have these at the bottom of my .vimrc:
set tabstop=2
set shiftwidth=2
set softtabstop=2
set expandtab
The shiftwidth are 2 for all other filetypes; but it's 4 only for python.
I want to know if it's possible to see where a variable (in my case, the shiftwidth) has been changed; if this is not possible when what are the possible locations that this variable is changed..? And is there any way go make the shiftwidth=2 universally... even for the python files? (Although it's also recommended of using 4 spaces for python, but I personally prefer 2)
:verbose set shiftwidth
tells you what's the current value and where it was defined. See :help :verbose.
Filetype plugins are sourced after the corresponding FileType event was triggered so they will override the options in your vimrc.
The dirty way to add filetype-specific settings to your config is to add something like this to your vimrc:
augroup Python
autocmd!
autocmd fileType python setlocal tabstop=2 shiftwidth=2 softtabstop=2 expandtab
augroup END
See :help :autocmd.
The clean way is to put your desired settings into a custom ftplugin.
The file:
~/.vim/after/ftplugin/python.vim
Its content:
setlocal tabstop=2
setlocal shiftwidth=2
setlocal softtabstop=2
setlocal expandtab
Note the use of setlocal instead of set because you don't want those settings to be applied everywhere.
This is probably being set by the built-in ftplugin file for Python. If you
:e $VIMRUNTIME/ftplugin/python.vim
and search for shiftwidth, you’ll probably find it set there to 4.
I'd like to default to soft tabs, indented two spaces (but hard tabs displayed as two spaces for Makefiles), and for vim to reindent appropriately on save.
See this question. For your case, you would want this in your .vimrc:
set tabstop=2
set softtabstop=2
set expandtab
And this in ~/.vim/after/ftplugin/make.vim:
setlocal noexpandtab
You can use autocmd for this, so it pretty much becomes a oneliner in your.vimrc
autocmd Filetype jade setlocal ts=2 sw=2 expandtab
autocmd Filetype yaml setlocal ts=2 sw=2 expandtab
Everything else will still use the global defaults.
I want normally always use four spaces to indent code.
Unfortunately for example Makefiles enforce the use of tabs as separator.
My idea now was to set the tab key to four spaces and some extra key (e.g. tab + shift) for the real tabs.
How do I set something like this up?
Currently my ~/.vimrc looks like:
syntax on
:set tabstop=4
:set cindent
:set autoindent
You may prefer something like this as well:
set expandtab
autocmd FileType make setlocal noexpandtab
This will always convert tabs to spaces, except when you are editing Makefiles.
Ctrl+V Tab will insert a literal tab, even if expandtab is set. If you prefer, you can map this to Shift+Tab with :inoremap <s-tab> <c-v><tab>.
You need to use shiftwidth, e.g.,
:set shiftwidth=4
:set expandtab
You may also need to use this command to convert existing tabs to spaces:
:retab
:filetype plugin on should be enough to have the tab key always insert real tabs in makefiles, using file type detection.
See :help vimrc-filetype.
While I generally prefer to convert everything to spaces, I have a specific command in my .vimrc to prevent doing so in makefiles. First I activate expandtab and set shiftwidth, but then I turn off expandtab for makefiles. Order is important here.
set shiftwidth=4
set expandtab
if has('autocmd')
autocmd FileType make set noexpandtab
endif
I also recommend the following, as they are somewhat related and possibly helpful:
set softtabstop=4
set shiftwidth=4
set smarttab
set autoindent
set nohlsearch
set ai
set bg=dark
set showmatch
highlight SpecialKey ctermfg=DarkGray
set listchars=tab:>-,trail:~
set list
autocmd BufRead *.py set smartindent cinwords=if,elif,else,for,while,try,except,finally,def,class
set tabstop=4
set shiftwidth=4
set expandtab
set autoindent
set smartindent
syntax on
set listchars=tab:>-
set listchars+=trail:.
set ignorecase
set smartcase
map <C-t><up> :tabr<cr>
map <C-t><down> :tabl<cr>
map <C-t><left> :tabp<cr>
map <C-t><right> :tabn<cr>
map <F1> <Esc>
imap <F1> <Esc>
set pastetoggle=<F5>
This is my vimrc. I want to fix it so that it doesn't show >---- >---- when other people tab. Thank.
Remove these lines:
set listchars=tab:>-,trail:~
set listchars=tab:>-
set list
is doing this. You can
:set nolist
if it is bothering you but I recommend keeping it for python files (you might be coding in python looking at that autocommand). ie. ... Learn to love it. create a mapping if it is a regular issue.
Also there is
:retab
to remove tabs if you have an expandtab setting.