When writing code in vim, I've noticed that it sometimes messes up the indentation of the current line after pressing return. (In the gif, I go into insert mode, with the cursor at the end of the date function. When I press return, the whole "echo date('Y');" part looses its indentation when it should not).
I have a ton of directives in my vimrc to try and stop all kinds of autoindentation, but much to my irritation, the problem persists.
filetype plugin indent off
filetype plugin off
set noautoindent
set nosmartindent
set nocindent
set indentexpr=''
let b:did_indent = 1
set ft?
filetype indent off
I wish there was a way to have vim indent files much like your "more standard" text editors, whereby they don't try to be smart, but just maintain the current indentation of the current line in the new line. Like this:
Is this possible?
set smartindent is only needed on my VIM to get the effect you shown by gedit.
So it seems that this line was causing the first issue displayed in the vim. Its part of the Vundle plugin manager inclusion code:
filetype plugin indent on
Ensuring that my indent rules were after this fixed the problem completely.
Related
I am having some weird behavior when it comes to VIM.
The behavior that I'm expecting:
When editing a file, if one is in insert mode and doesn't make any file modifications when you exit insert mode the buffer should be considered "saved".
The behavior that I'm actually getting:
When editing a file and when I exit insert mode while making NO MODIFICATIONS, the buffer says that the file has been edited. Even though
I tested if this behavior was normal in vim by opening a clean version of vim, and it seems that the behavior I'm expecting is the default.
This a snippet of my config, my full configuration is here: (https://github.com/TheBabu/Config/blob/master/.vimrc)
I'm assuming whatever is causing this behavior is here otherwise it might be a plugin
"Setup
set shell=bash
set nowrap
set number
set nocompatible
set noshowmode
set directory^=$HOME/.vim/.swapfiles
set undofile
set mouse=a
syntax on
colorscheme neodark
hi Normal ctermbg=none
sign define transparent_sign
augroup SignColFixAu
au!
au BufReadPost *.c,*.cc,*.h,*.cpp,*.hh,*.hpp,*.py,*.js,*.php,*.rs exe "sign place 1111 name=transparent_sign line=1 file=".#%
augroup end
inoremap <cr> <space><bs><cr>
inoremap <esc> ~<bs><esc>
inoremap <expr> <up> pumvisible() ? "\<c-p>" : "~\<bs>\<up>"
inoremap <expr> <down> pumvisible() ? "\<c-p>" : "~\<bs>\<down>"
inoremap <Esc>x <Esc>x
"Tabs
set listchars=tab:➡\
set list
set autoindent
set noexpandtab
set tabstop=4
set shiftwidth=4
..SNIP...
"Plugins
Plugin 'VundleVim/Vundle.vim'
Plugin 'scrooloose/nerdtree'
Plugin 'Valloric/YouCompleteMe'
Plugin 'vim-airline/vim-airline'
Plugin 'vim-airline/vim-airline-themes'
Plugin 'easymotion/vim-easymotion'
Plugin 'w0rp/ale'
Plugin 'godlygeek/tabular'
Plugin 'LukeLike/auto-pairs'
Plugin 'ananagame/vimsence'
Plugin 'preservim/nerdcommenter'
call vundle#end()
filetype plugin indent on
...SNIP...
Thank you for any help!
The problem is caused by your <Esc> mapping:
inoremap <esc> ~<bs><esc>
This mapping is inserting a ~ character then backspacing over it, which in practice doesn't change the contents, but still marks the buffer as modified. (Vim doesn't really track the exact contents of the buffer, but tracks whether any change was introduced, and both inserting ~ and backspacing over a character count as such.)
You mentioned in the comments you use this mapping (together with the <CR> mapping) to change how Vim behaves with indentation. The usual behavior of Vim is to remove auto-indentation when you move to a new line (with <CR> on an otherwise blank line) or leave Insert mode (with <Esc>.)
There doesn't seem to be a way to change this behavior in Vim. But the default Vim behavior is actually very useful, as it prevents ending up with lines with trailing spaces all over the place. (Many use Vim autocmd's to trim trailing whitespace on save and others use git hooks or code review bots to block source code files that have trailing whitespace.)
I imagine your motivation to keep the trailing whitespace is so that you can leave Insert mode and then later enter Insert mode again on that same line, while preserving indentation. But it turns out you don't really need to do that, since Vim has many ways to start Insert mode again with the proper indentation.
For example, if you insert a line above (with O) or below the current one (with o), Vim will insert the appropriate indentation on the new line. If you're on a blank line and want to start inserting there, then instead of using i to start Insert mode, use S to replace the contents of the current line. Since the line is empty, there won't be anything to replace, but the S command will start the replacement with the current indentation, so that should solve that too.
If you're already in Insert mode (and not necessarily at the beginning of a line), you can also use Ctrl+F to have Vim apply the proper indentation to the current line, so that's an option too. With some languages (notably Python), Vim can't always figure out the proper indentation (since you indent to end a block), so Vim might not be able to guess right. In those cases, you also have Ctrl+T to increase indentation and Ctrl+D to decrease it. Both are Insert mode commands and can be executed anywhere in the line.
Hopefully with the use of these commands you'll be able to let go of the trailing spaces that you're using to track indentation and also drop those mappings.
A good starting point to debug this might be comparing both original and modified versions after vim says its been edited.
To see the changes, use git diff if the file is VCS controlled, or vim -d original_copy edited_copy if it's not.
The diff-ed characters should give you an idea of the side-effect of the code that might be causing this. Paste the diff here if you can't figure that out.
My after/ftplugins/fortran.vim has the following lines
let fortran_fold=1
let fortran_fold_multilinecomments=1
set foldmethod=syntax foldlevel=1 foldnestmax=2
let fortran_free_source=1
let fortran_do_enddo=1
If I open a fortran file, I don't see folded code, but when I change foldmethod to indent it works and I see code folded based on indentation.
The same settings work if I put them in init.vim.
I also have
syntax on
filetype indent plugin on
in my init.vim.
Nvim enables filetype plugin indent on and syntax on by default (:h nvim-defaults). So, just nvim test.for (without any init.vim) would do the right thing.
But when doing it manually, the order does matter! This is correct:
filetype plugin indent on
syntax on
Vim suffers the same issue.
I'm not sure if this is the right place to ask about this, but I figured it couldn't hurt to ask here. I am using a plugin called auto-close so that I don't have to close my own parentheses. It has a very nice feature that does the following:
This is a great feature, but I don't like how far it indents for me.
I have the following line in my .vimrc:
" for filetype "js", tab = insert 4 spaces, backspace will delete all 4
autocmd Filetype javascript setlocal expandtab softtabstop=4
In editing a javascript file, it automatically did an 8-space indentation instead of a 4-space indenation, as I've specified in my .vimrc. Can anybody help me figure out how I can make it automatically indent 4-space tabs instead of 8-space tabs? I can't find it in the documentation either. Thanks!
If you get shiftwidth=8, softtabstop=0, tabstop=8, that means that your autocmd FileType didn't take effect. You'd have to troubleshoot that.
I would recommend putting any settings, mappings, and filetype-specific autocmds into ~/.vim/ftplugin/{filetype}_whatever.vim (or {filetype}/whatever.vim; cp. :help ftplugin-name) instead of defining lots of :autocmd FileType {filetype}; it's cleaner and scales better; requires that you have :filetype plugin on, though. Settings that override stuff in default filetype plugins should go into ~/.vim/after/ftplugin/{filetype}.vim instead. The change of indent settings would fit the latter, after directory location.
I know that gg=G is one of the simplest and easiest commands to indent lines in vim.
But, is there a way to indent lines as I type and press Enter?
Yes. Add the following to your vimrc:
set autoindent
filetype plugin indent on
Autoindent just indents based on the previous line's indentation if it does not know how to indent. Filetype indentation uses some things vim knows about what kind of programming language you are writing in to indent it correctly. Filetype indentation will override autoindentation.
I'm using spf13-vim The Ultimate Vim Distribution. One of the my c files' colorscheme is not working. Now whole file is displaying like a commented block. I could not figure out which of my actions caused this problem. So if anybody has any idea, please share.
set filetype and set syntax is not showing anything.
:set filetype
filetype=
:set syntax
syntax=