VIM: why do these bindings work only sometimes? - vim

I've got these bindings in my .vimrc. They work most of the time, but sometimes they don't: they will just save the file but not run it.
Then I go into insert mode and exit it back to normal mode, and they work again. What could be the problem?
Thanks!
autocmd FileType python map <C-k> :write <CR> :! python % <CR>
autocmd FileType lisp map <C-k> :write <CR> :! clisp % <CR>
autocmd FileType scala map <C-k> :write <CR> :! scala % <CR>

There are many topics to discuss here so with out further ado:
Mappings
Your current mappings map <c-k> ... will only work in normal, visual, and operator-pending modes. However executing your mappings in visual mode or operator-pending modes would save the buffer with only a range of lines (read not good). I suggest you make your mappings for normal mode only.
Two general rules of thumb:
Always supply a mode like n for normal.
Always use noremp instead of map unless you are mapping to a <Plug> mapping.
So one of your mappings might look something similar to this:
nnoremap <c-k> :w<cr>:!python %<cr>
For more information:
:h :map-modes
:h map-overview
:h :nore
:h map-listing
:h map-verbose
Filetype based mappings
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 python nnoremap <buffer> <c-k> :w<cr>:!python %: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/python.vim and place your mappings inside like so:
nnoremap <buffer> <c-k> :w<cr>:!python %: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
:make
A more vim like way of doing this would be to use :make. (Assuming you want to lint vs execute your current buffer)
:make will execute the 'makeprg'. It defaults to make which is great of C projects
After running :make the quickfix list will be contain any errors.
Set your compiler via the :compiler command.
Extra parameter can be passed like so :make foo-command
Current filename can be represented by %. e.g. :make %
Often people set do :complier/'makeprg' in side of ftplugins e.g. ~/.vim/ftplugin/perl.vim or autocmd's e.g. autocmd FileType perl compiler perl.
Fop more help see:
:h :make
:h 'makeprg'
:h :compiler
:h c_%
quickfix list
Use :cnext and :cprev to move between your errors.
:copen to open up the quickfix list in a window (:cclose to close)
:cwindow to open quickfix list window only if there are errors
May want to use better mappings for :cnext and friends. I suggest Tim Pope's unimpaired plugin
For more help see the following:
:h quickfix
:h :cnext
:h :cope
Alternatives to using :make
Just use <c-z> to suspend vim and run your build system. (Cons: loose out on the quickfix list)
Use :! to compile. (Same cons as suspending) e.g. :!make
Syntastic is a syntax checking system that checks files on save
Ale (Asynchronous Lint Engine) is a plugin for providing linting in NeoVim and Vim 8 while you edit your text files
Dispatch can be used to run things in the background. Great for test suites
May want to consider terminal multiplexers like tmux or screen.
SingleComplile tries and takes some of the work out of using :make
Conclusion
Personally I would install ALE as this removes the need for your mappings. It is also a great idea to learn how to use the location (or quickfix) list. When you are ready to get your hands dirty then you can learn and use :make.
tl;dr
Install ALE.

The space inserted between <CR> and : could lead to errors, because this space is really entered during the execution of the mapping.
With :noremap or :nnoremap (which should be used most of the times, as Peter Rincker wrote in his answer), it wouldn't be a big problem, since it only would move the cursor one char to the right (see :h <space>).
But with :map, it could trigger a custom mapping (or a partial one).
Then, instead of:
autocmd FileType python map <C-k> :write <CR> :! python % <CR>
the following would be better:
autocmd FileType python map <C-k> :write <CR>:! python % <CR>
and this one, really better:
autocmd FileType python nnoremap <C-k> :write <CR>:! python % <CR>

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

Vimrc autocmd runs command instead of mapping

I want to add a hotkey to my vim to run my python files, so I have added the following line to my vimrc:
autocmd FileType python map <F5> :w|!python3 %
But on opening the file instead of running autocmd, it runs python with my file.
For me this seems no different from what many tutorials suggest on using autocmd and map, so I'd like to know what exactly is happening here instead of the behavior I expect.
Your :map command ends at the command separator |; the remainder is already executed when the :autocmd runs. You need to escape it (\|), or better use the special <Bar> character:
autocmd FileType python nnoremap <buffer> <F5> :w<Bar>!python3 %<CR>
Cp. :help map_bar. Also, you should use :noremap; it makes the mapping immune to remapping and recursion. And (as #PeterRincker suggested), make the mapping buffer-local, so that it only applies to Python buffers).
Your :autocmd FileType approach is fine for a few mappings, but it doesn't scale well. If you have :filetype plugin on in your ~/.vimrc, you can factor our your mappings into a separate script ~/.vim/ftplugin/python_mappings.vim.

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()

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