How do you set up formatting in vim? - vim

Earlier, when I used open .py files in vim on ubuntu, they would be well formatted, with separate colours for separate segments of the program. Now, when I am using VIM on ubuntu, all the text in the .py file appears black. How can I correct this?

Formating in Vim means text formatting; e.g. indenting lists and breaking long lines. You're concerned about syntax highlighting, which is purely about the visual appearance of code.
First, it needs to be turned on.
:syntax on
does that.
Second, you probably want Vim to automatically detect the used language (e.g. Python) and choose the correct syntax plugin for you.
:filetype on
does that, though you usually enable more via :filetype plugin indent on.
To make these settings persistent, put them into your ~/.vimrc configuration.

Check man vim. In a nutshell, find a copy of a vimrc file, one might be under /usr/share/vim/ subtree. It may be named vimrc_example.vim. Copy to your home directory and rename it as .vimrc.

Related

Vim syntax doesn't highlight in real time

I have enabled vim syntax on (in ~/.vimrc syntax on) and it works but only on files with a code in when I view them. When I create a new file with vim and write there some code - no syntax highlight. After saving this file and reopening with vim - syntax highlight works perfect.
I am using manjaro KDE.
When you open a new file without an extension (vim mynewfile) none of vim’s filetype detection mechanisms can recognize it (they all use either extensions or first-couple-of-lines heuristics, which don’t work here).
When you enter code and reopen the file, the line-checks for filetypes work, causing the syntax to be set correctly, causing highlights to apply.
You can always set syntax=mine (though set filetype=mine is better) to set it manually.
This problem shouldnt happen when you do vim some.c or similar, because the extension will force detection based on extension rules.
Vim must know how to highlight your syntax in order to actually highlight it. One way to do this, is for Vim to check the file name and sometimes inspect the contents of the file, and set the file type. The file type is then used to highlight the syntax.
To enable detection of the file type (and load plugin and indent files), add the following to your vimrc:
filetype on plugin indent
If Vim is unable to detect the file type, and you have not yet saved your file with a known extension, you can set the file type manually, like this:
:set filetype=html
Vim will then highlight the syntax of the file as HTML syntax.
More information is available in the help pages.

set vim to autoindent using the same characters as the previous line

I use tabs for indentation in all my files. However, some bigger projects still use spaces, so I also need to follow those guidelines when I contribute.
This issue could easily be solved if the editor would detect the indent character used in the edited file, and use that one when e.g. autoindenting.
Sadly, vim forces spaces for indent, even when I am editing a file which otherwise only uses tabs.
How can I tell vim to always just use what the file is using (and use tabs if the file isn't using anything)?
I tried adding the following line (as I understand it, this should be forcing tabs - not exactly what I want, but better than inserting spaces everywhere) to ~/.vimrc and reopening the file I was editing, but this failed to take effect.
I am running vim-8.0.0386.
It looks like tpope/sleuth plugin is exactly what you want.
Otherwise, DetectIndent works too.
Clone the DetectIndent repo in your .vim/bundle/ directory (I use pathogen to install plugins), then open any file and type: :DetectIndent
Now shiftwidth, expandtab and tabstop should be adjusted to your file.
Here's another plugin recommendation: My IndentConsistencyCop plugin. It does not only detect the used indent setting, but also warns you if inconsistent settings are used within the same file.

Vim hard or soft tab depending on what's used in the file

Is there a vim command or plugin that will span the source file I'm opening, and adjust my tab setting to whatever is used in the file.
I currently work on a diverse codebase at work, with some source files using hard tabs, other 4 space soft tabs, and other's 2 space soft tabs. My hard tab, if I forgot to changes, can lead to ugly whitespace in different editors if I forget to change it when I'm editting a soft tab file.
I'd like to not have to remember to check whatever's convention is used in a file every time I open a buffer, and adjust my preferences accordingly.
I use yaifa with great success. It's pretty much an "install-and-forget" plugin that does what it says it does without ever getting in the way. Working in the messy environment I work in was horrible until I found that gem.
If most people at work use Vim (and/or Emacs), you can encode the indent settings within the file itself, see :help modeline (at least Emacs has something similar). Of course, agreeing on a common setting and gradually migrating to it would be even better :-)
In case you can't do that or things are just too chaotic, you need a plugin to dynamically adapt the indent settings. My IndentConsistencyCop plugin checks whether the buffer's indentation is consistent and conforms to tab settings, and then offers to adapt the settings to the detected ones. (The plugin page has links to alternative plugins.)
I've used the following function (found it somewhere on the web) with good success. Just put it into your .vimrc:
function Kees_settabs()
if len(filter(getbufline(winbufnr(0), 1, "$"), 'v:val =~ "^\\t"')) > len(filter(getbufline(winbufnr(0), 1, "$"), 'v:val =~ "^ "'))
set noet
else
set et
endif
endfunction
autocmd BufReadPost * call Kees_settabs()
Take a look at sleuth by the prolific Tim Pope:
This plugin automatically adjusts 'shiftwidth' and 'expandtab' heuristically based on the current file, or, in the case the current file is new, blank, or otherwise insufficient, by looking at other files of the same type in the current and parent directories. In lieu of adjusting 'softtabstop', 'smarttab' is enabled.
I wrote this because I wanted something fully automatic. My goal is that by installing this plugin, you can remove all indenting related configuration from your vimrc.
If your file is consistently indented with hard tabs, 'shiftwidth' will be set to your 'tabstop'. Otherwise, a 'tabstop' of 8 is enforced.

