The extra space problem for Chinese with `gq` command in VIM - 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

Related

vim indention not preserving spacing

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.

Vim is NOT indenting by spaces

I have a file that is set to indent by four spaces. I have a block of code that looks something like:
content
content
end
You can see how everything above is 4 spaces in. Vim likes to do something like:
content
content
new line #(2 spaces, not 4)
end
I set up my vimrc file to have the following lines, in addition to the default settings:
set smartindent
set tabstop=4
set shiftwidth=4
set expandtab
What's going on? Even if I do 1>> it moves too far over and 1<< moves to far left.
add set cindent to your vimrc and see effect.
I think smartindent is depreciated. Try cindent for C-style languages. You could also try filetype indent on.

How to set up vim allowing to use tabs and 4spaces

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

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.

How to use only tab (not space) in vim

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)

Resources