Can I make a Vim mapping for a single file? - vim

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.

Related

Vim autocmd does not map properly a mapping

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>

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

With snipMate, how do I add specific snippets for mySpecial.html file only?

In my vim environment, I want to add a few specific snippets that should only be active when I am editing mySpecial.html file.
Since this file is an html file I want it to have the html filetype. But on the other hand, if I edit any other html file, I don't want the specific snippets to be active. So, it seems I cannot use the standard filetype mechanism for this.
Is there any solution to my problem?
For the original (Michael Sanders) snipMate, I've defined the following command:
:command! -bar -nargs=1 -complete=filetype AddSnippets silent call ExtractSnipsFile(g:snippets_dir . <q-args> . '.snippets', &l:filetype)
With that, you can interactively or through an autocmd add the snippets:
:autocmd BufRead,BufNewFile mySpecial.html AddSnippets mySpecial
This sounds like an interesting feature request.
In the mean time, you can try this simpler variant of Carlo's solution:
Put your custom snippets in ~/.vim/snippets/foo.snippets.
Add the following line to your ~/.vimrc:
augroup mySpecialHTML
autocmd!
autocmd BufRead,BufNewFile mySpecial.html set filetype=html.foo
augroup END
I think you can define a new filetype just for your mySpecial.html
au BufRead,BufNewFile mySpcial.html set filetype=myHtml
And in that filetype you can reuse the html highlights group, plus your custom snippets.
Does this sound reasonable?

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.

Define mapping in insert mode that works only with a certain filetype

I should know this, but my mind is blank right now ... long night ...
How to define a mapping that is active only when a certain filetype is active (say, markdown and I wish to define a mapping/abbrev for inserting brackets ][ since I don't have them on my keyboard in my language layout)?
To define a mapping effective only in the buffers having certain filetype
value, one can use the combination of a filetype autocommand (see :help
autocmd, :help FileType) and a local mapping (see :help map-local).
:autocmd FileType markdown inoremap <buffer> (( [
:autocmd FileType markdown inoremap <buffer> )) ]
The first, mandatory, step is indeed to define buffer local mappings with :h :map-<buffer> as ib said.
Then you have the choice. You can :
either clutter your .vimrc with autocommands
or use ftplugins, which is the solution that scales, that can be easily redistributed, and so on.
The first approach is fine when we only use vim for a couple of languages/filetypes and when we have very few mappings/commands/abbreviations/... It's fine the first couple of years in Vim. Past a certain amount of ft-specific settings, the second solution is the one to be preferred.
(BTW, this subject is a duplicate, but I too lazy right now to search for the other posts)
Add where appropriate (e.g. ~/.vimrc):
au FileType markdown inoremap <C-b> [

Resources