How to prevent Vim indenting wrapped text in parentheses - vim

This has bugged me for a long time, and try as I might I can't find a way round it.
When I'm editing text (specifically latex, but that doesn't matter) files, I want it to auto-wrap at 80 columns. It does this, except if I happen to be in the middle of a parenthetical clause, it indents the text which is very annoying. For example, this works fine
Here is some text... over
two lines.
but this doesn't
Here is some text... (over
two
lines
If anyone can tell me how to turn this off (just for text/latex files) I'd be really grateful. Presumably it has something to do with the fact that this is desired behaviour in C, but I still can't figure out what's wrong.

:set nocindent
The other options do nothing, and the filetype detection doesn't change it.

There are three options you may need to turn off: set noai, set nosi, and setnocin (autoindent, smartindent, and cindent).

This may be related, when pasting from gui into terminal window, vim cannot distinguish paste modes, so to stop any odd things from occuring:
set paste
then paste text
set nopaste
I had similar issues trying to paste xml text, it would just keep indenting. :)
gvim, the gui version of vim, can detect paste modes.

You can have a look at the autoindent option :
autoindent - ai
Copy indent from current line when starting a new line (typing
in Insert mode or when using the "o" or "O" command). If you do not
type anything on the new line except and then type or
, the indent is deleted again. When autoindent is on,
formatting (with the "gq" command or when you reach 'textwidth' in
Insert mode) uses the indentation of the first line. When
'smartindent' or 'cindent' is on the indent is changed in specific
cases. The 'autoindent' option is reset when the 'paste' option is
set. {small difference from Vi: After the indent is deleted when
typing or , the cursor position when moving up or down is
after the deleted indent; Vi puts the cursor somewhere in the deleted
indent}.

From the official Vim documentation
filetype plugin indent on
This switches on three very clever
mechanisms:
Filetype detection. Whenever you start editing a file, Vim will try to
figure out what kind of file this
is. When you edit "main.c", Vim will
see the ".c" extension and
recognize this as a "c" filetype.
When you edit a file that starts with
"#!/bin/sh", Vim will recognize it as
a "sh" filetype. The filetype
detection is used for syntax
highlighting and the other two
items below. See |filetypes|.
Using filetype plugin files Many different filetypes are edited with
different options. For example,
when you edit a "c" file, it's very
useful to set the 'cindent' option to
automatically indent the lines. These
commonly useful option settings are
included with Vim in filetype plugins.
You can also add your own, see
|write-filetype-plugin|.
Using indent files When editing programs, the indent of a line can
often be computed automatically.
Vim comes with these indent rules for
a number of filetypes. See
|:filetype-indent-on| and
'indentexpr'.

:set noai
sets no auto indent tt may be smartindent though. Check out the doc and see if you can find something more
http://www.vim.org/htmldoc/indent.html

Related

How to disable automatic indentation by Slimv for non-Lisp files?

