my autoindent is not working, any diagnostic tests to figure it out?
my ":set" is:
:set
--- Options --- cindent laststatus=2 scroll=17
tabstop=4 window=36
filetype=cpp number
smartindent ttyfast
helplang=en paste
syntax=cpp ttymouse=xterm2
backspace=indent,eol,start
fileencoding=utf-8
fileencodings=ucs-bom,utf-8,default,latin1
printoptions=paper:letter
runtimepath=~/.vim,/var/lib/vim/addons,/usr/share/vim/vimfiles,/usr/share/vim/vim72,/usr/share/vim/vimfiles/af
ter,/var/lib/vim/addons/after,~/.vim/after
suffixes=.bak,~,.swp,.o,.info,.aux,.log,.dvi,.bbl,.blg,.brf,.cb,.ind,.idx,.ilg,.inx,.out,.toc
try:
:set ai
or:
:set autoindent
find more about auto-indent:
:h ai
Otherwise, it's might be something with file type detection.
I had a stale function in indentexpr which persisted after changing the filetype. This eventually fixed it for me:
:set indentexpr=
In case someone else face the same issue, I had a similar issue that none of the above fixed.
What was wrong for me was the tab interpretations. here is the set up that made it work:
set expandtab
set tabstop=2
set shiftwidth=2
set autoindent
set smartindent
And to check when indenting if the indentation was correct, I added the following, still in my vimrc file:
" helper for indent mistake
set list listchars=tab:»·,trail:·
Which display a "»" instead of the regular "·" if my indent is wrong. Very handy.
Hope it helps.
I had the same issue and these settings fixed it.
filetype on
filetype plugin on
filetype indent on
You should probably turn off smartindent and use :filetype indent on and cindent (which seems to be also set) instead.
Here's one way to test out whether you have the configuration correct, then persist your configuration so Vim always operates thusly. This font indicates text which should be typed in literally, except <CR> means press the "Enter" or "Return" key.
Create a new system user, with a new home directory.
Start Vim. All settings should be set however they ship with Vim by default.
Open a file, say, test.txt.
Make sure autoindent is enabled (:set ai?<CR>)
Prove that autoindent doesn't happen:
Type a space or two, then hit enter.
When the cursor advances to the next line, it should return to column 1, the far-left column.
Turn on autoindent (:set ai<CR>)
Make sure autoindent is enabled (:set ai?<CR>)
Prove that autoindent happens:
Type a space or two, then hit enter.
When the cursor advances to the next line, it should still be in the same column.
Persist autoindent with :mkvimrc<CR>.
Hope that helps! Here are some other notes:
These instructions might be specific to left-to-right locales.
Here's my vimrc
"The 'autoindent' option is reset when the 'paste' option is set". So try to remove 'paste' from your settings (vim-options).
:set smartindent? showd: nosmartindent
Then activating it with :set smartindent solved problem for me.
I had the same problem, and I have tried many commands, all failed.
At last, I use the following command, and it works:
autocmd VimEnter * set autoindent
It's not a elegant method, however, it does works.
Related
Today I have discovered vim's relativenumber option. I really would like to use it, but sometimes I need to swap between relative numbering and classic one.
I have tried to turn it off with :set relativenumber=off option (which returns me error attached above) and using :set number again but none of those works.
To turn on relative line numbering use this:
:set rnu
To turn it off use this:
:set nornu
By the way, if you had Googled around for your question, you would have found this great post which discusses things in more detail.
To turn on whatever option in vim:
:set <option>
To turn off whatever option in Vim:
:set no<option>
To toggle an option on/off:
:set <option>!
To check if an option is on or off:
:set <option>?
To set an option back to its default:
:set <option>&
I just use this toggle switch in my vimrc file.
function! NumberToggle()
if(&rnu == 1)
set nornu
else
set rnu
endif
endfunc
nnoremap <C-l> :call NumberToggle()<cr>
Relative Numbering is not turned on by default in vim, which means that you are probably turning it on through your ~/.vimrc file or one of your plugins. Look for set relativenumber or set rnu.
To turn it off for the current vim session you would simply run set norelativenumber or set nornu for short. This is a normal vim pattern for turning on and off settings like this. For example, spell check is set spell to activate and set nospell to deactivate.
To find this information and more on relative numbering, I recommend that you look in the vim help docs. For this case, while in vim run :h relativenumber
You actually have hybrid mode on (the line number your cursor is on is labelled as the absolute line number, not 0). In this case, both nu and rnu are on. You'll need to remove both to remove line numbers:
The following should work:
set nornu nonu
I am using vim inside tmux. For some reason, my vim settings are getting constantly reset. --EDIT-- more detail: specifically, tabstop and autoindent are being set to default values, namely tabstop=8 and noautoindent. I don't think its something in my settings that is setting them to that, because when I type :so $MYVIMRC it resets to the proper values from my vimrc. I think vim is somehow "forgetting" my settings?
I haven't been able to figure out exactly what is causing it, but it happens pretty frequently, almost every couple of minutes. It seems to happen most often when I focus on another window, or switch panes in tmux. But it doesn't happen every time, and sometimes it just happens while typing. I have no idea what the problem is but its very frustrating. Also, it seems to happen most with python, slightly less with javascript, and even less frequently with PHP or other languages. Though this could be that I spend most of my time working in python and javascript...
I was having a problem earlier where I was getting gibberish entered into my status bar: Vim inside Tmux: <C-w>l (swapping between vim splits) enters ^]lfilename^] into vim. That fixed that issue, but seems to have caused this new one.
Here are what I think are the relevant parts of my .vimrc, .tmux.con and .bashrc. These are all of my settings, I didn't include keybindings.
.vimrc
set nocompatible
set showmatch
execute pathogen#infect()
syntax enable
filetype plugin indent on
colorscheme desert
set t_Co=256
set shiftwidth=4
set softtabstop=4
set backspace=indent,eol,start " consume expanded tabs if possible
set expandtab
set shiftround
set autoindent
set relativenumber
set showmode
set showcmd
set hidden
set autoread
set ignorecase
set smartcase
set incsearch
set autochdir
set laststatus=2
set statusline=%<%F\ %h%m%r%=%-14.(%l,%c%V%)\ %13.L\ %P
set titlestring=%F
set splitbelow
.bashrc:
export TERM=screen-256color
.tmux.conf
export TERM=screen-256color
Some settings are local to a buffer or window. Indent settings, e.g. 'shiftwidth', 'softtabstop', and 'expandtab', are local to a buffer and not global. This makes sense because different filetypes have different needs. A good example of types that need completely different indent settings would be python and makefile.
Setting up indent setting per filetype are usually done one of the following ways:
Use modelines for each file. Gross! (:h modeline)
Use autocmd's in your ~/.vimrc. e.g. autocmd FileType c,cpp,cs,java setlocal shiftwidt=4 softtabstop=4 expandtab
Put these setting in ~/.vim/after/ftplugin/python.vim. Replace python with any filetype you want to have specific settings for.
Note: You can find a buffer's file type via :set ft?
Personally I like the after directory structure as it is nice and neat and keeps the clutter out of my ~/.vimrc file.
For more help see:
:h local-options
:h 'sw
:h 'rtp
:h after-directory
:h ftplugin-overrule
You said you work in javascript and python and that you notice the difference when changing between them. Are you sure this is changing and not that you get different behavior for javascript and python?
Note the pathogen#infect(). You probably have something like syntastic installed which in turn will have lint tools for javascript and python. Those tools may have file type specific indentation settings. If you have something following PEP8 for python it's probably defaulting to spacing instead of tabs for indentation.
Check .vim/ftplugin and .vim/ftdetect, filetype specific settings can be put there which will override the default behavior specified in your .vimrc.
Couple of times a day I get this, usually when typing in a browser() statement while debugging.
It repeats a whole bunch of brackets, when I only typed a pair. Usually I very quickly type "browser() esc :w enter" (without the quotes) so that I can save and go back to my R environment, and it pauses for a second, and then sticks all these brackets in. It's not really a problem, a quick Undo takes it out, just wondering why it does this. Am I moving too fast for Vim (touch typist).
I'm using Vim 7.3 on Windows 7. Does this on more than one machine. In all cases I'm also mapping caps lock to escape using sharpkeys, I don't know if that might be a factor. Anybody seeing this? Any remedy?
Thanks.
For completeness, here is my vimrc file:
colorscheme clarity
syntax enable
set guifont=Consolas:h10:cANSI
set number
set tabstop=4
set shiftwidth=4
set softtabstop=4
set expandtab
set autoindent
set smarttab
set backspace=indent,eol,start
set nocompatible
set expandtab
filetype plugin on
filetype indent on
set ruler
set cursorline
set noerrorbells
set visualbell
"set guioptions-=m
set guioptions-=T
I am guessing on line 32 you had:
browser
Then you typed:
90a()<esc>
The mistake here being that typed "90", thinking that you were in insert/append mode, realized you weren't went into that mode, typed your parenthesis, then exited. What you actually told vim was "Append this text 90 times", and it gladly did it for you, so you ended up with:
browser()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()
It's ok, it happens to the best of us.
I prefer to use tab than white space(may be a little different from most of others)
But I found, when I hit Enter at the end of line, it will add some white spaces, but not tab. So, I have to delete them and press tab.
I want to know how to set vim as:
use only tab to indent the lines
a tab looks like 4-spaces, but actually is a tab
when hit enter at the end of a line, the new line is started with only tabs
I've googled for this for a while, but not found a good answer. Thank you in advance
UPDATE
The answer #Alok has provided works well in most of cases. But I just found, sometimes, it depends on the file type. For example, if you are editing a haml file, and there is a haml.vim in your vimfiles/indent/, then all the tabs will be converted to space. So if you want it to be tab only, you should modify(or delete) the corresponding indent file.
The settings you are looking for are:
set autoindent
set noexpandtab
set tabstop=4
set shiftwidth=4
As single line:
set autoindent noexpandtab tabstop=4 shiftwidth=4
autoindent can be replaced with smartindent or cindent, depending upon your tastes. Also look at filetype plugin indent on.
http://vim.wikia.com/wiki/Indenting_source_code
Late to the discussion, just for anyone who struggled to force the use of tabs. With the current version of vim (>8) it was required to set:
:set noet ci pi sts=0 sw=4 ts=4
which expands to:
:set noexpandtab
:set copyindent
:set preserveindent
:set softtabstop=0
:set shiftwidth=4
:set tabstop=4
We can also use this setup with only specific file types, taking advantage of setlocal:
autocmd FileType c,cpp,java,python setlocal noet ci pi sts=0 sw=4 ts=4
For further reference, see: https://vim.fandom.com/wiki/Indent_with_tabs,_align_with_spaces
In my case set indentexpr= did the trick.
I have found the config files which set the annoying behavior with fd python.vim /. In /usr/share/vim/vim90/indent/python.vim there is the line setlocal indentexpr=python#GetIndent(v:lnum). The function python#GetIndent is defined in /usr/share/vim/vim90/autoload/python.vim. This function returned a weird value which was not a multiple of shiftwidth and tabstop. Therefore vim inserted a tab followed by some spaces.
Now, that I have set indentexpr to an empty value, I think vim falls back to autoindent. smartindent and cindent are turned off. (https://vimhelp.org/indent.txt.html)
Of course set noexpandtab is required, too. I am not sure if any other settings are relevant here.
EDIT: autoindent is not perfect, it does nothing more than indent the new line as much as the previous line.
set smartindent
set cinwords=if,elif,else,try,except,finally,with,for,while,class,def
does not work as expected and cindent does not seem suitable for python, so using indentexpr is the way to go after all.
Luckily the indentation function can be configured, see help ft-python-indent or look at the code – that was more informative regarding the disable_parentheses_indenting feature. It seems this is the solution for me:
if !exists('g:python_indent')
let g:python_indent = {}
endif
let g:python_indent.disable_parentheses_indenting = 1
(https://vi.stackexchange.com/a/38824/43863)
How do I make vi-Vim never use tabs (converting spaces to tabs, bad!), makes the tab key == 4 spaces, and automatically indent code after curly brace blocks like Emacs does?
Also, how do I save these settings so I never have to input them again?
I've seen other questions related to this, but it always seems to be a little off from what I want.
As has been pointed out in a couple of other answers, the preferred method now is NOT to use smartindent, but instead use the following (in your .vimrc):
filetype plugin indent on
" show existing tab with 4 spaces width
set tabstop=4
" when indenting with '>', use 4 spaces width
set shiftwidth=4
" On pressing tab, insert 4 spaces
set expandtab
In your [.vimrc:][1] file:
set smartindent
set tabstop=4
set shiftwidth=4
set expandtab
The help files take a bit of time to get used to, but the more you read, the better Vim gets:
:help smartindent
Even better, you can embed these settings in your source for portability:
:help auto-setting
To see your current settings:
:set all
As graywh points out in the comments, smartindent has been replaced by cindent which "Works more cleverly", although still mainly for languages with C-like syntax:
:help C-indenting
Related, if you open a file that uses both tabs and spaces, assuming you've got
set expandtab ts=4 sw=4 ai
You can replace all the tabs with spaces in the entire file with
:%retab
The best way to get filetype-specific indentation is to use filetype plugin indent on in your vimrc. Then you can specify things like set sw=4 sts=4 et in .vim/ftplugin/c.vim, for example, without having to make those global for all files being edited and other non-C type syntaxes will get indented correctly, too (even lisps).
To have 4-space tabs in most files, real 8-wide tab char in Makefiles, and automatic indenting in various files including C/C++, put this in your ~/.vimrc file:
" Only do this part when compiled with support for autocommands.
if has("autocmd")
" Use filetype detection and file-based automatic indenting.
filetype plugin indent on
" Use actual tab chars in Makefiles.
autocmd FileType make set tabstop=8 shiftwidth=8 softtabstop=0 noexpandtab
endif
" For everything else, use a tab width of 4 space chars.
set tabstop=4 " The width of a TAB is set to 4.
" Still it is a \t. It is just that
" Vim will interpret it to be having
" a width of 4.
set shiftwidth=4 " Indents will have a width of 4.
set softtabstop=4 " Sets the number of columns for a TAB.
set expandtab " Expand TABs to spaces.
On many Linux systems, like Ubuntu, the .vimrc file doesn't exist by default, so it is recommended that you create it first.
Don't use the .viminfo file that exist in the home directory. It is used for a different purpose.
Step 1: Go to your home directory
cd ~
Step 2: Create the file
vim .vimrc
Step 3: Add the configuration stated above
filetype plugin indent on
set tabstop=4
set shiftwidth=4
set expandtab
Step 3: Save file, by pressing Shift + ZZ.
The recommended way is to use filetype based indentation and only use smartindent and cindent if that doesn't suffice.
Add the following to your .vimrc
set expandtab
set shiftwidth=2
set softtabstop=2
filetype plugin indent on
Hope it helps as being a different answer.
From the VIM wiki:
:set tabstop=4
:set shiftwidth=4
:set expandtab
edit your ~/.vimrc
$ vim ~/.vimrc
add following lines :
set tabstop=4
set shiftwidth=4
set softtabstop=4
set expandtab
The auto-indent is based on the current syntax mode. I know that if you are editing Foo.java, then entering a { and hitting Enter indents the following line.
As for tabs, there are two settings. Within Vim, type a colon and then "set tabstop=4" which will set the tabs to display as four spaces. Hit colon again and type "set expandtab" which will insert spaces for tabs.
You can put these settings in a .vimrc (or _vimrc on Windows) in your home directory, so you only have to type them once.
Firstly, do not use the Tab key in Vim for manual indentation. Vim has a pair of commands in insert mode for manually increasing or decreasing the indentation amount. Those commands are Ctrl-T and Ctrl-D. These commands observe the values of tabstop, shiftwidth and expandtab, and maintain the correct mixture of spaces and tabs (maximum number of tabs followed by any necessary number of spaces).
Secondly, these manual indenting keys don't have to be used very much anyway if you use automatic indentation.
If Ctrl-T instead of Tab bothers you, you can remap it:
:imap <Tab> ^T
You can also remap Shift-Tab to do the Ctrl-D deindent:
:imap <S-Tab> ^D
Here ^T and ^D are literal control characters that can be inserted as Ctrl-VCtrl-T.
With this mapping in place, you can still type literal Tab into the buffer using Ctrl-VTab. Note that if you do this, even if :set expandtab is on, you get an unexpanded tab character.
A similar effect to the <Tab> map is achieved using :set smarttab, which also causes backspace at the front of a line to behave smart.
In smarttab mode, when Tab is used not at the start of a line, it has no special meaning. That's different from my above mapping of Tab to Ctrl-T, because a Ctrl-T used anywhere in a line (in insert mode) will increase that line's indentation.
Other useful mappings may be:
:map <Tab> >
:map <S-Tab> <
Now we can do things like select some lines, and hit Tab to indent them over. Or hit Tab twice on a line (in command mode) to increase its indentation.
If you use the proper indentation management commands, then everything is controlled by the three parameters: shiftwidth, tabstop and expandtab.
The shiftwidth parameter controls your indentation size; if you want four space indents, use :set shiftwidth=4, or the abbreviation :set sw=4.
If only this is done, then indentation will be created using a mixture of spaces and tabs, because noexpandtab is the default. Use :set expandtab. This causes tab characters which you type into the buffer to expand into spaces, and for Vim-managed indentation to use only spaces.
When expandtab is on, and if you manage your indentation through all the proper Vim mechanisms, the value of tabstop becomes irrelevant. It controls how tabs appear if they happen to occur in the file. If you have set tabstop=8 expandtab and then sneak a hard tab into the file using Ctrl-VTab, it will produce an alignment to the next 8-column-based tab position, as usual.
Afterall, you could edit the .vimrc,then add the conf
set tabstop=4
Or exec the command
Simplest one will be n vim file
set tabstop=4