Execute autocommand in vim - vim

I am using SimpleFold for folding vim. I mapped it to a key setting:
map <unique> <silent> <Leader>f <Plug>SimpleFold_Foldsearch
However, I would like to set it up as an auto command. I tried adding this line to my .vimrc file:
autocmd FileType ruby <Plug>SimpleFold_Foldsearch
But, I just get an error now when I open a ruby file. Can anybody help me setup the autocmd so that it works?

:autocmd is executing Ex mode commands, so your code is wrong. You should be using :normal or feedkeys():
autocmd FileType ruby :execute "normal \<Plug>SimpleFold_Foldsearch"

Related

How do I use the same keymap for different commands depending on the file type?

I have the following config, but when I enter an eruby file and then go back to a different file, F3 still executes :Autoformat.
noremap <F3> :Neoformat<CR>
autocmd FileType eruby bufdo map <F3> :Autoformat<CR>
I want it to only apply that command while in eruby buffers.
First, don’t use bufdo here; it executes a command for all buffers. Second, prefer <buffer> mappings.
With autocommands:
augroup vimrc_eruby
au!
au FileType eruby noremap <buffer> <F3> :Autoformat<CR>
augroup END
But I highly encourage reading about ftplugins, and using ~/.vim/after/ftplugin/eruby.vim. Read about setlocal, map-<buffer>, and b:undo_ftplugin in vim’s help.
I’ve written answers about using these tools on Vi & Vim StackExchange a few times: https://vi.stackexchange.com/a/22256/10604, https://vi.stackexchange.com/a/15329/10604, https://vi.stackexchange.com/a/15019/10604

How to use different .vimrc than what is the default .vimrc

