vim indention not preserving spacing - vim

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.

Related

The extra space problem for Chinese with `gq` command in VIM

The following is a Chinese paragraph, just forget the meaning of Chinese.
The following is the chinese paragraph after call gq command. Notice that there appear some extra spaces, for example 灵 活性 and 实际上 某种. In Chinese, we don't need such extra spaces.
The following is some setting in my .vimrc
set ts=4
set shiftwidth=4
set softtabstop=4
set expandtab
set autoindent
set smartindent
set ruler
set showcmd
set number
set nrformats=
set textwidth=80
set nocompatible
set formatoptions+=m
So what kind of setting can solve this extra spaces problem? Thanks very much.
From :help fo-table:
B: When joining lines, don't insert a space between two multi-byte characters. Overruled by the 'M' flag.
Thus,
set formatoptions+=mB

When I press enter and create a new line in vim while editing CSS, it gives me two tabs instead of just one

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.

.vimrc textwidth not applying

The following is my .vimrc file in it's entirety:
For some reason, the set wrap and set textwidth=73 (at bottom of file) are not working (I want to have files wrap at 73 columns). I can still go into a file and type past 73 columns.
Is there a conflicting command here that needs to be fixed? Or am I doing something wrong?
set nocompatible
set smartindent
set cursorline
filetype plugin indent on
set background=dark
syntax enable
set grepprg=grep\ -nH\ $*
syntax on
set mouse=a
set history=1000
set showmode
if has('cmdline_info')
set ruler " show the ruler
set rulerformat=%30(%=\:b%n%y%m%r%w\ %l,%c%V\ %P%) " a ruler on steroids
set showcmd " show partial commands in status line and
endif
set backspace=indent,eol,start " backspace for dummys
set linespace=0 " No extra spaces between rows
set nu " Line numbers on
set showmatch " show matching brackets/parenthesis
set incsearch " find as you type search
set hlsearch " highlight search terms
set winminheight=0 " windows can be 0 line high
set ignorecase " case insensitive search
set smartcase " case sensitive when uc present
set wildmenu " show list instead of just completing
set wildmode=list:longest,full
set scrolljump=5 " lines to scroll when cursor leaves screen
set scrolloff=3 " minimum lines to keep above and below cursor
set gdefault " the /g flag on :s substitutions by default
set autoindent " indent at the same level of the previous line
set shiftwidth=4 " use indents of 4 spaces
set expandtab " tabs are spaces, not tabs
set tabstop=4 " an indentation every four columns
set softtabstop=4 " let backspace delete indent
set matchpairs+=<:>
set comments=sl:/*,mb:*,elx:*/
autocmd FileType c,cpp,java,php,js,python,twig,xml,yml autocmd BufWritePre <buffer> :call setline(1,map(getline(1,"$"),'substitute(v:val,"\\s\\+$","","")'))
set foldmethod=syntax "fold based on indent
set foldnestmax=10 "deepest fold is 10 levels
set foldlevel=0 "this is just what i use
set wrap
set textwidth=73
set formatoptions+=t
EDIT: I want VIM to auto wrap at 73 lines (real time). Is that possible? I have tried adding set formatoptions+=t to wrap text but it still isn't effective.
Options wrap and textwidth regard two completely different kinds of wrapping.
The textwidth sets a line width limit, after which every new word (that means separated with spaces) will be placed in a new line.
Attached .vimrc probably works correctly, and the textwitdh does precisely this.
wrap doesn't affect the edited file contents at all, just causing the line in file be displayed in several lines if it is longer than display width. If found no pleasing way to configure this to wrap at the fixed column width and I, personally, don't see such a need.
However there are two aspects of soft-wrapping that can be changed if you find soft wrapping at the end of the screen annoying.
Change the width of the Vim window, using set columns=73 (it is quite disturbing, because it changes the width of whole window)
Soft-wrap lines not on the boundary of screen, but on the last word boundary, by setting linebreak option, see help linebreak for details.
Perhaps not the best solution, but try to assign textwidth and formatoptions inside an autocommand, and then format all lines of your file.
autocmd BufRead * set fo+=t tw=73|normal gggqG
This worked in my test. You can be more specific and substitute * with *.txt or similar.
Put this into .vimrc, that worked for me:
autocmd FileType * set textwidth=58

Vim Indentation Using ">"

I'm using vim under Mac OS 10.7 Terminal.
My .vimrc already specify the tabstop to be 4. However, if I use shift to select multiple lines and then using ">" to indent, it will give me a indention of 8 spaces instead of 4. How can I correct that to be 4?
Part of my .vimrc:
set cindent
set autoindent
set tabstop=4
The shiftwidth variable controls the indentation:
set shiftwidth=4
set shiftwidth=4
They're different things: tabstop says how many spaces wide to use when displaying a tab character, shiftwidth is for indentation.

changing tabstop from 4 to 2 after using expandtab (with minimum manual effort)

For some time I have used tabstop=4 in my source files. Now that I write lot of javascript that has deep indentations, a tabstop of 4 seems wasteful, so I want to convert it to 2. The problem is I use "set expandtab" too. So merely setting tabstop=2, won't help.
Any suggestions on how I can convert all my files quickly to tabstop 2?
My current relevant .vimrc settings are
set tabstop=4
set shiftwidth=4
set expandtab
You can do
" convert spaces to tabs first
set noexpandtab
set tabstop=4
set shiftwidth=4
retab!
" now you have tabs instead of spaces, so insert spaces according to
" your new preference
set tabstop=2
set shiftwidth=2
set expandtab
retab!
in a vim window, and it will retab according to your preferences. You can probably create a mapping to make this easier, or record a macro. There might be another way, but the above should work. (" is comment)
You can change your settings, select all, and hit the = key to indent it:
ggVG=
I also recommend using ftplugin to specify settings per file type.

Resources