Auto-indent the whole text file shortcut? - vim

What's the shortcut for autointenting the whole text file (like .c files or .java files). It should be equivalent to the tab in emacs but I couldn't find a correct way to do this online.

To reindent the whole file can be done with.
gg=G
gg moves to the top of the file.
=G reindents from the cursor position to the end of the file.

In addition to #FDinoff's suggestion, you could also use autocmd/au for user-defines filetype indentation settings. Like so-
au FileType c setl sw=8 ts=8 noet (For C);
au FileType python setl sw=4 ts=4 et (for python)
and so on..

Related

Make vim recognise COOL syntax

I am using vim 8.1 on macOS (vim-plug for plugin management). I need to work with COOL files, so I manually downloaded the syntax file and put it in ~/.vim/syntax
I added the following line to my .vimrc in order to associate the .cl extension with COOL:
au BufNewFile,BufRead *.cl setf cool
However, when I load .cl files into vim, it fails to recognise the language.
:verbose set ft ?
yields:
filetype=lisp
Last set from /usr/local/Cellar/vim/8.1.0650/share/vim/vim81/filetype.vim line 875
Changing the .virmc line to:
au BufNewFile,BufRead *.cool setf cool
makes vim recognise the files as COOL.
What should I do in order to have .cl default to COOL instead of LISP?
Create the file ~/.vim/filetype.vim with the following content:
augroup filetypedetect
au BufNewFile,BufRead *.cl setf cool
augroup END
See :help new-filetype.

VIm - How to remove right margin

How do I remove the right margin in Vim? It seems to be adding a carriage return and line feed (column 80?) whenever I save a file as .txt or .md. This doesn't happen if I save the file with no extension, but I want to be able to edit .txt files.
I've tried :set nowrap but this doesn't have the desired effect. I also commented out everything in my .gvimrc file in case I inadvertently did something there but no luck with that approach.
The text filetype in Vim sets the textwidth option to 78 by default, which has the effect of splitting lines. If you remove the textwidth option by setting it to zero, Vim will stop splitting lines. You can remove textwidth by hand with
:set textwidth=0
or you could add something like
autocmd FileType text setlocal textwidth=0
to your .vimrc.
I was able to resolve this by commenting out the following line in the vimrc_example.vim file:
" autocmd FileType text setlocal textwidth=78
Here's the path to the file on my machine:
C:\Program Files (x86)\Vim\vim80\vimrc_example.vim
I'm using gVim.
With the current monitor sizes (4K and bigger) the 78 character rule is not valid anymore (valid in the era before the 21" CRT monitors)
I tried the solution by Jon, but it did not work for git commit messages.
I had to change the .vimrc line to
autocmd FileType * setlocal textwidth=0
Now it works for any file type.

Vim autocompletion word with character _ , in python and latex

In vim, I managed to have autocompletion installing Supertab.
When I work on a .py file, it works ok: I can autocomplete xxx_yyy by typing xxTAB (and it pops up options if many xxx_yyy1 xxx_yyy2 exist).
But on a .tex file, if I have already the word xxx_yyy, when I type xxTAB I get the only match xxx.
How can I match xxx_yyy with xxTAB in a .tex file too?
This is my .vimrc :
filetype plugin indent on
syntax on
set backspace=indent,eol,start
autocmd Filetype python setlocal expandtab tabstop=4 shiftwidth=4
set ww=<,>,[,]
SuperTab uses the built-in insert mode completion (:help i_CTRL-N), and that is based on keywords. This setting is filetype-specific, controlled by the 'iskeyword' option. For Python, the _ is included, for Latex, it isn't (and based on #Konrad Rudolph's comment, for a reason).
You can certainly adapt this if it bothers you. In your ~/.vimrc:
autocmd Filetype tex setlocal iskeyword+=_

Wrong indentation when editing Yaml in Vim