I share lab server with my colleagues, but want my separate .vimrc file. How to get that?
$ cat .vimrc
color desert
$ pwd
/fvs101/home
We have our separate working directory, so my directory is inside
/fvs101/home/sp
At /fvs101/home there is common .vimrc file.
I want to play with vim but do not want to touch this /fvs101/home/.vimrc file.
I want to create my own .vimrc file.
++++++++++my vimrc file
[sp]$ cat .vimrc
noremap - ddp
noremap _ dd2kp
inoremap <c-u> <esc>lviwU<esc>i
nnoremap <c-u> <esc>viwU<esc>
let mapleader = "-"
let maplocalleader = "\\"
nnoremap <leader>ev :vsplit $MYVIMRC<cr>
nnoremap <leader>sv :source $MYVIMRC<cr>
iabbrev ssig -- <cr>Sachin Pawar<cr>sachin.sp.pawar#oracle.com
vnoremap <leader>' vi<esc>`<<esc>i'<esc>`><esc>i'<esc>
nnoremap H 0
nnoremap L $
inoremap jk <esc>
inoremap <esc> <nop>
augroup filetype_js
autocmd!
autocmd FileType javascript nnoremap <buffer> <localleader>c I//<esc>
augroup END
augroup filetype_python
autocmd!
autocmd FileType python nnoremap <buffer> <localleader>c I#<esc>
augroup END
augroup filetype_sql
autocmd!
autocmd FileType sql nnoremap <buffer> <localleader>c I--esc>
augroup END
augroup filetype_shell
autocmd!
autocmd FileType shell nnoremap <buffer> <localleader>c I#esc>
augroup END
augroup filetype_html
autocmd!
autocmd FileType html nnoremap <buffer> <localleader>f Vatzf
augroup END
++++++++++
If i use my separate .vimrc file i am not able to see the mapping for some mappings like
inoremap jk
Why is this happening and how to avoid it.
In order to use my own .vimrc file i have create a separate alias.
alias vi='vim -u /fvs101/home/sp/.vimrc'
Make a different .vimrc (as you have stated in the comments, /refresh/oracle/spawar/.vimrc) and make an alias for your own user
myvi='vim -Nu /refresh/oracle/spawar/.vimrc'
Note that without the -N flag, using the -u flag will start vim in compatibility mode causing all sorts of nasty, unexpected side-effects such as mappings not working entirely.
I found this out by going to see what the -u flag really does and following from there.
Helpful links below.
u flag
N flag
Seeing as you specifically want everyone to log into the same user, the easiest way I can think of how to do this is if you make your own .vimrc (give it a short name, such as ~/.vrc) and every time you open vim, you type
:so ~/.vrc
which will load that vimrc for you. Every time you open vim it will use the defaults, until you load your own vimrc, so you will have to do this each and every time. Also remember that this way, ~/.vimrc loads automatically before you manually load ~/.vrc
You can call Vim with a different runtime configuration file than the default one with
vim -N -u path/to/your/vimrc
If you want to use this always, edit your users shell runtime configuration to define an alias for that.
alias vim="vim -N -u path/to/your/vimrc"
I think the simplest solution would be to append a single line to the end of the default vimrc which reads:
source ~/.vimrc.local " Or whatever you want to call it
Where .vimrc.local contains your custom configuration.

How can I show winmanager automatically?

I’ve installed the winmanager, NERDTree and BufExplorer plugins. Now, I have <F8> set to toggle the winmanager display, using the following code in my .vimrc:
" mapping for triggering winmanager plugin
nnoremap <silent> <F8> :if IsWinManagerVisible() <BAR>WMToggle<CR><BAR> else<BAR> WMToggle<CR>:q<CR> endif <CR><CR>
This works fine.
What I want to do is to have winmanager show up automatically if the filetype is .c or .cpp. I added this to my .vimrc:
autocmd FileType c,cpp nested "\<F8>"
but it does not work.
Any help? Thanks in advance!
<F8> is a normal-mode mapping, but :autocmd expects an Ex command on its right-hand side. You need to use :normal (without a ! here, to allow mappings to take effect), and :execute to interpret the special key code:
:autocmd FileType c,cpp nested execute "normal \<F8>"
But I think it is cleaner to avoid the additional redirection and duplicate the mapping's commands instead:
:autocmd FileType c,cpp nested if IsWinManagerVisible() |exe 'WMToggle'| else| exe 'WMToggle' | quit | endif

Is there a better way to map command call based on filetype [duplicate]

This question already has answers here:
Vim inoremap for specific filetypes
(5 answers)
Closed 8 years ago.
I'm new to vim after switching from Notepad and Notepad++, but I've been using it exclusively for the past four months. In my .vimrc I have a command that automatically changes the command call based on file extensions. For example, if I'm editing an R file, I can press <F5> and vim executes !Rscript %:p<cr>, but if I switch to a python file and press <F5>, vim executes !python %:p<cr>. I accomplish this by putting the following in my .vimrc:
autocmd BufRead *.R noremap <F5> :!Rscript %:p<cr>
autocmd BufRead *.pl noremap <F5> :!perl %:p<cr>
autocmd BufRead *.py noremap <F5> :!python %:p<cr>
I'm wondering if this is the "proper" vim way to execute it based on the command call. I know some of python files I work with do not have a *.py extension, and so the setting is useless in this case.
You need to do 2 things:
Create a mapping local to a specific buffer by using the <buffer> option for noremap.
Load the mappings for just a specific filetype.
This can be done via an autocmd and FileType event in your .vimrc like so:
autocmd FileType perl noremap <buffer> <F5> :!perl %:p<cr>
The other way option is by creating a filetype plugin. (see :h ftplugin for more details)
A simple example is do create a file named, ~/.vim/ftplugin/perl.vim and place your mappings inside like so:
nnoremap <buffer> <F5> :!perl %:p<cr>
I personally lean more towards the ftplugin approach but having a everything in your .vimrc file can be nice.
For more help see:
:h :au
:h FileType
:h map-local
:h ftplugin
if you have BufRead *.py, the autocommand was trigger only when it is loading a *.py buffer/file.
vim has filetype detection mechanism.
Vim can detect the type of file that is edited. This is done by
checking the file name and sometimes by inspecting the contents of the
file for specific text.
you should switch :filetype on
and use FileType event in your autocmd.

Can you have file type-specific key bindings in Vim?

In my .vimrc file, I have a key binding for commenting out that inserts double slashes (//) at the start of a line:
" the mappings below are for commenting blocks of text
:map <C-G> :s/^/\/\//<Esc><Esc>
:map <C-T> :s/\/\/// <Esc><Esc>
However, when I’m editing Python scripts, I want to change that to a # sign for comments
I have a Python.vim file in my .vim/ftdetect folder that also has settings for tab widths, etc.
What is the code to override the keybindings if possible, so that I have Python use:
" the mappings below are for commenting blocks of text
:map <C-G> :s/^/#/<Esc><Esc>
:map <C-T> :s/#/ <Esc><Esc>
You can use :map <buffer> ... to make a local mapping just for the active buffer. This requires that your Vim was compiled with +localmap.
So you can do something like
autocmd FileType python map <buffer> <C-G> ...
The ftdetect folder is for scripts of filetype detection. Filetype plugins must be inside the ftplugin folder. The filetype must be included in the file name in one of the following three forms:
.../ftplugin/<filetype>.vim
.../ftplugin/<filetype>_foo.vim
.../ftplugin/<filetype>/foo.vim
For instance, you can map comments for the cpp filetype putting the following inside the .../ftplugin/cpp_mine.vim:
:map <buffer> <C-G> :s/^/\/\//<Esc><Esc>
:map <buffer> <C-T> :s/\/\/// <Esc><Esc>
I prefer to have my configuration in a single file so I use the autocmd approach.
augroup pscbindings
autocmd! pscbindings
autocmd Filetype purescript nmap <buffer> <silent> K :Ptype<CR>
autocmd Filetype purescript nmap <buffer> <silent> <leader>pr :Prebuild!<CR>
augroup end
Vim doesn't clear set autocmds when you source your vimrc, so starting vim, changing something in your vimrc and running :so ~/.vimrc would define autocmds twice. That's why the bindings are grouped and cleared with autocmd! group_name. You can read more here.
Since mappings are applied to every buffer by default, and you want to change them for buffers matching the filetype only, the <buffer> modifier is in there, limiting the mappings to the local buffer.
Btw... if your primary problem is about commenting... you should check out 'nerdcommenter' plugin, its the fastest way to comment/uncomment your code in java/c/c++/python/dos_batch_file/etc etc.
This is only a partial answer for people coming here having difficulties getting any ftplugin scripts working, but remember that your .vimrc (or a file that it sources) should contain
filetype plugin on
or
:filetype plugin on
for filetype-plugins to be executed when a file of a given type is loaded.
I recommend the .../ftplugin/<filetype>.vim approach that freitass suggests, but in your specific case Vim Commentary will solve all of this for you.

Resources