Adding a ":" command inside autocmd in .vimrc - vim

I have this in my .vimrc:
autocmd Filetype mkd call SetWritingOptions()
function SetWritingOptions()
setlocal guioptions-=T
colorscheme pencil
setlocal background=light
endfunction
I have a plugin which I activate by doing :VimroomToggle
Now, I would like to add that to the SetWritingOptions() function, so :VimroomToggle gets called each time I open a mkd file. How to do that?

Using this line in the function:
VimroomToggle
Commands you call in functions are the same as functions you enter using the : leader.

Related

Stop vim from folding git commit

Vim automatically folds my code when I open a new file. How can I stop it from folding the text when I run git commit -a, since I don't want folding only in this specific case?
I currently have this line in my code setlocal foldmethod=syntax to fold all code automatically.
I tried to add this line before and after setting the foldmethod autocmd FileType gitcommit setlocal nofoldenable, but it did not changing anything.
Put this into after/ftplugin/gitcommit.vim in your runtime (~/.vim) or a plugin:
setlocal nofoldenable
Alternately, in your .vimrc,
autocmd FileType gitcommit setlocal nofoldenable

How do I programmatically execute the 'ColorSupportSave' command?

What I want to do:
Given: I'm using the colorsupport.vim plugin to make my 'gui' colorscheme work in terminal.
And: colorsupport.vim has a command 'ColorSupportSave' that let's me save the 'converted' colorscheme.
Then: On startup, I want to use the 'converted' colorscheme if present, else create it
I would think, that after checking the 'ColorSupport' exists and the converted colorscheme does not, that I could just
execute 'colorscheme benjamin'
" then either
execute 'ColorSchemeSave benjamin-colorsupport'
" or lower-level
call s:colorscheme_save("benjamin-colorsupport")
but with the former I get 492: Not an editor command: ColorSchemeSave benjamin-colorsupport
and with the latter I get E117: Unknown function: <SNR>19_colorscheme_save
clearly I don't understand how these functions/commands are different from others I'm successfully scripting. (I'm new at this. I've read docs and other questions, but haven't quite figured it out)
Here's how the colorsupport.vim function and commands are defined, in brief
function! s:colorscheme_save(...)
" snip
endfunction
command! -nargs=? ColorSchemeSave :call s:colorscheme_save(<f-args>)
Here are, with more context, the relevant parts of my vimrc
set nocompatible " be iMproved
filetype off " required!
set runtimepath+=~/.vim/bundle/vundle/
call vundle#begin()
Plugin 'gmarik/vundle'
Plugin 'vim-scripts/colorsupport.vim'
call vundle#end() " required
filetype plugin indent on " required
if has("gui_running")
" stuff
else
if &term != 'cygwin'
silent !echo "setting 256 color term"
set t_Co=256
endif
let mycolorscheme = 'benjamin'
" wrap color scheme in gui->term plugins
if exists('##ColorScheme')
if filereadable(expand("$HOME/.vim/colors/".mycolorscheme."-colorsupport.vim")) ||
\ filereadable(expand("$HOME/vimfiles/colors/".mycolorscheme."- colorsupport.vim"))
silent !echo "using colorsupport.vim with ".mycolorscheme."-colorsupport"
execute 'colorscheme '.mycolorscheme.'-colorsupport'
else
silent !echo "using colorsupport.vim with ".mycolorscheme
execute 'colorscheme '.mycolorscheme
" can't figure out how to get this working in the script
" silent !echo "using colorsupport.vim with ".mycolorscheme."-colorsupport"
" execute 'ColorSchemeSave '.mycolorscheme.'-colorsupport'
" or lower-level
" call s:colorscheme_save("'.mycolorscheme.'-colorsupport")
endif
endif
endif
Answers culled from the responses:
refs:
https://stackoverflow.com/a/26739615/879854
https://stackoverflow.com/a/17688716/879854
https://twitter.com/garybernhardt/status/529664734040432640
vimrc is loaded before plugins.
If you look at :h initialization you will find that step 3 is load
vimrc and step 4 is load plugins.
You can also see that vimrc is loaded before plugins by looking at the
output of :scriptnames. scriptnames lists all sourced scripts in the
order they were sourced and vimrc is the first thing sourced. (Take a
look at :h :scriptnames).
So create the file .vim/after/plugin/colorsupport.vim
http://vim.wikia.com/wiki/How_to_initialize_plugins
it is also possible to achieve the same goal utilizing the autocmd
event VimEnter. Because Event VimEnter is performed after all other
startup stuff, commands called with this autocmd will take place after
the loading of all plugins. The procedure is as following:
First create a function including all the plugin specific scripts in
it.
function! g:LoadPluginScript ()
" ColorSupport {{{
if exists(":ColorSchemeSave")
" stuff
endif
" }}}
endfunction
augroup plugin_initialize
autocmd!
autocmd VimEnter * call LoadPluginScript()
augroup
Why your attempts don't and can't work
Plugins are sourced after your ~/.vimrc so you can't call functions and commands defined in plugins as they simply don't exist yet. The only exception to this rule is autoload functions but that's irrelevant here.
You can't call :Command or function() if they are not defined directly in your ~/.vimrc or another file explicitly sourced by your ~/.vimrc.
Additionally, the s: prefix in s:colorscheme_save() tells you that the function is scoped to its script and thus can't be called from another one.
What you should do instead
Write your terminal-compatible colorscheme to disk with a clear name, say foobar_term.vim.
Add this little conditional to your ~/.vimrc:
if has("gui_running")
colorscheme foobar
else
colorscheme foobar_term
endif

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

Vim plugin : Rainbow parentheses using tab

I'm using vim 7.3 and Rainbow Parentheses plugin. When opening multiple tabs with vim -p file1 file2 or with vim -S session.vim, or even with tabnew file or any other method, my parenthesis are colored in only one file.
I just put this into my .vimrc : au VimEnter * RainbowParenthesesToggle
as said here. I tried to use :RainbowParenthesesToggle on the other tabs once opened but it only toggles in the parenthesis-activated tab.
What should I do to make things work in all tabs ?
I made it work by adding the same instructions as here in my .vimrc, thanks to FDinoff. I replaced the last instruction to make it work using tab, as I intended first.
function! Config_Rainbow()
call rainbow_parentheses#load(0)
call rainbow_parentheses#load(1)
call rainbow_parentheses#load(2)
endfunction
function! Load_Rainbow()
call rainbow_parentheses#activate()
endfunction
augroup TastetheRainbow
autocmd!
autocmd Syntax * call Config_Rainbow()
autocmd VimEnter,BufRead,BufWinEnter,BufNewFile * call Load_Rainbow()
augroup END
The VimEnter flag on the autocommand tells vim to perform the command specified (in this case RainbowParenthesesToggle only when starting the editor, which is in your case when you open the first file.
If you want to extend the functionality to everytime you load a buffer you should do something like:
autocmd BufRead,BufNewFile * RainbowParenthesesToggle

Function to source .vimrc and .gvimrc

I generally use GVim, but most of my configuration is done via .vimrc (like keymappings) because I want them in vim and gvim. So when I edit my vimrc and then source it from gvim, I have to source my .gvimrc after that in order to get my colorscheme back (since it's gvim only). I tried to write a function to do this, and ran into the problems described in the comments below:
function ReloadConfigs()
:source ~/.vimrc
if has("gui_running")
:source ~/.gvimrc
endif
endfunction
command! Recfg call ReloadConfigs()
" error: function already exists, add ! to replace it
function! ReloadConfigs()
:source ~/.vimrc
if has("gui_running")
:source ~/.gvimrc
endif
endfunction
command! Recfg call ReloadConfigs()
" error: cannot replace function, it is in use
Is it possible to do something like this? Or, since my .gvimrc only has a few lines, should I just put its contents into an if has("gui_running") block?
You've put your function somewhere in your .vimrc. This means that, while it's being executed, the :source .vimrc is trying to redefine it, which is a problem. You could try doing this:
if !exists("*ReloadConfigs")
function ReloadConfigs()
:source ~/.vimrc
if has("gui_running")
:source ~/.gvimrc
endif
endfunction
command! Recfg call ReloadConfigs()
endif
If the function is already defined, this should skip redefining it, avoiding the issue.
I would say that whatever you have in your .vimrc that's messing up gvim settings should be surrounded by an if !has("gui_running") block.
An autocmd seems to be the easiest way of handling what you're trying to do:
autocmd BufWritePre .gvimrc,.vimrc source <amatch>
This way you get your configuration file automatically reloaded when you save it without having to mess around with functions. Alternatively, you could use a mapping to trigger :source $MYVIMRC or :source $MYGVIMRC.

Resources