I've been trying to take my vim/neovim game to the next level and one of the things I've been trying to do is fix the highlighting issues I've been having. The gist of it is that at times, syntax, visual selection, and search highlighting use the same/similar color for the highlighting as the original text, making it difficult to see what has been highlighted. Does anyone know how i might be able to fix this? based on the first screenshot, it looks like its completely capable of doing so, I just can't seem to find a config option to make it consistent.
Here are some examples (while I was using neovim in these, the behavior is the same w/ vim):
note how the some of the text is almost impossible to read while highlighted? (it was whitish before highlighting) the places highlighted in red/blue are much more how i would have expected highlighting to work all the time (the highlight color more or less matches the original text color, but when highlighted, the text color was changed to make the selection readable).
all occurrences of "if" are highlighted. again, note how the text and highlighting are pretty much indistinguishable.
note how the syntax highlighting (of the [ ] pair in this case) mostly hides what the 2 surrounding characters are. this also happens with (, ), {, and }.
.vimrc (init.vim essentially identical):
syntax enable
:color peachpuff
set nocp
set wildmenu
set relativenumber
set number
set ignorecase
set smartcase
set incsearch
set nowrap
set tabstop=4
set softtabstop=4
set expandtab
set shiftwidth=4
set showcmd
set clipboard=unnamed
set laststatus=2
set clipboard+=unnamedplus
set directory=~/.vim/tmp
"sudo after opening:
cmap w!! w !sudo tee >/dev/null %
"formatting brackets and such:
inoremap {<Tab> {}<Esc>i
inoremap {<CR> {<CR>}<Esc>ko<Tab>
inoremap (<Tab> ()<Esc>i
inoremap (<CR> (<CR>)<Esc>ko<Tab>
inoremap [<Tab> []<Esc>i
inoremap [<CR> [<CR>]<Esc>ko<Tab>
Just try a different colorscheme. This plugin has loads:
https://github.com/flazz/vim-colorschemes
I'd recommend gruvbox, twilight or apprentice, but there are lots of great ones, find one that works for you.
You can install this plugin to be able quickly switch to the next colorscheme:
https://github.com/xolox/vim-colorscheme-switcher
Edit: here is a list of the colorschemes that you probably have installed by default in your vim:
https://github.com/vim/vim/tree/master/runtime/colors
If you want to try some without installing any plugins or anything, just do e.g. :colorscheme evening and see how they look. If you type :colorscheme <CTRL-D> you'll be shown which colorschemes are available.
Below is my vimrc configuration. I want the text to be wrapped to the next line when it reaches the textwidth maximum.
syntax on
set tabstop=4
set linebreak
set wrap
filetype indent off
set paste
set tw=120
set ruler
I'm using Macbook pro 2014, El Capitan, iterm2.
I have also tried set formatoptions+=cqtrol.
This is the effect of set paste. From the Vim help for paste:
When the 'paste' option is switched on (also when it was already on):
...
- 'formatoptions' is used like it is empty
The default value for formatoptions is vt, where t is required for auto-wrapping:
t Auto-wrap text using textwidth
Refer to :help fo-table.
So you should remove set paste from your configuration, or unset it temporarily via set nopaste command.
I recommend keeping nopaste by default, i.e. removing set paste, and using the pastetoggle option instead. For example, the following command configures F12 key for toggling the paste mode.
set pastetoggle=<F12>
I want to automatically set the paste mode on when I open a new file (an empty file that doesn't exist).
autocmd BufNewFile * :set paste
in the vimrc does the trick with vim newfile or :e newfile, but not with :enew.
How can I run an autocommand on :enew?
set paste will have many side effects, e.g. disable indention. I would imagine the requested behavior would become annoying quickly.
Alternatives:
Use 'pastetoggle' setting to setup a key to toggle 'paste',
unimpaired.vim's yo mappings which put Vim in insert mode with 'paste' set and disables 'paste' upon leaving insert mode
vim-bracketed-paste enables transparent pasting into Vim for certain terminals.
For more help see:
:h 'paste'
:h 'pastetoggle'
To answer the question, the following will run an autocommand on :enew:
autocmd BufCreate * if '' == expand("<afile>") | set paste | endif
You can replace BufCreate with BufAdd or BufNew, it will work all the same. The important part is the if '' == expand("<afile>") that will only execute the command if it the new buffer has no name.
NB:
For the specific use-case of pasting text in new buffer, auto-switching to paste mode is not a great solution though, I wouldn't recommend using this.
As the paste is maintained even when you switch buffer. You'd need additional autocommands to unset the paste mode when you switch buffer/window (autocmd WinEnter * set nopaste will work for windows, but for some reason autocmd BufEnter * set nopaste doesn't help when switching buffer).
To ease pasting stuff in new buffers (or otherwise), I'd recommend either:
typing "*p or "+p in normal mode, as to use vim's native mechanism to paste from the clipboard registers, as suggested by #dartNNN (assuming you're on your local machine)
using one of the plugins recommended in #peter-rincker's answer
I am an average vim user and with an avarage .vimrc containing:
set autoindent
It's not that bad in general, but when it comes to copying the content from outside, I get the indentation of this piece terribly messed up.
Even worse, if the text contains the comments like "//", they are propagated to the next lines of the copied content even if they originally were not commented.
gg=G doesn't help.
Is there any way to set the autoindent to be disabled on the action of copying something from outside?
This is described on the Vim wiki. Try adding this to your .vimrc:
set pastetoggle=<F2>
You can set any key - <F2> is just an example. Then just press your key to allow pasting, do your copy into Vim, then press the key again to return to normal autoindent behaviour.
How do I make vi-Vim never use tabs (converting spaces to tabs, bad!), makes the tab key == 4 spaces, and automatically indent code after curly brace blocks like Emacs does?
Also, how do I save these settings so I never have to input them again?
I've seen other questions related to this, but it always seems to be a little off from what I want.
As has been pointed out in a couple of other answers, the preferred method now is NOT to use smartindent, but instead use the following (in your .vimrc):
filetype plugin indent on
" show existing tab with 4 spaces width
set tabstop=4
" when indenting with '>', use 4 spaces width
set shiftwidth=4
" On pressing tab, insert 4 spaces
set expandtab
In your [.vimrc:][1] file:
set smartindent
set tabstop=4
set shiftwidth=4
set expandtab
The help files take a bit of time to get used to, but the more you read, the better Vim gets:
:help smartindent
Even better, you can embed these settings in your source for portability:
:help auto-setting
To see your current settings:
:set all
As graywh points out in the comments, smartindent has been replaced by cindent which "Works more cleverly", although still mainly for languages with C-like syntax:
:help C-indenting
Related, if you open a file that uses both tabs and spaces, assuming you've got
set expandtab ts=4 sw=4 ai
You can replace all the tabs with spaces in the entire file with
:%retab
The best way to get filetype-specific indentation is to use filetype plugin indent on in your vimrc. Then you can specify things like set sw=4 sts=4 et in .vim/ftplugin/c.vim, for example, without having to make those global for all files being edited and other non-C type syntaxes will get indented correctly, too (even lisps).
To have 4-space tabs in most files, real 8-wide tab char in Makefiles, and automatic indenting in various files including C/C++, put this in your ~/.vimrc file:
" Only do this part when compiled with support for autocommands.
if has("autocmd")
" Use filetype detection and file-based automatic indenting.
filetype plugin indent on
" Use actual tab chars in Makefiles.
autocmd FileType make set tabstop=8 shiftwidth=8 softtabstop=0 noexpandtab
endif
" For everything else, use a tab width of 4 space chars.
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.
On many Linux systems, like Ubuntu, the .vimrc file doesn't exist by default, so it is recommended that you create it first.
Don't use the .viminfo file that exist in the home directory. It is used for a different purpose.
Step 1: Go to your home directory
cd ~
Step 2: Create the file
vim .vimrc
Step 3: Add the configuration stated above
filetype plugin indent on
set tabstop=4
set shiftwidth=4
set expandtab
Step 3: Save file, by pressing Shift + ZZ.
The recommended way is to use filetype based indentation and only use smartindent and cindent if that doesn't suffice.
Add the following to your .vimrc
set expandtab
set shiftwidth=2
set softtabstop=2
filetype plugin indent on
Hope it helps as being a different answer.
From the VIM wiki:
:set tabstop=4
:set shiftwidth=4
:set expandtab
edit your ~/.vimrc
$ vim ~/.vimrc
add following lines :
set tabstop=4
set shiftwidth=4
set softtabstop=4
set expandtab
The auto-indent is based on the current syntax mode. I know that if you are editing Foo.java, then entering a { and hitting Enter indents the following line.
As for tabs, there are two settings. Within Vim, type a colon and then "set tabstop=4" which will set the tabs to display as four spaces. Hit colon again and type "set expandtab" which will insert spaces for tabs.
You can put these settings in a .vimrc (or _vimrc on Windows) in your home directory, so you only have to type them once.
Firstly, do not use the Tab key in Vim for manual indentation. Vim has a pair of commands in insert mode for manually increasing or decreasing the indentation amount. Those commands are Ctrl-T and Ctrl-D. These commands observe the values of tabstop, shiftwidth and expandtab, and maintain the correct mixture of spaces and tabs (maximum number of tabs followed by any necessary number of spaces).
Secondly, these manual indenting keys don't have to be used very much anyway if you use automatic indentation.
If Ctrl-T instead of Tab bothers you, you can remap it:
:imap <Tab> ^T
You can also remap Shift-Tab to do the Ctrl-D deindent:
:imap <S-Tab> ^D
Here ^T and ^D are literal control characters that can be inserted as Ctrl-VCtrl-T.
With this mapping in place, you can still type literal Tab into the buffer using Ctrl-VTab. Note that if you do this, even if :set expandtab is on, you get an unexpanded tab character.
A similar effect to the <Tab> map is achieved using :set smarttab, which also causes backspace at the front of a line to behave smart.
In smarttab mode, when Tab is used not at the start of a line, it has no special meaning. That's different from my above mapping of Tab to Ctrl-T, because a Ctrl-T used anywhere in a line (in insert mode) will increase that line's indentation.
Other useful mappings may be:
:map <Tab> >
:map <S-Tab> <
Now we can do things like select some lines, and hit Tab to indent them over. Or hit Tab twice on a line (in command mode) to increase its indentation.
If you use the proper indentation management commands, then everything is controlled by the three parameters: shiftwidth, tabstop and expandtab.
The shiftwidth parameter controls your indentation size; if you want four space indents, use :set shiftwidth=4, or the abbreviation :set sw=4.
If only this is done, then indentation will be created using a mixture of spaces and tabs, because noexpandtab is the default. Use :set expandtab. This causes tab characters which you type into the buffer to expand into spaces, and for Vim-managed indentation to use only spaces.
When expandtab is on, and if you manage your indentation through all the proper Vim mechanisms, the value of tabstop becomes irrelevant. It controls how tabs appear if they happen to occur in the file. If you have set tabstop=8 expandtab and then sneak a hard tab into the file using Ctrl-VTab, it will produce an alignment to the next 8-column-based tab position, as usual.
Afterall, you could edit the .vimrc,then add the conf
set tabstop=4
Or exec the command
Simplest one will be n vim file
set tabstop=4