Below is example text I put in vim, only spaces used no tabs.
dog;
dogcat
dogcatmoose
box
box car
woop
woopadoop
Lets say I highlight all code, and use indention > what happens is the following:
dog;
dogcat
dogcatmoose
box
box car
woop
woopadoop
Original spacing is not preserved. I dont want that, I want spacing to be preserved so it looks like this:
dog;
dogcat
dogcatmoose
box
box car
woop
woopadoop
I tested with tabs instead of spaces. When using tabs, indention preserves the tabs.
Here is my vimrc. IS there some value in here causing this problem? Is there a value I can add to fix the problem?
set number
set nowrap
set linebreak
set tabstop=4
set softtabstop=4
set shiftwidth=4
set shiftround
set expandtab
set title
set mouse=a
set hlsearch
set smarttab
set autoindent
set smartindent
set background=dark
filetype indent on
set filetype=html
set smartindent
set nocompatible
filetype off
set nobackup noswapfile
set clipboard=unnamed
let &colorcolumn=join(range(81,999),",")
highlight ColorColumn ctermbg=235 guibg=#2c2d27
"Stop vm from indenting on comment out sections starting with "#"
set indentkeys-=0#
My suggestion is to get rid of set shiftround.
It seems like set shiftround coupled with set shiftwidth=4 is causing the behaviour. shiftround tries to indent the text to multiples of 4 (which is set by shiftwidth), while your text is indented with 2 spaces. So when you do >, shiftround makes all the lines have the same 4-space indent. You can experiment this with the same text but with 4-space indentation; your setting will preserve the indentation.
For some reason it seems the default for vim with Go files is to highlight trailing whitespace in red. In a way this is nice, but mostly I find it annoying because every time I type a space it starts as a red highlight. Is there a way to stop this behavior? I've only experienced this with Go files. Below is my vimrc, but I don't think I put anything there that would affect it.
set nocompatible
syntax on
set autoindent
set tabstop=4 softtabstop=0
autocmd FileType go set tabstop=8 softtabstop=0
set formatoptions=tcroql
set relativenumber
set incsearch
set hlsearch
set smartindent
filetype indent on
From go.vim Vim syntax file:
" There are some options for customizing the highlighting; the recommended
" settings are the default values, but you can write:
" let OPTION_NAME = 0
" in your ~/.vimrc file to disable particular options.
Put in your .vimrc
let g:go_highlight_trailing_whitespace_error=0
There are these other options:
" - g:go_highlight_array_whitespace_error
" Highlights white space after "[]".
" - g:go_highlight_chan_whitespace_error
" Highlights white space around the communications operator that don't
" follow the standard style.
" - g:go_highlight_extra_types
" Highlights commonly used library types (io.Reader, etc.).
" - g:go_highlight_space_tab_error
" Highlights instances of tabs following spaces.
If you still like the highlighting of trailing whitespaces but not during the typing, you can try
au InsertEnter *.go match goSpaceError /\s\+\%#\#<!$/
au InsertLeave *.go match goSpaceError /\s\+$/
Read more in Highlight unwanted spaces from wikia.
In VIM I've, all my tabs are 2 spaces. But when I push to github, they get converted to 4 spaces. Can anyone help me figure out how to prevent them from getting converted to 4 spaces?
My vimrc file:
call pathogen#infect()
call pathogen#runtime_append_all_bundles()
" search
set hlsearch " highlight the search
set incsearch " incremental search
set ignorecase " search ignoring case
set showmatch " show matching bracket
" colors
set background=dark
let g:solarized_termcolors=8 " proper solarized coloring
colorscheme peachpuff
" syntax
syntax on
filetype on " Enable filetype detection
filetype plugin on " Enable filetype-specific plugins
filetype indent on " Enable filetype-specific indenting
set ruler " show the line number on the bar
set more " use more prompt
set autoread " watch for file changes
set number " line numbers
set hidden
set noautowrite " don't write old file out when switching files
set lazyredraw " don't redraw when don't have to
set showmode
set showcmd
set nocompatible " vim, not vi
set autoindent smartindent " auto/smart indent
set smarttab " tab and backspace are smart
set tabstop=2 " 6 spaces
set shiftwidth=2
set scrolloff=5 " keep at least 5 lines above/below
set sidescrolloff=5 " keep at least 5 lines left/right
set history=200
set backspace=indent,eol,start
set linebreak
set cmdheight=2 " command line two lines high
set undolevels=1000 " 1000 undos
set updatecount=100 " switch every 100 chars
set complete=.,w,b,u,U,t,i,d " do lots of scanning on tab completion
set ttyfast " we have a fast terminal
set noerrorbells " No error bells please
set shell=bash
set fileformats=unix
set ff=unix
set wildmode=longest:full
set wildmenu " menu has tab completion
set laststatus=2
set diffopt=filler,iwhite " ignore all whitespace and sync
" scss formatting
autocmd BufRead,BufNewFile *.scss ""set ft=scss.css
" jade formatting
autocmd BufRead,BufNewFile *.jade setlocal ft=jade noexpandtab
autocmd FileType jade :setlocal sw=2 ts=2 sts=2
autocmd VimEnter * NERDTree
autocmd VimEnter * wincmd p
autocmd Filetype javascript setlocal ts=2 sw=2 sts=0 noexpandtab
The standard representation of a tab character is 8 spaces. In reality, the tab is a control character and as such doesn't have an associated glyphs or a set width. This allows most programs to offer a way to modify how it is displayed but no program ever change its width because it doesn't have one to start with: a tab is still a tab, no matter the value of tabstop.
Your Vim settings only alter how tabs are displayed in Vim: they have no bearings on how they are displayed outside of Vim.
You have two solutions:
set GitHub and Vim to use the same tab width
use spaces
Since we are at it…
set background=dark
let g:solarized_termcolors=8 " proper solarized coloring
colorscheme peachpuff
You can delete the solarized line if you don't use it and set background is useless because your colorscheme takes care of that.
set noautowrite
is also useless because autowrite is off by default.
And
call pathogen#runtime_append_all_bundles()
serves no purpose whatsoever.
I would recommend reading this on how to change tabsize for Github
There appears to be a browser extension called Stylish that changes tabs in Github by downloading the style "Better sized tabs in code"
Download the browser extension and use the style and change the tab spacing to two space tabs.
Hope this helped!
I debated with myself whether to post it on SuperUser but I did it here.
When I have this in CSS, editing it in VIM:
#container {
then I press Return at the bracket to give me a new line, it sends the cursor two tabs to the left on the next line:
#container {
|
Instead of like this, how I want it.
#container {
|
how could I edit the .vimrc file to give me only one new tab on the next line instead of two?
My .vimrc file.
set ts=4
imap <C-Return> <CR><CR><C-o>k<Tab>
set cindent
set nocompatible
filetype indent plugin on
syntax on
set hidden
set wildmenu
set showcmd
set hlsearch
set ignorecase
set smartcase
set backspace=indent,eol,start
set autoindent
set nostartofline
set ruler
set laststatus=2
set confirm
set visualbell
set t_vb=
set mouse=a
set cmdheight=2
set number
set notimeout ttimeout ttimeoutlen=200
set pastetoggle=<F11>
set shiftwidth=2
set tabstop=1
map Y y$
nnoremap <C-L> :nohl<CR><C-L>
Thanks.
I believe your issue is that your shiftwidth is twice your tabstop. You are also setting tabstop twice, once at the top to 4 and again at the bottom to 1.
Try setting shiftwidth to the same at tabstop, there really aren't many cases that you'd want these two to be different.
My current setting assumes 8 spaces; how could I redefine it?
It depends on what you mean. Do you mean:
you want tab characters in your file to appear 4 character cells wide?
you want the tab key to generate an indent consisting of 4 space characters
Depending on which behavior you need, one of the following sets of settings should work:
If you want tab characters in your file to appear 4 character cells wide:
set tabstop=4
If your code requires use of actual tab characters these settings prevent unintentional insertion of spaces (these are the defaults, but you may want to set them defensively):
set softtabstop=0 noexpandtab
If you also want to use tabs for indentation, you should also set shiftwidth to be the same as tabstop:
set shiftwidth=4
To make any of these settings permanent add them to your vimrc.
If you want pressing the tab key to indent with 4 space characters:
First, tell vim to use 4-space indents, and to intelligently use the tab key for indentation instead of for inserting tab characters (when at the beginning of a line):
set shiftwidth=4 smarttab
If you'd also like vim to only use space caharacters, never tab characters:
set expandtab
Finally, I also recommend setting tab stops to be different from the indentation width, in order to reduce the chance of tab characters masquerading as proper indents:
set tabstop=8 softtabstop=0
To make any of these settings permanent add them to your vimrc.
More Details
In case you need to make adjustments, or would simply like to understand what these options all mean, here's a breakdown of what each option means:
tabstop
The width of a hard tabstop measured in "spaces" -- effectively the (maximum) width of an actual tab character.
shiftwidth
The size of an "indent". It's also measured in spaces, so if your code base indents with tab characters then you want shiftwidth to equal the number of tab characters times tabstop. This is also used by things like the =, > and < commands.
softtabstop
Setting this to a non-zero value other than tabstop will make the tab key (in insert mode)
insert a combination of spaces (and possibly tabs) to simulate tab stops at this width.
expandtab
Enabling this will make the tab key (in insert mode) insert spaces instead of
tab characters. This also affects the behavior of the retab command.
smarttab
Enabling this will make the tab key (in insert mode) insert spaces or tabs to
go to the next indent
of the next tabstop when the cursor is at the beginning of a line (i.e. the
only preceding characters are whitespace).
For further details on any of these see :help 'optionname' in vim (e.g. :help 'tabstop')
To define this on a permanent basis for the current user, create (or edit) the .vimrc file:
$ vim ~/.vimrc
Then, paste the configuration below into the file. Once vim is restarted, the tab settings will apply.
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
or shorthand for vim modeline:
vim :set ts=4 sw=4 sts=4 et :
There are few settings which define whether to use spaces or tabs.
So here are handy functions which can be defined in your ~/.vimrc file:
function! UseTabs()
set tabstop=4 " Size of a hard tabstop (ts).
set shiftwidth=4 " Size of an indentation (sw).
set noexpandtab " Always uses tabs instead of space characters (noet).
set autoindent " Copy indent from current line when starting a new line (ai).
endfunction
function! UseSpaces()
set tabstop=2 " Size of a hard tabstop (ts).
set shiftwidth=2 " Size of an indentation (sw).
set expandtab " Always uses spaces instead of tab characters (et).
set softtabstop=0 " Number of spaces a <Tab> counts for. When 0, featuer is off (sts).
set autoindent " Copy indent from current line when starting a new line.
set smarttab " Inserts blanks on a <Tab> key (as per sw, ts and sts).
endfunction
Usage:
:call UseTabs()
:call UseSpaces()
To use it per file extensions, the following syntax can be used (added to .vimrc):
au! BufWrite,FileWritePre *.module,*.install call UseSpaces()
See also: Converting tabs to spaces.
Here is another snippet from Wikia which can be used to toggle between tabs and spaces:
" virtual tabstops using spaces
set shiftwidth=4
set softtabstop=4
set expandtab
" allow toggling between local and default mode
function TabToggle()
if &expandtab
set shiftwidth=8
set softtabstop=0
set noexpandtab
else
set shiftwidth=4
set softtabstop=4
set expandtab
endif
endfunction
nmap <F9> mz:execute TabToggle()<CR>'z
It enables using 4 spaces for every tab and a mapping to F9 to toggle the settings.
I copied and pasted this into my .vimrc file:
" size of a hard tabstop
set tabstop=4
" always uses spaces instead of tab characters
set expandtab
" size of an "indent"
set shiftwidth=4
The first 2 settings mean that when I press Tab I get 4 spaces.
The third setting means that when I do V> (i.e. visual and indent) I also get 4 spaces.
Not as comprehensive as the accepted answer but it might help people who just want something to copy and paste.
One more thing, use
:retab
to convert existing tab to spaces
http://vim.wikia.com/wiki/Converting_tabs_to_spaces
Put your desired settings in the ~/.vimrc file -- See below for some guidelines and best practices.
There are four main ways to use tabs in Vim:
Always keep 'tabstop' at 8, set 'softtabstop' and 'shiftwidth' to 4 (or 3 or whatever you prefer) and use 'noexpandtab'. Then Vim will use a mix of tabs and spaces, but typing and will behave like a tab appears every 4 (or 3) characters.
Note: Setting 'tabstop' to any other value than 8 can make your file appear wrong in many places (e.g., when printing it).
Set 'tabstop' and 'shiftwidth' to whatever you prefer and use 'expandtab'. This way you will always insert spaces. The formatting will never be messed up when 'tabstop' is changed.
Set 'tabstop' and 'shiftwidth' to whatever you prefer and use a |modeline| to set these values when editing the file again. Only works when using Vim to edit the file.
Always set 'tabstop' and 'shiftwidth' to the same value, and 'noexpandtab'. This should then work (for initial indents only) for any tabstop setting that people use. It might be nice to have tabs after the first non-blank inserted as spaces if you do this though. Otherwise aligned comments will be wrong when 'tabstop' ischanged.
Source:
vimdoc.sourceforge.net/htmldoc/options.html#'tabstop'
:help tabstop
Add line
set ts=4
in
~/.vimrc file for per user
or
/etc/vimrc file for system wide
:set sw=4
See Mastering the VI editor
My basic ~/.vimrc with comment:
set number " show line number
set tabstop=2 " set display width of tab; 1 tab = x space with
set expandtab " transform tab to x space (x is tabstop)
set autoindent " auto indent; new line with number of space at the beginning same as previous
set shiftwidth=2 " number of space append to lines when type >>
Permanent for all users (when you alone on server):
# echo "set tabstop=4" >> /etc/vim/vimrc
Appends the setting in the config file.
Normally on new server apt-get purge nano mc and all other to save your time. Otherwise, you will redefine editor in git, crontab etc.
Make sure vartabstop is unset
set vartabstop=
Set tabstop to 4
set tabstop=4
For permanent change, create the file ~/.vim/plugin/tab_expander.vim with the content
set tabstop=4 softtabstop=4 expandtab shiftwidth=4 smarttab
To prevent touching the ~/.vimrc, thus keeping other default settings untouched.