Vim autocmd does not map properly a mapping - vim

I want to create a plugin which maps a certain localleader mapping to a function call.
I have a ftplugin/javascript.vim file with the following content:
augroup javascript_pluginName
au!
echom "The plugin is loaded for JS"
au FileType javascript nnoremap <buffer> <localleader>j :call pluginName#pluginName#funName()<cr>
augroup END
When I open a javascript file, the message is printed, but then checking the mapped combinations shows that there is no such mapping.
I understand, that nnoremap is simply not going to map on top of other mappings. However, if I only leave the nnoremap command, without putting it in an autocmd, it would map properly.
Simply executing the call in command mode also works fine.
I looked into the vim help to see how to use autocmd, and I can't see any difference between the way I use it and what is explained there. The "Learn Vim the Hard Way" book also didn't help.
Is there something I'm missing? Should I frame the autocmd somehow differently?

I finally found out what the problem is!
Apparently, according to this article: https://vimways.org/2018/from-vimrc-to-vim/ if you put a filetype specific code into a filetype plugin, there is no need to create an autocmd for it.
The boilerplate is all made redundant by the general behaviour of vim setting the filetype on open and then running the ftplugin scripts, which are relevant for the file.
This means that in my ftplugin/javascript.vim file I only need the mapping:
nnoremap <buffer> <localleader>j :call pluginName#pluginName#funName()<cr>

Related

How to create a if estatement inside a function to check file type in vimscript?

I need to check the file type I have opened in Vim to do one thing or another inside a function.
This is what my vimrc looks like:
function! MyFunction()
autocmd filetype typescript <C-V>%
autocmd filetype go <C-V>%
endfunction
nnoremap <Leadar>f :call MyFunction()<CR>
Next to <C-V>% will be more instructions, but for now this is what I'm testing.
My function has to detect the file I have openend, according to its type do one thing or another. I'm doing this inside a function because in the near future I will move this to a separete plugin, but for now it is my vimrc file.
Another thing I've tried already and I know it works is this
autocmd filetype typescript nnoremap <Leader>f <C-V>% DoTypescriptThings
autocmd filetype go nnoremap <Leader>f <C-V>% DoGolangThings
If I move this lines outside of a function body I works. But this way I couldn't change the <Leader> KEY easily if I make this a plugin. That's why I moved it to a function.
How can I make my function detect my types so my function works?
If I can understand clearly, you actually want to pull out the filetype checking from auto-command so that remapping the main key (<LEADER>f) becomes easy.
You can do this by using &filetype.
Your function will look something like this:
function! MyFunction()
if &filetype ==# 'typescript'
autocmd filetype typescript <C-V>%
" Now you dont need autocmds here to be precise;
" But you may consider some other autocmd-events in future
" Until then a simple :execute or :execute "normal!..." should be sufficient instead
elseif &filetype ==# 'go'
autocmd filetype go <C-V>%
endif
endfunction
nnoremap <Leadar>f :call MyFunction()<CR>
Now considering the fact that autocmd also does the same thing (checking filetype and applying a mapping) so I guess your main motivation is not to use autocmd but to be able to remap main key (<LEADER>f) easily.
So in conclusion, I would suggest to not use autocmd here and go with a function definition only so that one key can rule your function call. (Of course, unless you decide to use some other autocmd-events too)

Modify vim-fugitive Gstatus mapping

The :Gstatus window has specific mappings for that particular buffer. In my case, I would like to change the cc mapping to not only execute :Gcommit but also go into insert mode afterwards.
It seems like the user robodendron figured out how to do this as shown in https://github.com/tpope/vim-fugitive/issues/647, but I'm don't know what he means when he says "changing the order should be enough." Also, I would ask this on the Git issues page, but it seems like the user NicolasWebDev already tried that and no one got back to him.
I can add mappings by creating a after/ftplugin/gitcommit.vim file, but modifying an existing mapping seems to be more difficult since the mapping is defined after the filetype is set.
Also, I could modify the source code mappings, but we all know that's more of a temporary fix.
I am not sure about trying to alter :Gstatus mappings, but you can start insert mode when entering the commit buffer.
Add the following to your vimrc file:
augroup turbo_commit
autocmd!
autocmd BufEnter COMMIT_EDITMSG startinsert
augroup END
For more help see:
:h :autocmd
:h :augroup
:h BufEnter
:h startinsert