Vim does not seem to correctly react at a dash symbol in YAML files therefore breaking the format.
For example I have a block which should look like this:
handlers:
- name: restart exim4
service: name=exim4 state=restarted
When I finish typing restart exim4 and type service: Vim reindents my final service line:
handlers:
- name: restart exim4
service: name=exim4 state=restarted
So clearly Vim tries to align sentences column-wise but that's not what is needed in YAML. I want to create an array with two values.
How to fix that?
In order to get the nice 2-space YAML as the default when I hit carriage return after the colon, I added this to my .vimrc:
autocmd FileType yaml setlocal ts=2 sts=2 sw=2 expandtab
This also plays nice with the dash for the OP's expected result.
Install this plugin:
https://github.com/chase/vim-ansible-yaml
It is made with Ansible in mind, but in theory it will work with all kinds of YAML files. You will have to :set filetype=ansible unfortunately, if you are not working with ansible related files.
You can use this autocommand to make Vim properly indent YAML files (put it to your .vimrc):
" Fix auto-indentation for YAML files
augroup yaml_fix
autocmd!
autocmd FileType yaml setlocal ts=2 sts=2 sw=2 expandtab indentkeys-=0# indentkeys-=<:>
augroup END
Basically, for YAML file it instructs Vim to:
Use 2 spaces for indentation.
Use spaces instead of tabs.
Skip re-indenting lines after inserting a comment character (#) at the beginning of a line, or a colon.
You can disable reindent when you type : key:
:set indentkeys-=<:>
Please edit ~/.vimrc file, and add these lines:
filetype plugin indent on
autocmd FileType yaml setl indentkeys-=<:>
Note: autocmd comes after filetype.
You can trigger reindent by typing CTRL-F in INSERT mode, for example:
hello: world
foo: bar<C-F>
You can add a comment in your YAML to tell Vim special config for this file. For example:
# vim: set shiftwidth=2 tabstop=2 softtabstop=-1 expandtab:
foo:
bar:
- a
- b
Then everyone, who use this file with a default vim, can share the file creator's configuration. It works well especially when cooperating.
I've found https://github.com/stephpy/vim-yaml to work great. It's a vim plugin that does indentation (and syntax highlighting) of yaml files. Installing it solves the specific problem that you've asked about too.
You need to install the plugin (see the doco in the GitHub repo) and as long as your filetype=yaml it'll correct your indenting. It'll help you both
as you're typing the snippet you've provided, or
if you already have some yaml written, you can select it (with V for line-wise selection then use j or k to select more lines) then trigger the vim formatting with =
Here's the augroup I have for yaml:
augroup filetype_yaml
autocmd!
autocmd BufNewFile,BufReadPost *.{yaml,yml} set filetype=yaml foldmethod=indent
autocmd FileType yaml |
setlocal shiftwidth=2 |
setlocal softtabstop=2 |
setlocal tabstop=2
augroup END
I have found a very good answer instead of updating .vimrc.
here by Ignacio Vazquez-Abrams

How can I get MacVim to properly indent my .vimrc

I'm going through the VimCasts.org archive of videos and number 5 covers using Vim's auto-indentation to format source code. I see it working properly in my Objective-C file but not my .vimrc.
My tab settings are as follows:
set tabstop=2
set shiftwidth=2
set softtabstop=2
set expandtab
My .vimrc file has the following if block:
if has("autocmd")
filetype on
autocmd BufNewFile,BufRead *.rss,*.atom setfiletype xml
autocmd BufWritePre *.py,*.js :call <SID>StripTrailingWhitespaces()
endif
I would think that if I placed the cursor on the first line above and pressed Vjjjj= I would get the second, third and fourth line indented by two spaces, but what I get instead is:
if has("autocmd")
filetype on
autocmd BufNewFile,BufRead *.rss,*.atom setfiletype xml
autocmd BufWritePre *.py,*.js :call <SID>StripTrailingWhitespaces()
endif
Are my expectations incorrect or is this correct for some reason given the Vimscript language?
You need to add filetype plugin indent on to your vimrc to get vim to do indentation properly. (The plugin part isn't really necessary but is nice to have)
I would recommend replacing the filetype on line with filetype plugin indent on

Resources