Non-Slimv Behavior
At first, let me show the normal Vim behavior when Slimv is not enabled.
Create a new file: vim foo.c.
Enter insert mode: i
Enter this code: void f()
Press enter
The cursor is now at position 2,1 (2nd row, 1st column).
Slimv Behavior
After installing Slimv, if I perform the steps above, at the end, I find the cursor at position 2,5 (2nd row, 5th column) with four spaces inserted as indentation before the 5th column automatically.
How can I disable this Slimv behavior for non-Lisp files (such as .c files)?
The issue is caused by this line in paredit.vim:
filetype indent on
I added the option g:paredit_disable_ftindent to paredit.vim to disable the loading of indent files, please add this line to your .vimrc when using paredit.vim (or slimv that also contains paredit.vim):
let g:paredit_disable_ftindent=1
First, add to your vimrc file the following two lines:
filetype indent off
filetype plugin indent off
Then you have to hope that it’ll work! But there is a large probability that it won’t…
If the solution doesn’t work, you may have to make quite complicated actions to solve the trouble.
The problem is that many of your vim options are constantly changed by a lot of triggers and auto commands. The only way I found is to mark the important options (the options I don’t want to be changed) with a special symbol, and then to restore them forcibly after any possible impact. So, I did the following in my vimrc:
augroup MyVimrc
autocmd!
augroup END
"The options I want to keep unchanged, I mark with the “❗” symbol.
"The usual exclamation sign “!” will work as well.
"Of course, you’re free using any other symbol you like,
"but don’t forget the lambda-function in the hack below.
"These are the options that may influence on the indent.
set formatoptions=j "❗
set autoindent "❗
set nosmartindent "❗
set indentexpr= "❗
set copyindent "❗
set preserveindent "❗
"And I marked with the same way a number of other
"important (for me) options… (not shown here)
"At the bottom of the vimrc file, I wrote this dirty hack.
function! s:keep_options()
for line in filter(readfile($MYVIMRC), 'v:val =~ ''\v\".*(!|❗)\s*$''')
exe line
endfor
endfunction
command! KeepOptions call <SID>keep_options()
"Note, the “OptionSet” trigger doesn’t work always, so that I preferred “BufEnter”.
autocmd MyVimrc BufEnter * KeepOptions
And all the troubles with unpredictable changes in my settings, at last, have gone.

Disable autoindent in vim

I use vim since almost 20 years and recently someone told me about the usage of = key for indenting a block of code. Sometimes I try a new vim key but stop using it because it isn't doing exactly what I want or I just don't need the feature. But in this case I find its a nice feature.
So I downloaded vim script 1120 (PHP-correct-Indenting) and installed it. My .vimrc contains:
filetype indent on
set smartindent
Now I can use = to indent a visually marked code block.
But I do NOT want vim to automatically indent code while I am typing. This is just irritating me as I am usually doing indentation myself and I am very much used to it ...
So how can I stop vim from automatically indenting my code while typing but can still continue to use = for indenting a visually marked block of text.
PS: Use hjkl for moving around in vim. It will make you about 1.5 times faster :)
My complete vimrc:
syntax on
set tabstop=3
set shiftwidth=3
execute pathogen#infect()
filetype indent on
set smartindent
Commenting the last two lines stops autoindenting but also using "=" does not use the mentioned vim script anymore
The plugin sets 'indentexpr', which controls both explicit reindenting via = as well as indenting-as-you-type. Fortunately, you can control the triggering of the latter via the 'indentkeys' option, so clearing that should work.
Put the following into ~/.vim/after/indent/php.vim; this way, it'll apply after the plugin:
setlocal indentkeys=
The effect I want can get achieved using:
:set paste
This is exactly(?) the mode I wanted to switch into. No autoindenting at all. But the "=" key works to indent a marked block of text.
Maybe I will add "set paste" to my .vimrc :)
Thanks for your support anyways.

How do I avoid cumulative filetype commands in vim?