Mapping the same key for Compiling/Running code of different languages from vim

I have map <F8> : w <bar> !clang -o %< % && ./%< <CR> inside my .vimrc, however I want to map F8 to run python codes as well. How is that possible? (obviously I don't want clang to run python, I want to have a condition or something to redirect what command F8 maps to based on the language that is already specified e.g. via :setf python etc.)
You're looking for filetype-specific mappings. Put the Python variant into ~/.vim/after/ftplugin/python.vim, and add the <buffer> keyword to the :map command:
nnoremap <buffer> <F8> : w <bar> !python % <CR>
This requires :filetype plugin on, but you probably already have that. Similarly, you can move your original mapping to ftplugin/c.vim, or keep that as a global fallback for all filetypes.
Additional tips
You should use :noremap; it makes the mapping immune to remapping and recursion.
Better specify the concrete modes this applies to, in this case normal mode via :n[nore]map.
Very doable. Here's an example where I have the same key mapped for tidying different types of file:
autocmd FileType perl nnoremap <buffer> <F12> mz:%!perltidy<CR>`z
autocmd FileType javascript nnoremap <buffer> <F12> :call JsBeautify()<CR>
It's been a while since I added this to my vimrc, but if memory and a quick google serves me well, this watched for the event of the fileype being perl or javascript, and then runs the nnoremap command for whichever event has occurred. I'm sure there are many other ways to accomplish it!

Can I make a Vim mapping for a single file?

Basically I would like to be able to quickly look up Vim's help pages for specific commands. To do that, I am using this mapping:
nnoremap K :help <C-r><C-w><CR>
However, I would like to apply this mapping only to my .vimrc (in order to be able to use different lookup documentations for different files).
Is that possible?
K already does keyword lookup but the associated 'keywordprg' option is rarely defined.
Add the snippet below to your ~/.vimrc to get the desired behavior:
augroup vim
autocmd!
autocmd FileType vim setlocal keywordprg=:help
augroup END
Thanks for reminding me to set it up correctly in my own config.

Get vim to provide tab completion for CSS class and ID names

The one IDE feature that I always missed and invariably plug into vim is tab completion.
I'm a big fan of SuperTab, but one thing I can't stand is the fact that it treats the parts of CSS class names and IDs with dashes as individual words.
I've found a couple of possible solutions for camelCase and underscore_completion but I can't seem to find anything that supports plain-old-dashes.
This is not a CSS-specific problem: Vim uses the value of iskeyword to perform completion.
Type :set iskeyword? to see what characters are considered to be part of keywords. The default on a Mac is supposed to be #,48-57,_,192-255.
You can add the dash to the list with this command:
:set iskeyword+=-
Add this line to your ~/.vimrc to make this setting stick:
set iskeyword+=-
This seems to work for me:
autocmd FileType css,scss set iskeyword=#,48-57,_,-,?,!,192-255
Taken from here: VIM: How to autocomplete in a CSS file with tag ids and class names declared in HTML file
For future readers: if you want the benefits of dashes for edit/movement commands, but want full property autocompletion, try adding this to your .vimrc:
augroup css_dash_autocompletion
autocmd FileType scss,css autocmd! css_dash_autocompletion InsertEnter <buffer> set isk+=-
autocmd FileType scss,css autocmd css_dash_autocompletion InsertLeave <buffer> set isk-=-
augroup END
The first ! prevents duplicate event firing. Thanks to ZyX for the structure. If you re-source your .vimrc, you will need to :e any (S)CSS files you have open to pick up the change.

Resources