VIM mapping an internal command - vim

I can't figure out how to map an internal command in vim.
I want to map to the command :Indent, the action g=GG (indenting the whole document)
I did this :
:command Indent execute "g=GG"
And it doesn't seem to work.
I successfully mapped
:command Java execute ":!javac *.java; echo ' **** done **** ' "
but how do i make it compile only the file that i am working on.

gg=G is a normal mode command. You need to use :normal, here:
:command! Indent normal! gg=G
But… :Indent<CR> is much longer than gg=G so I'm not sure that's a good idea.

I don't understand the need either. Still, if you want a command, at least let's have it support ranges:
:command! -range=% -nargs=0 Indent <line1>,<line2>normal! ==

Related

Create Command to Fix Indentation

I am familiar with the = filter. == key sequence indents the current line, while gg=G indents the entire file. I am trying to create a command:FixIndentation to get a similar effect, to fix indentation in the entire file.
# vim documentation provides example to map to key
map <F7> gg=G<C-o><C-o>
# my attempt to map to a command fails
command ReIndent execute "gg=G"
:ReIndent
E492: Not an editor command: gg=G
add normal! to tell it to execute it in normal mode:
command ReIndent execute "normal! gg=G"

vi, condition in vimrc

I am trying to personalize my ~/.vimrc file.
Here is what I want: when the file name opened with vi is titi45.tex, and when I press <space> in normal mode, I want that the command make toto45
is executed. And if the file opened with vi is called titi65.tex, I want that
make toto65 is executed, and so on.
I tried to use a command
au FileType tex nmap <space> :w<CR>:!make<CR><CR>
in my .vimrc but I don't know how to match the file name and use the number.
Can you help me?
Mathieu
You are looking for :make %<. BTW why don't you compile within vim? Avoid :!make. Prefer :make, and check the help related to the quickfix mode (:h quickfix).
Your mapping would then be:
nnoremap <buffer> <silent> <space> :update<cr>:make %<<cr>
(the <buffer> part is very important, it makes sure your mapping won't leak to other filetypes; the other are not critical here, but good practices)
EDIT: Sorry I missed the exact requirement.
Then, you'll have to transform the target name. You'll to play with substitute() and :exe. But your substitution won't be a simple substitution. It looks like a rotating substitution. Solutions for this kind of substitution have been described over there: How do I substitute from a list of strings in VIM?
And IIIRC, there exist a plugin that does similar things.
In your case, I guess I would use a dictionary to define how names are substituted:
let k_subs = { 'toto': 'titi', 'titi': 'toto' }
nnoremap <buffer> <silent> <space> :update<cr>:exe 'make '.substitute(expand('%'), join(keys(k_subs), '\|'), '\=k_subs[submatch(0)]', '')cr>
NB: I haven't tested it.
If you want to get rid of the extension, it'd better be done in expand() argument.
Hum...
finally i use an additionnal script
#!/bin/bash
maRegex='source_(enonce|corrige)([0-9]+)$'
if [[ "${1}" =~ $maRegex ]]
then
commande="make enonce${BASH_REMATCH[2]}"
else
commande="make plouf"
fi
echo commande de compilation lancée: $commande
$commande
This script in launched by vimrc.

Calling "set" command within vim custom command

I'm trying to write a custom command in Vim to make setting the makeprg variable for an out-of-source build easier. Having read the command manual, so far I've got as far as this
command! -complete=file -nargs=1 Cmakeprg call set makeprg=cmake --build <args><CR>
but it isn't working. How do I call "set" within the command?
You :call functions, :set is an Ex command just like :call (as it's invoked with the : prefix).
A complication with :set is that whitespace must be escaped with \, but that can be avoided by using :let with the &option, and <q-args> automatically quotes the passed command arguments.
You also don't need <CR>; this isn't a mapping. Taken all together:
command! -complete=file -nargs=1 Cmakeprg let &makeprg = 'cmake --build ' . <q-args>
Add a colon in front of "set" and use a <CR> to execute it: :set … <CR>
Do not use call.

vim: map command with confirmation to key

I've written a few macros in my .vimrc for the version control system I'm using (Perforce) (please don't suggest the perforce plugin for vim, I tried it and I don't like it). They all work fine except the revert macro, which breaks due to a confirmation prompt (which I need so I don't accidentally fat-finger my changes away). It currently looks like this:
map <F8> :if confirm('Revert to original?', "&Yes\n&No", 1)==1 | !p4 revert <C-R>=expand("%:p")<CR><CR><CR>:edit<CR> | endif
This causes bash to complain when vim tries to load the file:
bin/bash: -c: line 0: syntax error near unexpected token `('
Looking at the buffer bash sees, it looks like the error is that vim sends it everything after the first pipe, not just the part meant for bash. I tried a few alternatives but I can't seem to make it work. I've got it to show confirm dialog correctly when I removed the pipes and endif (using shorthand if), but then vim complains after the user gives a response.
I think you want something along these lines:
:map <F8> :if confirm('Revert to original?', "&Yes\n&No", 1)==1 <Bar> exe "!p4 revert" . expand("%:p") <Bar> edit <Bar> endif<CR><CR>
Remember that :map is a dumb sequence of keystrokes: what you're mapping F8 to has to be a sequence of keystrokes that would work if typed. A <CR> in the middle of the :if statement doesn't mean ‘and press Enter when executing the command at this point if the condition is true’; it means ‘press Enter here when in the middle of typing in the :if command’, which obviously isn't what you want.
Building it up a piece at time, from the inside out:
There's a shell command you sometimes want to run.
That shell command needs to be inside an :if to do the ‘sometimes’ bit, and so have an :endif following it.
After a literal ! everything following is passed to the shell, including | characters which normally signify the start of another Vim command. That's reasonable, because | is a perfectly good character to use in shell commands. So we need some way of containing the shell command. :exe can do this; it executes the supplied string as a command — and its argument, being a string, has a defined end. So the general form is :if condition | exe "!shell command" | endif.
Your shell command has an expression in it. Using :exe makes this easy, since you can simply concatenate the string constant parts of the command with the result of the expression. So the command becomes :exe "!p4 revert" . expand("%:p") — try that out on its own on a file, and check it does what you want before going any further.
Putting that inside the condition gives you :if confirm('Revert to original?', "&Yes\n&No", 1)==1 | exe "!p4 revert" . expand("%:p") | edit | endif — again try that out before defining the mapping.
Once you have that working, define the mapping. A literal | does end a mapping and signify the start of the next Vim command. In your original the mapping definition only went to the end of the condition (check it with :map <F8> after loading a file) and the !p4 part was being run immediately, on the Vim file that defines the mapping! You need to change each | in your command into <Bar>, similarly to how each press of Enter in your command needs writing as <CR>. That gives you the mapping above. Try it by typing it at the command line first, then do :map <F8> again to check it's what you think it is. And only then try pressing F8.
If that works, put the mapping in your .vimrc.
Use of the pipe to string multiple vim commands together is not particularly well-defined, and there are numerous eccentricities. Critically, (see :help :bar) it can't be used after a command like the shell command :! which sees a | character as its argument.
You might find it easier to use the system() function.
E.G.
:echo system("p4 revert " . shellescape(expand("%:p")))
The shellescape() wrapper is useful in case you have characters like spaces or quotes in the filename (or have cleverly named it ; rm -rf ~ (Don't try this at home!)).
In the interest of creating more readable/maintainable code, you may want to move your code into a function:
function Revert()
if confirm('Revert to original?', "&Yes\n&No", 1)==1
return system("p4 revert " . shellescape(expand("%:p")))
endif
endfunction
which you would access by using the :call or :echo command in your macro.

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

Resources