Let's say I load up a python file in vim. A quick check of :scriptnames shows that my ~/.vim/ftplugin/python/python.vim file loads as expected. One of the commands in this file highlights all characters that are past the 80th column. Now lets say I open a C++ file in another buffer (therefore running ~/.vim/ftplugin/cpp/cpp.vim). Although the new commands are executed, the settings in python.vim still apply; therefore characters are highlighted past the 80th column in my C++ file.
Is there anyway to make filetype commands not cumulative like this? I have filetype plugin indent on in my .vimrc.
The problem is that both 'colorcolumn' and :match (you didn't specify whether you use the new setting or the older highlight approach) are local to the window, but ftplugins should only set buffer-local settings.
Why are these settings window-local? That allows you to have the same buffer displayed in two windows, one with, and one without the highlighting.
What can you do to prevent this?
a) Don't set this in the ftplugin, and instead use mappings to toggle the colorcolumn on/off.
b) Put :setlocal nocolorcolumn into all ftplugin scripts (e.g. in ~/.vim/after/ftplugin/*.vim) for all filetypes that you're using. This will only work unless you switch between different filetypes in the same window.
c) The correct (but most complex) way to solve this is through a couple of :autocmds on BufWinEnter, BufWinLeave, and WinLeave events.

Emacs like strict auto-indent in vim

Emacs, while editing a C file, forces me to follow a particular indentation. When I press tab on an incorrectly indented line, it corrects the indentation. I want this behaviour from my vim editor. Till now I have done the following :
set cindent
set smartindent
set autoindent
set expandtab
set tabstop=2
set shiftwidth=2
set cinkeys=0{,0},:,0#,!,!^F
in my .vimrc file. However, I am not achieving the same emacs-like forced effect as I want.
Is it possible at all in vim ?
'smartindent' is obsolete. There's really no reason you need to have that in your vimrc.
'cindent' overrules 'smartindent', so setting both in your vimrc is pointless. Setting 'cindent' in your vimrc isn't very useful either, since it only really works well on C-like languages.
filetype indent on will enable the filetype-specific indentation plugins (c.f., the indent directory under $VIMRUNTIME). That paired with 'autoindent' at least gives you basic automatic indentation support regardless of what filetype you're editing.
If you want to add indent settings for a specific filetype, you can create your own indent script in ~/.vim/indent/<filetype>.vim, or ~/.vim/after/indent/<filetype>.vim if you're augmenting an existing system-wide indent script.
As the settings you posted show, pressing Ctrlf in insert mode will do what Emacs does when you press Tab. This is described at :help indentkeys-format. 'cinkeys' is used when 'cindent' is enabled and 'indentexpr' is empty. 'indentkeys' is for the reverse. It's just a slight change to modify the setting so Tab can be used in place of/in addition to Ctrlf.
On a final note, I'd recommend learning your way around Vim's help. It's very thorough and easy to use once you figure things out. :help usr_toc is a good place to start for user-level documentation. :help describes some of the basic about navigating the help, how to search for topics, etc.
The == command is what you want, if I understand you correctly. It reindents the current line according to syntax rules.
As for binding it to tab, that's certainly possible, but I have not done that and am not completely sure as to how you can catch the right moment when it should actually insert the tab and when it should reindent.
Personally, I find it less confusing to just press ==. = accepts a range even, so you can go into visual mode, make a selection and tap = and the region will be reindented.

Disable all auto indentation in vim

In TeX vim usually screws up my indentation. Mainly when I'm in a displayed equation which I think should look like this:
\[
x=\frac{y}{z}
\]
where the whitespace infront of the x is one tab.
When I start type the equation I type the \[ and \] marks first and then go back between them, typing the tab and then the rest of the equation.
Vim doesn't do anything wrong until I have to use something that incorporates curly braces (\frac{} for example). When I type the closing } vim automatically shifts the indentation for the whole line to the left, which undoes my typed tab.
This is very anoying, how do I disable it?
my .vimrc contains:
"indentation
set smartindent
set autoindent
set tabstop=5
set shiftwidth=5
filetype indent on
I just spent a few hours working through indentation pains with javascript, and the conclusion I came to is don't remove filetype indent on from your vimrc!
This setting provides the best smart indentation for multiple file types. If you're getting bad results with this, there's likely a configuration issue at hand.
File Specific Indent Settings
So if you're like me, you probably had filetype indent on in your vimrc and had no idea what it was doing.
All this setting does is tell vim to look for files with filetype-specific indent rules. There are a few places it looks, but there are probably only two that you'd be interested in.
$VIMRUNTIME/indent/
~/.vimrc/after/indent/
The first place holds the default indent rules that come with vim. If you were to set filetype indent on on a fresh vim installation, this is where all the smart indenting would come from. For example, when you open a file called index.html in would get the rules from $VIMRUNTIME/indent/html.vim.
In my experience, these default rules are pretty darn good, but they can get messed up by other settings.
The second place (the after directory) allows you to add settings that will supercede those in the first place. This is nice because you don't have to edit the default files in order to customize them.
Flavors of Indentation
There are a few different indentation options as you've seen, and they don't all play nice together. From the Vim wiki:
autoindent
'autoindent' does nothing more than copy the indentation from the previous line, when starting a new line. It can be useful for structured text files, or when you want to control most of the indentation manually, without Vim interfering. 'autoindent' does not interfere with other indentation settings, and some file type based indentation scripts even enable it automatically.
I use filetype indent on and set autoindent in my vimrc, since they work well together. I don't have the others set.
smartindent & cindent
'smartindent' automatically inserts one extra level of indentation in some cases, and works for C-like files. 'cindent' is more customizable, but also more strict when it comes to syntax.
'smartindent' and 'cindent' might interfere with file type based indentation, and should never be used in conjunction with it.
When it comes to C and C++, file type based indentations automatically sets 'cindent', and for that reason, there is no need to set 'cindent' manually for such files. In these cases, the 'cinwords', 'cinkeys' and 'cinoptions' options still apply.
Generally, 'smartindent' or 'cindent' should only be set manually if you're not satisfied with how file type based indentation works.
indentexpr
Runs filetype indent scripts found in (vimfolder)\indent\\(indentscripts). It is mentioned in the vim documentation for filetype, alongside the others just mentioned (also, it was the cause of the problem I was having):
Reset 'autoindent', 'cindent', 'smartindent' and/or 'indentexpr' to disable indenting in an opened file.
Troubleshooting
There's a chance that some rogue plugin is changing your indent settings and that's why you're getting poor results. Luckily verbose will tell you which file was the last to change the option in question.
:verbose set autoindent?
:verbose set cindent?
:verbose set smartindent?
:verbose set indentexpr?
You may get a result such as
indentexpr=SomeMessedUpValue
Last set from ~/.vim/bundle/some_plugin/indent/plaintex.vim
If that happens, you can move that file, close and open vim, and see if it fixes your problem.
Turning Off Indent Settings for TeX
Maybe the defaults just aren't doing it for you, and you want to disable the indent settings for TeX, but leave all other file types alone. You can easily do so by setting these values to their defaults in a file in the after directory.
I don't know much about Tex or LaTex, but when I created a file with the .tex extension and ran :filetype it had the filetype as plaintex. Assuming that this is correct, you'd want to create a file, ~/.vim/after/indent/plaintex.vim. In that file:
set autoindent&
set cindent&
set smartindent&
set indentexpr&
This will set all these values to their defaults whenever you open a .tex file.
There seem to be a little mix of terms in your question. In vim the term autoindent points to a special kind of indentation that simply follows the indent level of the previous line (which is quite handy sometimes). To remove it set noautoindent by hand, or write it in your _vimrc.
There are two other automatic kinds of indentation, cindent and smartindent. Similarly, if you wish to disable them go with set nocindent and set nosmartindent
If you look in help (help autoindent, ...) they are all quite nicely explained. Which one you prefer (or don't) is mostly determined by your programming style and habits. So, try them out and see which you like most.
Unfortunatelly, I don't use LaTeX that much anymore, so I'm not familiar with its internal filetype indentation rules.
For anyone else having a similar problem, a solution that worked for me was:
Use :verbose set indentexpr? to find what file was causing the de-indentation
Find where indentexpr is changed (for me it was setlocal indentexpr=GetTeXIndent())
Change that line to setlocal indentexpr& to turn indentexpr off
This removed all de-indenting from brackets, parentheses, and braces.
Remove the lines set autoindent and set smartindent to remove all vim autoindentation.
The following command finally stopped VIM from pretending it knows how to indent files for me and only do what I explicitly tell it:
:setl noai nocin nosi inde=
Courtesy https://vim.fandom.com/wiki/How_to_stop_auto_indenting
If you are using the vim-latex plugin, set this option:
let g:tex_indent_brace=0
For other plugins, if you don't want to turn off indentexpr as in the above answers, you can find where indentkeys is set and comment out those lines. This should stop triggering re-indent when you type a closing brace.
:verbose set indentkeys?

Resources