Use a for loop in vimscript to automate autocmd-calls - vim

I have different vim-scripts for different filetypes
.
├── .vim
│   ├── vimrc-c.vim
│   ├── vimrc-cpp.vim
│   ├── vimrc-default.vim
│   ├── vimrc-lisp.vim
│   ├── vimrc-pl.vim
│ └── vimrc-vim.vim
└── .vimrc
and my first solution was
au BufNewFile,BufRead * source $HOME/.vim/vimrc-default.vim
au BufNewFile,BufRead *.c source $HOME/.vim/vimrc-c.vim
au BufNewFile,BufRead *.cpp source $HOME/.vim/vimrc-cpp.vim
au BufNewFile,BufRead *.lisp source $HOME/.vim/vimrc-lisp.vim
au BufNewFile,BufRead *.pl source $HOME/.vim/vimrc-pl.vim
au BufNewFile,BufRead *.vim source $HOME/.vim/vimrc-vim.vim
but i would like do replace that by a for loop - since it's an simple repetition - in a way like
au BufNewFile,BufRead * source $HOME/.vim/vimrc-default.vim
let s:extensions =
\[
\c,
\cpp,
\lisp,
\pl,
\vim,
\]
for ext in s:extensions
au BufNewFile,BufRead '*' . ext source $HOME . "/.vim/vimrc-" . ext . '.vim'
endfor
but that's not working. I don't get an error though, so I don't know what to do.

Vim already has a built-in mechanism for sourcing filetype-specific configuration files so reimplementing it, and in a non-portable way to boot, is totally pointless.
Here is how your directory structure should look like:
.
├── .vim/
│ └── after/
│ └── ftplugin/
│ ├── c.vim
│ ├── cpp.vim
│ ├── lisp.vim
│ ├── perl.vim
│ └── vim.vim
└── .vimrc
The only requirement is to have the line below in your vimrc:
filetype plugin indent on
See :help filetypes.
A few notes:
The filename of your "ftplugins" matches the name of the filetype, not the extension. For example, Perl files have a .pl extension but their filetype is perl, so your ftplugin should be called perl.vim.
Whatever you have put in your vimrc-default.vim should be in your vimrc.

Related

How to turn off HtmlBeautify for md files vim

