Autosave buffer on :make in vim? - vim

Is there any way to autosave the buffer before issuing :make? I use MacVim and make is bound to Command-B, which is very helpful but I cannot seem to figure out how to write the buffer before a make. I looked at all the autocmd events and nothing seemed to fit.
There's a QuickFixCmdPre which should be called before a make but can't seem to get it to work:
~/.vimrc
function! AutoSaveOnMake ()
if &modified
write
endif
endfunction
autocmd QuickFixCmdPre *.c :call AutoSaveOnMake()

Vim has a built-in setting for that:
:set autowrite
Write the contents of the file, if it has been modified, on each
:next, :rewind, :last, :first, :previous, :stop, :suspend, :tag, :!,
:make, CTRL-] and CTRL-^ command; and when a :buffer, CTRL-O, CTRL-I,
'{A-Z0-9}, or `{A-Z0-9} command takes one to another file.

Instead of creating an autocmd command, why not just update the Command-B mapping?
nnoremap <d-b> :update<bar>make<cr>

Related

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.

VIM: why do these bindings work only sometimes?

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>

Function to source .vimrc and .gvimrc

I generally use GVim, but most of my configuration is done via .vimrc (like keymappings) because I want them in vim and gvim. So when I edit my vimrc and then source it from gvim, I have to source my .gvimrc after that in order to get my colorscheme back (since it's gvim only). I tried to write a function to do this, and ran into the problems described in the comments below:
function ReloadConfigs()
:source ~/.vimrc
if has("gui_running")
:source ~/.gvimrc
endif
endfunction
command! Recfg call ReloadConfigs()
" error: function already exists, add ! to replace it
function! ReloadConfigs()
:source ~/.vimrc
if has("gui_running")
:source ~/.gvimrc
endif
endfunction
command! Recfg call ReloadConfigs()
" error: cannot replace function, it is in use
Is it possible to do something like this? Or, since my .gvimrc only has a few lines, should I just put its contents into an if has("gui_running") block?
You've put your function somewhere in your .vimrc. This means that, while it's being executed, the :source .vimrc is trying to redefine it, which is a problem. You could try doing this:
if !exists("*ReloadConfigs")
function ReloadConfigs()
:source ~/.vimrc
if has("gui_running")
:source ~/.gvimrc
endif
endfunction
command! Recfg call ReloadConfigs()
endif
If the function is already defined, this should skip redefining it, avoiding the issue.
I would say that whatever you have in your .vimrc that's messing up gvim settings should be surrounded by an if !has("gui_running") block.
An autocmd seems to be the easiest way of handling what you're trying to do:
autocmd BufWritePre .gvimrc,.vimrc source <amatch>
This way you get your configuration file automatically reloaded when you save it without having to mess around with functions. Alternatively, you could use a mapping to trigger :source $MYVIMRC or :source $MYGVIMRC.

Can I use cppcheck when I execute :wq in Vim editor for c/c++

I want to override wq/q/w!/w/q! to user defined command along with its functionality.
Example :
If I use :wq to exit, the command should do static code check of that particular c/c++ file and exit.
Please help me in this case.
Thanks in advance.
The built in solution to your problem is called an "autocommand" in Vim.
It is a way to invoke a command at a specific time like opening, saving or closing a buffer.
See :help autocmd for the full list
In your case, you should add to your .vimrc the following command
autocmd BufWritePre *.cpp,*.hpp !cppcheck %
BufWritePre means 'before writing the buffer' (You can also use BufWrite or BufWritePost)
*.cpp,*.hpp means the auto command will only be applied when saving cpp or hpp files. You can add c and h files if you want.
% means 'path of the current buffer'
cppcheck must be in your path
You are not overriding the defaut behaviour of 'w' but you are using 'hooks' to add custom commands.
I wouldn't do that. It obliges us (well, you actually) to save only when the file is really compilable -- which make no sense when we have to abort the current editing because an urging meeting that we've forget about is about to begin; saving the file in that situation is the normal way to proceed.
Moreover, what is the purpose of running cppcheck on a :wq? How can we exploit the result?
Instead, I'd have a mapping that run cppcheck, though :make in order to exploit the vim quickfix feature (:h quickfix)
function s:Check()
try
let save_makeprg=&makeprg
set makeprg=cppcheck
" you may have to specify other files/extensions
:make *.cpp *.hpp
finally
let &makeprg=save_makeprg
endtry
endfunction
nnoremap <buffer> <c-f7> :call <sid>Check()<cr>
<block>
function FunForQuickfix(makeprgIn, makeefmIn)
try
let save_makeprg=&makeprg
let save_makeefm=&efm
let &makeprg=a:makeprgIn
let &efm=a:makeefmIn
:wa
:make
:cw
finally
let &makeprg=save_makeprg
let &efm=save_makeefm
endtry
endfunction
function CompileAndRunTestCodeByScons()
call FunForQuickfix('scons . -j8 -u', '%f:%l:%c:\ %m,%f:%l:\ %m,build/release64/%f:%l:%c:\ %m,build/release64/%f:%l:\ %m,%f\|%l\|,build/release64/%f:%s,%m:%l:Assertion,%sExpression:\ false,scons:\ building\ terminated\ because\ of\ errors,%sError%m')
endfunction
function CppCheck()
call FunForQuickfix("cppcheck *.cpp -j 8 --enable=all", "\[%f:%l\]:\ %m")
endfunction
nmap ma :wa<CR>:call CompileAndRunTestCodeByScons()<CR>
nmap mc :wa<CR>:call CppCheck()<CR>
nmap <F3> :cp<cr>
nmap <F4> :cn<cr>
</block>
add this in ~/.vimrc, then use ma to compile code and mc to run cppcheck
this code is copy from Luc Hermitte, and i chang it
You might want some things even better, use cppcheck and other checkes while you are developing in vim.
Install and use that vim plugin: https://github.com/scrooloose/syntastic

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