How to do Vim mapping on help page only - vim

I would like to go-to a tag when pressing enter (instead of clicking with the mouse when mouse=a) or pressing ctrl-], which is a bit of a stretch for me. Is it possible to do the mapping in the help section only, such as:
:nnoremap *help* <CR> <C-]>

I do exactly that in an ftplugin for :help files: put your mapping in ~/.vim/after/ftplugin/help.vim and use <buffer>:
nnoremap <buffer> <CR> <C-]>

Create a buffer-local mapping when a help file is loaded:
:autocmd BufReadPost $VIMRUNTIME/doc/help*.txt nnoremap <buffer> <CR> <C-]>

If the "help page" you meant is vim doc/help, they have ft=help. You can verify it by :echo &ft or :set ft?.
Then it is easy if you want to create mapping only for those filetypes. You can do it using autocommand:
autocmd FileType help map....
If you want it to be buffer local mapping, you add <buffer> in mapping.

Related

Specific key mapping in Vim

How can I configure Vim to set
"require 'pry'; binding.pry"
in Ruby and
"debugger;"
in JavaScript when pressing F2 via key mapping?
You can set this in your .vimrc as follows:
autocmd FileType ruby map <F2> orequire 'pry'; binding.pry<ESC>
autocmd FileType javascript map <F2> odebugger;<ESC>
When the F2 key is pressed in a *.rb file, "require pry" will be set and "debugger" is set in a *.js file.
The other answer is correct, but not completely correct. You should use the noremap variant of map (see :h noremap), and the proper noremap for whatever mode your are in. If that's insert mode, then it's inoremap <F2> require..., or nnoremap for normal mode, etc.
You can also put those mappings into their own file instead of your vimrc so that you don't need to use autocommands (see :h ftplugin). And (thanks to the comments for reminding me) use <buffer> mappings so they only apply to the file you set them on (see :h <buffer>). In all, this is a good setup for you:
In ~/vim/after/ftplugin/ruby.vim, put the line:
inoremap <buffer> <F2> require 'pry'; binding.pry
and in ~/vim/after/ftplugin/javascript.vim, put the line:
inoremap <buffer> <F2> defbugger;
On windows, the vim directory is instead the vimfiles directory. If you want those mappings in normal mode instead of insert mode, you need to put i or O or another character like that at the front to go into insert mode and put <Esc> on the end to exit insert mode.

How to change nnoremap for different file types?

When I hit <leader>r inside a PHP file, I want to do:
nnoremap <leader>r ifile_put_contents(ini_get('error_log'), '');<cr>error_log(print_r(, true));<esc>
But when I do the <leader>r inside a JS file, I want to do:
nnoremap <leader>r iconsole.log();<esc>
How could I do this?
I also want to position the cursor inside the console.log/print_r. Any idea how to do that as well?
You can use autocomand and map to the buffer only. Here's an example:
augroup quick_debug
autocmd!
autocmd FileType php nnoremap <buffer> <leader>r ifile_put_contents(ini_get('error_log'), '');<cr>error_log(print_r(, true));<esc>
autocmd FileType javascript nnoremap <buffer> <leader>r iconsole.log();<esc>
augroup END
To move cursor around you can use <left> and <right> before <esc>. Like this: nnoremap <buffer> <leader>r iconsole.log();<left><left><esc>.
If you don't mind using plugins you could take a look at the UltiSnips and use snippets instead of mappings.
You can define that for certain filetypes by prepending :autocmd Filetype {filetype} ..., and put that into your ~/.vimrc. But that gets unwieldy as you add mappings and other settings for various filetypes. Better put the commands into ~/.vim/ftplugin/{filetype}_mappings.vim. (This requires that you have :filetype plugin on.)

How to map tab to regular space only for fortran files on vim

I've switched from Emacs to Vim and when I edit fortran (.f) files, I'm used to hit tab on a new line. On my machine the indentation is fine, but on other machines, the indentation is off, how can I tell vim to map the tab key to a single space only on fortran files?
You need to do 2 things:
create a mapping local to a specific buffer by using the <buffer> option for inoremap.
load the mappings for just a specific filetype.
This can be done via an autocommand in your ~/.vimrc like so:
augroup FortranMappings
autocmd!
autocmd FileType fortran inoremap <buffer> <tab> <space>
augroup END
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/after/ftplugin/fortran.vim and place your mappings inside like so:
inoremap <buffer> <tab> <space>
I personally lean more towards the ftplugin approach but having a everything in your ~/.vimrc file can be nice.
Conclusion
I personally think this is an issue with your indention settings and working around it with a mapping more of a bandaid approach. There is a nice Vimcasts episodes that might be helpful: Tabs and Spaces and Whitespace preferences and filetypes. Check your indention settings with the following:
:verbose set ts? sts? sw? et?
For more help see:
:h :map-local
:h ftplugin
:h :aug
:h :au
:h FileType
:h :set
:h 'ts'
:h 'sts'
:h 'sw'
:h 'et'
To map the tab key to a single space only on fortran files (*.f, *.f90, *.F or *.F90), add the following in your ~/.vimrc
function! FortranTab()
inoremap <Tab> <Space>
endfunction
au BufNewFile,BufRead *.f,*.f90,*.F,*.F90 call FortranTab()

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

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