I want to turn off htmlbeautify for md files.
I am using the plug in vim-jsbeautify and use a plug in on save the following way: in my vimfiles/ftplugin folder
├── ftplugin
│   ├── html
│   │   └── main.vim
I have added a html folder and added a main.vim file with the content
autocmd BufWritePre <buffer> call HtmlBeautify()
noremap <buffer> <c-f> :call HtmlBeautify()<cr>
so that html files are formatted on save. The command verbose set filetype returns
filetype=markdown
Last set from /usr/share/vim/vim80/filetype.vim
for md files
Some how the md files are also considered of html files. how can I turn this of?
Inside $VIMRUNTIME/ftplugin/markdown.vim you will find the following line:
runtime! ftplugin/html.vim ftplugin/html_*.vim ftplugin/html/*.vim
This will source html ftplugins for markdown files as Markdown uses html in its syntax.
One way to prevent this is do add an :if to avoid these inclusion for markdown.
if &filetype == 'html'
autocmd BufWritePre <buffer> call HtmlBeautify()
noremap <buffer> <c-f> :call HtmlBeautify()<cr>
endif

xml.vim plugin is nonfunctional

I can't seem to make any xml.vim plugin work for my vim setup. I have tried two (https://www.vim.org/scripts/script.php?script_id=1397 and its predecessor https://www.vim.org/scripts/script.php?script_id=301). They seem totally nonfunctional. I have tried to view their documentation from within vim (:help xml C-d), and nothing appears except the default xml syntax help.
I have been installing them using pathogen, the standard way (e.g. git clone https://github.com/sukima/xmledit.git ~/.vim/bundle/xmledit). After installation, I have tried opening a test.xml file to load helptext, with no results.
I saw the other question posted here with no resolution, and it didn't help: turn on xml.vim ftplugin in vim
So, does anyone know why this is going wrong? This is running on Ubuntu through Windows Subsystem for Linux, but I have other working plugins (tslime and vim-slime).
Edit: After looking at this some more, I tried :scriptnames and the plugin (currently xmledit) doesn't appear to be in it, although the bundle/xmledit directory is in the runtimepath. Here are the contents of bundle/xmledit:
xmledit/
├── build.vim
├── doc
│   └── xml-plugin.txt
├── ftplugin
│   ├── html.vim
│   ├── php.vim
│   ├── xhtml.vim
│   └── xml.vim
├── Makefile
├── README.mkd
└── tests
├── Gemfile
├── Gemfile.lock
├── Rakefile
├── README.md
└── spec
├── spec_helper.rb
└── xmledit_spec.rb
vim version: 7.4. included patches: 1-1689, extra patches: 8.0.0056
.vimrc:
" mapleader definition has to be on top:
let mapleader="," " leader is comma
" necessary for pathogen:
execute pathogen#infect()
" the good settings:
set background=dark " obvious
set tabstop=4 " number of visual spaces per tab
set shiftwidth=4 " similar
set softtabstop=4 " number of spaces added/removed while editing
set expandtab " tabs become spaces
set smartindent " do smart indenting when starting a new line
set autoindent " copies previous indent when starting a newline
filetype indent on " makes filetype-based indenting work
set number " show line numbers
set wildmenu " cyclical menu for autocompletion
set showmatch " highlight matching bracket
set incsearch " search as you type
set hlsearch " highlight search results
" turn off search highlight
noremap <leader><space> :nohlsearch<CR>
" move vertically by visual line (don't skip over wrapped lines)
nnoremap j gj
nnoremap k gk
" highlight last inserted text - doesn't work??
" nnoremap gV `[v`]
" edit vimrc with ev
nnoremap <leader>ev :vsp ~/.vimrc<CR>
" load vimrc with sv
nnoremap <leader>sv :source ~/.vimrc<CR>
" save session (reopen with vim -S)
nnoremap <leader>s :mksession<CR>
" set html indentation lower:
autocmd FileType html setlocal softtabstop=2 shiftwidth=2 tabstop=2
" for scheme/lisp:
let g:tslime_ensure_trailing_newlines = 1
" don't use, but useful to have documented:
" set cursorline " highlight current line
" idk:
"highlight Normal ctermbg=LightGray
"syntax enable " enables syntax highlighting; not sure why this is disabled but it works anyway?
" FROM good vimrc
" CtrlP settings
"let g:ctrlp_match_window = 'bottom,order:ttb'
"let g:ctrlp_switch_buffer = 0
"let g:ctrlp_working_path_mode = 0
"let g:ctrlp_user_command = 'ag %s -l --nocolor --hidden -g ""'
" --- NOTES ---
" C-[ is escape.... ugh...
" also, somehow M-; is still getting through???
" mapleader is ,
" useful page: https://dougblack.io/words/a-good-vimrc.html
" most keybindings are from here
" RETURN TO:
" silver searcher? see good vimrc ^ for it
" also ctrl-p. I should get both of these
Ok it finally started working. I solved it by trying a few things that I thought were not necessary for Vim 7.4:
I added a filetype plugin on line after execute pathogen#infect().
I added a filetype off line to .vimrc before execute pathogen#infect().
I also used the :helptags command (which I thought Pathogen did on its own).
I don't have the patience to spend more hours identifying exactly which configuration is necessary to fix the issue, but hopefully this is sufficient for similar problems. My guess is that turning filetype plugins on is the primary culprit.

How do I set a .vimrc configuration for many file types?

Let's say I want one group of settings for HTML, CSS and JavaScript files but another set for Ruby files (completely different). What's the simplest way to do this?
You can put ftplugin directory with filetype-specific settings inside
.vim directory
.vim
└── ftplugin
└── ruby.vim
└── markdown.vim
And keep your settings there. The are applied when file with
corresponding filetype is opened.
Also, you might need to have filetype detection(if not detected
properly). You can put this to your .vimrc
autocmd BufNewFile,BufRead *.markdown,*.md,*.mdown,*.mkd,*.mkdn set ft=markdown
Or, put it into ftdetect directory
.vim
└── ftdetect
└── markdown.vim
You can give global settings like this.
For other files:
set shiftwidth=4
for Ruby files:
autocmd BufRead,BufNewFile *.rb set shiftwidth=2
You can get what you want via autocmd, it's format as follows:
au[tocmd] [group] {event} {pattern} [nested] {cmd}
[group] and [nested] are optional.As I give the example above, BufRead,BufNewFile is the event, *.rb is the pattern, and set shiftwidth=2 is the cmd.
more information about autocmd, please refer: :help automcd

writing to .vimrc breaks pathogen?

I have pathogen set up in my vim installation and various plugins installed.
"Call pathogen to set up various plugins
"filetype off
call pathogen#infect()
call pathogen#incubate()
call pathogen#helptags()
When I write my .vimrc in vim, the following command is supposed to reload the file (and it does seem to work).
" Source the vimrc file after saving it
if has("autocmd")
autocmd bufwritepost .vimrc source $MYVIMRC
endif
After writing .vimrc, however, pathogen does not reload.
Here is the output of :set rtp? after starting vim:
runtimepath=~/.vim,~/.vim/bundle/Jellybeans,~/.vim/bundle/TwitVim,~/.vim/bundl
e/badwolf,~/.vim/bundle/calendar,~/.vim/bundle/tagbar,~/.vim/bundle/vim-airline,
~/.vim/bundle/vim-colors-solarized,~/.vim/bundle/vim-colorschemes,~/.vim/bundle/
vizardry,/var/lib/vim/addons,/usr/share/vim/vimfiles,/usr/share/vim/vim74,/usr/s
hare/vim/vimfiles/after,/var/lib/vim/addons/after,~/.vim/after
and after :w in .vimrc it returns to the default.
runtimepath=~/.vim,/var/lib/vim/addons,/usr/share/vim/vimfiles,/usr/share/vim/
vim74,/usr/share/vim/vimfiles/after,/var/lib/vim/addons/after,~/.vim/after
I tried adding the following modification to no avail...
if has("autocmd")
autocmd bufwritepost .vimrc source $MYVIMRC
autocmd bufwritepost .vimrc call pathogen#incubate()
endif
I've been looking around and can't seem to find a solution other than just restarting vim every time I modify my .vimrc, which is fairly disruptive. Any help would be appreciated.
edit: output of tree -d -L 2...
.
├── autoload
└── bundle
├── badwolf
├── calendar
├── color~
├── Jellybeans
├── tagbar
├── TwitVim
├── vim-airline
├── vim-colorschemes
├── vim-colors-solarized
└── vizardry
The problem may be the following lines on your .vimrc:
" This line should not be removed as it ensures that various options are
" properly set to work with the Vim-related packages available in Debian.
runtime! debian.vim
On $VIMRUNTIME/debian.vim there is a line that reset the runtime path:
" Debian system-wide default configuration Vim
set runtimepath=~/.vim,/var/lib/vim/addons,/usr/share/vim/vimfiles,/usr/share/vim/vim74,/usr/share/vim/vimfiles/after,/var/lib/vim/addons/after,~/.vim/after
The problem doesn't happens when the .vimrc is loaded on startup because these lines are executed before call pathogen#infect(). When you reload your .vimrc you are overwriting your &rtp, but pathogen doesn't set it again (possible because s:done_bundles is already set).

Vim bind some hotkeys different to filetype

I want to bind something like this:
For CSS, HTML files: <c-space> <c-x><c-n>
For Ruby files: <c-space> <c-x><c-u>
How to do this?
The documentation is more complete, precise and correct, but briefly:
Make a file in your ftplugin folder (create this folder if necessary) starting with the filetype you'd like to use.
├── vim
│   ├── after
│   ├── autoload
│   ├── bundle
│   ├── ftplugin
│   │   ├── python.vim
│   │   ├── rnoweb.vim
│   │   └── tex.vim
│   └── syntax
and in one of of these files, the python.vim one for example, a line like
noremap! <buffer> <F5> <Esc>:w<CR>:!python % <CR>
will be a bind only defined for this filetype, just in this buffer. Make sure filetype plugin on or similar appears in your vimrc.
Edit: excellent suggestions by Peter Rincker incorporated: noremap version of map command and <buffer> flag. He also reminds us to use setlocal instead of set in our filetype plugin files.
Use autocmd:
autocmd filetype css inoremap <buffer> <c-space> <c-x><c-n>
autocmd filetype html inoremap <buffer> <c-space> <c-x><c-n>
autocmd filetype ruby inoremap <buffer> <c-space> <c-x><c-u>
Put the following in your .vimrc. Substitute the do stuff lines with your bindings.
:if match(#%, ".css") >=0
: do css stuff
:endif
:if match(#%, ".rb") >=0
: do ruby stuff
:endif

Resources