Vim highlighting weird parts of FORTRAN

I am using VIM theme molokai, if that makes any difference.
I have been learning FORTRAN lately and when I write a FORTRAN program using VIM, I have weird coloring depending on my whitespace.
For instance, if I tab things over as is (no indenting) I have this purple highlight on only a portion of the word (sometimes it isn't there, notice the PRINTs and READs).
If I tab it over it looks normal:
I am new to VIM (not to mention FORTRAN) so I am not sure what's happening, I don't mind tabbing over all the time but I think it looks a little ridiculous if the whole program is wasting that column of white space.
If you search :help fortran, you'll get a list of options that you can set.
These are the fortran options I set in my own .vimrc file. (I don't work with fixed-format code though) I know there are one or two fortran specific scripts that are available online, but I don't use them.
let fortran_free_source=1
let fortran_have_tabs=1
let fortran_more_precise=1
let fortran_do_enddo=1
Vim was using fixed-format/punchcard when I created a new .f90 file. In order to differentiate it, I found this.
I copied it to my ~/.vim/syntax/ directory and added the following lines to my .vimrc (also specified at following link)
nmap <S-F> :set syntax=fortran<CR>:let b:fortran_fixed_source=!b:fortran_fixed_source<CR>:set syntax=text<CR>:set syntax=fortran<CR>
nmap <C-F> :filetype detect<CR>
Allowing me to bind Shift-F to switch formats, and Ctrl-F to auto detect.
You can install the Fortran F90/95 indentation rules script in ~/.vim/after/indent, and it should fix the index errors.

Can you remove default color schemes from vim in a vimrc?

I've reduced the number of custom color schemes in vim to the bare minimum of great ones, but I still have all of the crazy defaults, like elflord, which I'd rather not have to tab through. Can I remove these without hacking my MacVim install and having to do it in the next update?
Obviously this is splitting hairs, but configuring my .vimrc in the first place is splitting hairs.
tmcw,
My installation of vim73 has the colors stored in \path\to\vim\vim73\colors.
In there I see elflord.vim and a host of other files.
Presumably removing them from this directory should remove them altogether.
Then, you can just tab through whatever you want.
Edit: If you want to have something that goes in and removes the files from the colors directory after an update, perhaps you may have to write a script to manage that--I'm not really sure.
HTH
I presume you are tabbing through your colorschemes for editing different filetypes? why not just set up your colorscheme preference in corresponding filetype-specific entries using autocmd FileType or via individual vim files in the ftplugin directory. Make sure you have filetype plugin on
How many colorschemes do you actually use? If it's just a few (and you really need to change them frequently, like whether it's dark or bright daylight), I'd define shortcut commands, like:
command -bar Col1 colorscheme default
command -bar Col2 colorscheme darkblue
You could remove the colorschemes from $VIMRUNTIME/colors/, but you're right about having to do this after every Vim upgrade.
A (complex) alternative would be writing a custom :Colorscheme command, with a custom completion that uses something like a blacklist to suppress certain schemes. As a bonus, with the cmdalias plugin, you could even overwrite the original :colorscheme command, to make this fully transparent.

Resources