In my .vimrc, I set a shortcut command for define main function.
autocmd FileType c,cpp abbr intmain int main(){<C-M><C-M> return 0;<C-M><C-M>}
When I type intmain in my cpp file or c file, the main function is supposed to be defined as follows.
int main(){
return 0; // two-spaces indent
}
However, actual definition goes as follows.
int main(){
return 0; // two-spaces indent
} // extra two-spaces indent...
How can I fix this one?
The error likely occurs because of an interaction with your default settings for c/cpp files. When I insert your command and type intmain the result is
int main(){
return 0; // has 2x4 spaces!
}
This is because of my clang config file:
$ cat .vim/after/c.vim
set tabstop=4
set softtabstop=4
set shiftwidth=4
set noexpandtab
For optimal results, type as the autocmd exactly what you would type into an open buffer with a .c/.cpp extension.
Related
I'm trying to create a mapping for Visual Mode where I press F7 and the selected code will be wrapped with /* and / respectively.
I want / and */ to be on a line by themselves.
I have this in my vimrc:
autocmd BufNewFile,BufRead *.c,*.js xmap <F7> I <ENTER> <ESC> k I /* <ESC> gv A */ <ENTER> <ESC>
since I want this mapping to only be valid for C and js files.
It works ALMOST as I want it. The only issue is instead a of the code being on a newline after /*, there'll be an empty line after the code and before the closing */.
I don't really understand why, since executing each action manually does what I want.
The result of pressing F7 should turn this code:
for (size_t a = 0; a<5; a++) {
printf("%d\n", somearray[a]);
}
into this
/*
for (size_t a = 0; a<5; a++) {
printf("%d\n", somearray[a]);
}
*/
while as it stands, it'll end up like this for some reason:
/*for (size_t a = 0; a<5; a++) {
printf("%d\n", somearray[a]);
}
---newline---
*/
It works now.
autocmd BufNewFile,BufRead *.c,*.js xmap <F7> I<CR><ESC>kI /*<ESC> gv A*/ <CR> <ESC>
The documentation for indenting base classes and member initialization list says (:help cino-i):
iN Indent C++ base class declarations and constructor
initializations, if they start in a new line (otherwise they
are aligned at the right side of the ':').
(default 'shiftwidth').
I use i2s in cinoptions and shiftwidth=3. When I indent the following
class Foo : public Bar,
public Baz
{
};
I would like it to be
class Foo : public Bar,
public Baz
{
};
but I get
class Foo : public Bar,
public Baz
{
};
Similarly, when I indent
Foo(int x, int y) : x_(x),
y_(y)
{
}
I would like to get
Foo(int x, int y) : x_(x),
y_(y)
{
}
but I get
Foo(int x, int y) : x_(x),
y_(y)
{
}
I thought the wording otherwise they are aligned at the right side of the ':' would do exactly what I want. It seems I am misinterpreting that line.
Is there any way to get the desired indenting?
Additional Info
Platform: Windows 10
vim version: 8.1
My settings:
autoindent fileformat=dos nolangremap scrolloff=5 tabstop=3 ttymouse=xterm
background=dark filetype=cpp laststatus=2 noshelltemp textmode visualbell
cindent helplang=en mousemodel=popup shiftwidth=3 textwidth=100 wildmenu
clipboard=unnamed hlsearch nrformats=bin,hex showcmd ttimeout nowrap
display=truncate incsearch ruler showmatch ttimeoutlen=100
expandtab langnoremap scroll=24 syntax=cpp ttyfast
backspace=indent,eol,start
cinkeys=0{,0},:,o,O,0#,!<Tab>
cinoptions=>1s,e0,n0,f0,{0,}0,^0,:s,=s,ps,ts,c3,i2s,+s,(0,u0,)20,*30,gs,hs,W2s
comments=sr:/*,mb:*,el:*/,://
fileencoding=utf-8
fileencodings=ucs-bom,utf-8,default,latin1
formatoptions=crql
keymodel=startsel,stopsel
omnifunc=ccomplete#Complete
selection=exclusive
selectmode=mouse,key
statusline=%f%h%m%r [%{&ff}] (%{strftime("%H:%M %d/%m/%Y",getftime(expand("%:p")))})%=%l,%c%V %P
suffixesadd=.h,.cc,.C,.cpp,.xml,.scm
whichwrap=b,s,<,>,[,]
wildmode=list:longest,longest:full
For example --
when i do gg=G on
int main()
{
return 0;
}
it will change it to
int main()
{
return 0;
}
What I want is --
int main(){
return 0;
}
The '{' should be on the funciton prototype line
AFAIK:
= re-adjusts indent, it doesn't reformat your codes' style. e.g, the code block style (your question); or add/removing empty lines; add/remove spaces e.g. a=2 -> a = 2 ...
you could do this to change the { before/after you gg=G:
:%s/)\n\s*{\s*$/) {/g
you could also write them into one line, and make a mapping to do it in one short.
e.g, this line:
:%s/)\n\s*{\s*$/) {/g|norm! gg=G
will turn:
int main()
{
if(foo)
{
return 1;
}
if(a>0)
return a;
for(int i=1;i<20;i++)
{
int foo=0;
foo=i;
}
return 0;
}
into
int main() {
if(foo) {
return 1;
}
if(a>0)
return a;
for(int i=1;i<20;i++) {
int foo=0;
foo=i;
}
return 0;
}
EDIT
My original answer suggested :g/)$/j to "join" the two lines, but I found it is not safe, for example:
if (a>0)
return a;
will be turned into
if (a>0) return a;
which is not expected by OP.
To go along with Cubic's Answer
To use astyle without modifying file you can use the command gq and the option `formatprg'
formatprg specifies an external program that will be used to format the buffer. After the command has been run the buffer will be replaced by the output of the program.
For exmample: To set this to work with c files you can put the following in your vimdc
autocmd FileType *.c set formatprg=astyle\ --style=kr
Note: the \ allows you to pass the different command line options to style.
Now to use this in your file you can type gggqG to apply the formatting to the whole file.
You could use astyle, with something like
nnoremap <A-S-f> :w<CR>:!astyle % --style=java<CR>:edit<CR>
Which binds it to Alt-Shift-f (note that this saves/reloads the file which may not always be what you want, there are ways around that but I didn't want to go too much into this right now).
Of course, you'll have to figure out what options to pass to astyle for your preferred formatting yourself.
I use Vim in a C++ code with openmp sentences.
And in my ~/.vimrc
set ai " auto indent
my problem: when I use an openmp sentence (this begins with #) the cursor jumps to the beginning of the line without the auto indent.
Example:
int main()
{
int idx = 100;
#pragma omp parallel private(idx) // jump to begin of line
, when I like this:
int main()
{
int idx = 100;
#pragma omp parallel private(idx) // This is OK
Can I set this in the autoindent in Vim?
Vim puts a line in column 1 when it starts with # (preprocessor directives), if cinkeys contains #.
So you can remove # from cinkeys to disable this feature:
:set cinkeys-=0#
I am using a custom keymap using langmap option in vimrc.
I am trying to use snipmate but I am running into trouble. When I type a word and hit tab it allows me to edit the parameter. The problem is that the first character is the remapped one, while I want it to be the actual key.
For instance, I'll type this:
for
and hit tab to expand the snippet:
for (i = 0; i < COUNT; ++i)
The i is highlighted which means I can edit it. I type "aaa":
for (baa = 0; i < COUNT; ++i)
It comes out baa even though I typed aaa. This is because I remapped a and b.
How can I fix this?
Here is my keymapping:
set langmap=nj,N},ek,E{,il,IL,{^,}$,lb,LB,uw,UW,ye,YE,jg,JG,\\;z,f\\.,F\\,,zu,ZU,.?,\\,/,/v,? V,ta,TA,si,SI,ro,RO,ac,AC,wr,WR,xx,XX,dd,DD,bs,BS,gf,GF,pt,PT,kn,KN,cy,CY,vp,VP,o\\;
It won't make much sense to others, and I haven't finalized how I want it to look.
From your :set langmap I understand that you mapped a to c so, by typing aaa, did you expect to obtain ccc?
From what I understand (:help langmap), your custom substitutions are not available in INSERT mode for actually inserting stuff and I don't see a mention of the SELECT mode you are in when overwriting SnipMate's placeholders.
If I do this
:set langmap+=ac,bs
and I type aaa in SELECT mode, I obtain caa.
That's because langmap applies to the first a (:help Select-mode) and, therefore inserts c. But, after this first character I am in INSERT mode for all subsequent characters. Since langmap doesn't apply in INSERT mode, aa is inserted as is.
What is not clear to me is why you obtain baa instead of caa. Your langmap seems to be pretty clear about your intention: you want a to insert c and b to insert s. Typing a shouldn't insert b.
I smell a risk of mistyping in your .vimrc. Try this: reset your set langmap and start adding your mappings one by one.
May I ask you what is the purpose of such a massive remapping?
C program which outputs mappings similar behavior to langmap but not for select:
/* input:
lhs rhs optional-descripton
lhs rhs ...
*/
#include <stdlib.h>
#include <stdio.h>
int main() {
FILE *fi = fopen("in.txt", "r");
FILE *fo = fopen("out.txt", "w");
char lc[8], rc[8];
while (fscanf(fi, "\n%s %s", lc, rc) != EOF) {
fprintf(fo, "nnoremap %s %s\n", lc, rc);
fprintf(fo, "xnoremap %s %s\n", lc, rc);
fprintf(fo, "onoremap %s %s\n", lc, rc);
while (fgetc(fi) != '\n');
}
fclose(fo);
fclose(fi);
}
It doesn't work identically to langmap and so it might break other bindings.
This has now been fixed in vim 7.4.1150. See
https://github.com/vim/vim/issues/572
for details.