Disable trimming of bracket whitespace after a semicolon in vim - vsvim

I recently started working on source code which requires the following convention for function arguments:
void example( int arg );
However, after typing a semicolon, vim automatically trims any whitespace between the parenthesis:
void example(int arg); //after typing the ;
Is there a way to turn this feature off?

I was using VsVim and the trimming was being applied by Visual Studio.

VisualStudio offers options to control such formatting behavior. Go to Tools > Options and search for "spacing".

Related

Is there a way to set EOL to LF in Visual Studio for whole solution or project?

In Visual Studio you can set file's EOL to CRLF, LF, or CR, however you can do it only to a single file. I'm looking for a way that will make sure that every new file made in my solution will automatically use LF. Any ideas? Thanks in advance!
Adding an .editorconfig file in the root directory of the project (or solution) with the following entry should set line endings to LF by default (for newly added lines, see the notes below).
[*]
end_of_line = lf
Quoting from the Create portable, custom editor settings with EditorConfig page.
You can add an EditorConfig file to your project or codebase to enforce consistent coding styles for everyone that works in the codebase.
The editor in Visual Studio supports the core set of EditorConfig properties: [...]   end_of_line
When you add an .editorconfig file to a folder in your file hierarchy, its settings apply to all applicable files at that level and below.
When you add an EditorConfig file to your project in Visual Studio, new lines of code are formatted according to the EditorConfig settings. The formatting of existing code is not changed unless you run one of the following commands:
Code Cleanup (Ctrl+K, Ctrl+E), which applies any white space settings, such as indent style, and selected code style settings, such as how to sort using directives.
Edit > Advanced > Format Document (or Ctrl+K, Ctrl+D in the default profile), which only applies white space settings, such as indent style.
Note
If you are looking for a way to control the default translation mode for opening files in Windows, you can use _set_mode() or set the _fmode global variable documented here:
int _fmode = _O_BINARY;
Or at run time:
_set_mode(_O_BINARY); // defined in `<stdlib.h>`
There seems to be a way to set this more globally by modifying the Global state in the CRT, but the details are beyond my skill level in this environment.
Because default settings.json contain "files.eol": "auto", you can browse this under VSCode documentation. You need set The default end of line character to appropriate value, e.g.:

How to underline text using printf in C

I note the question Colorful text using printf in C gives a good example of setting coloured text on the standard console output in Windows. Is there something similar that allows output to be underlined? Or possibly even bolded or italicised?
EDIT: I tried Lundin's answer on using COMMON_LVB_UNDERSCORE with no luck. Attempting to use AddFontResource() to add arial italic font to try italics gives an error that there is an undefined reference to __imp_AddFontResourceA
It is not possible to do so using any standard C functions, as the C language doesn't even recognize the presence of a screen.
With Windows API console functions you can change colors, underline and some other things. The particular function you are looking for is called SetConsoleTextAttribute just as in the post you linked. Change its attributes to include COMMON_LVB_UNDERSCORE.
You might run your program in some environment with a terminal accepting ANSI escape codes.
(I never used Windows - since I am using Linux only -, so I don't know how to set up such environment in Windows; but I heard that it is possible)
With ANSI escape codes, underlining is "\e[4m" with \e being the ASCII ESCAPE character.
Perhaps try using termcaps. Something like this (after initializing termcaps) :
printf(tgetstr("us", NULL)); /* underline on */
printf(""/* your string */);
printf(tgetstr("ue", NULL)); /* underline off */
or more concise :
printf("%s/* your text here */%s", tgetstr("us", NULL), tgetstr("ue", NULL));
https://www.gnu.org/software/termutils/manual/termcap-1.3/html_node/termcap_34.html
The '\r' sequence differs from the newline ('\n') in that it moves the cursor to the
beginning of the current line of output, not to the beginning of the next line. Using
'\r' gives a program the ability to create a file containing more than one character
in one line position.
This prints an underlined text
printf("\f\t\t\tFinal Report\r\t\t\t____________\n");

Format of structs using emacs c code auto-newline

I have recently started using emacs for editing C source, and have been using the auto-newline feature of cc-mode (c-toggle-auto-newline). This works well for constructs like functions and if/else statements, but seems to act strangely when a closing brace should be followed by a semi-colon.
Using auto-newline in GNU Emacs 23.3 I get:
struct foo
{
int x;
}
;
char int[2] =
{
0, 1
}
;
I would like to instead get:
struct foo {
int x;
};
char int[2] = { 0, 1 };
How can I get the closing semi-colon to remain on the same line as the closing brace?
I don't think you can go around this problem with auto newline on. It's not a greatly thought-through feature, it simply inserts newlines after certain characters (;, {, etc.). But seriously, how hard is it to press and enter key? Any automation is always error-prone.
You can customize the "cleanup" behavior when auto-newline is turned on. This is controlled by the contents of the c-cleanup-list variable. (View help for this within Emacs by entering C-h v c-cleanup-list.
Specifically, adding defun-close-semi to c-cleanup-list will solve your problem.
If you're already defining a custom style within your ~/.emacs file, then you can probably figure out how to do this. Otherwise, the easiest way to change this setting is via Customize. In the help buffer (displayed when you ran C-h v c-cleanup-list), the last line will have a link to customize this variable.

Condense multiline C function prototypes

I have C function prototypes (certain windows api header files) that look like:
int
foo
(
int
a
,
int
*
b
)
;
(they seem to have no coding convention)
which I am trying to programmatically turn into a one-line prototype of the form (or something close to it):
int foo(int a, int * b);
I have looked into programs like ctags ( ctags multi-line C function prototypes ) and into various settings in uncrustify ( http://uncrustify.sourceforge.net/ ) however I haven't been able to make any headway in either. (any insight would be great, or perhaps one of the 385 uncrustify options that I missed does what I want).
Programmatically, I am trying to look for unique markers that signify a function prototype so that I can write a script that will format the code to my liking.
Without using a lexer and a parser this seems like it could get very convoluted very quickly; any suggestions?
run them through indent -kr or astyle --style=kr
Solution using vim?
put marker on int and do 11J
sed ':a;N;$!ba;s/\n/ /g' prototypes.file | sed 's/; */;\n/g'
The first command - before the pipe - will replace all new-lines to spaces, and the next will put a new-line back after every semicolon.
Of course this will only work if there are nothing else but these prototypes in the file. If there are some other stuff that you want to keep as they are, you can use vim's visual selection and two substitution commands:
Select the region you want to join, than
:s/\n/ /
Select the joined line and
:s/; */;\r/g
Another solution using vi:
do a regex search removing all newlines. Then take the resulting mess and do another regex search replacing each ; with ; \n\n. That should leave you with a list of prototypes with a line skipped between each one. Since we're marking the ends of the prototypes instead of the beginnings and all prototypes end the same way, we don't have to worry about not recognizing special cases.

How can I autoformat/indent C code in vim?

When I copy code from another file, the formatting is messed up, like this:
fun()
{
for(...)
{
for(...)
{
if(...)
{
}
}
}
}
How can I autoformat this code in vim?
Try the following keystrokes:
gg=G
Explanation: gg goes to the top of the file, = is a command to fix the indentation and G tells it to perform the operation to the end of the file.
I like to use the program Artistic Style. According to their website:
Artistic Style is a source code indenter, formatter, and beautifier for the C, C++, C# and Java programming languages.
It runs in Window, Linux and Mac. It will do things like indenting, replacing tabs with spaces or vice-versa, putting spaces around operations however you like (converting if(x<2) to if ( x<2 ) if that's how you like it), putting braces on the same line as function definitions, or moving them to the line below, etc. All the options are controlled by command line parameters.
In order to use it in vim, just set the formatprg option to it, and then use the gq command. So, for example, I have in my .vimrc:
autocmd BufNewFile,BufRead *.cpp set formatprg=astyle\ -T4pb
so that whenever I open a .cpp file, formatprg is set with the options I like. Then, I can type gg to go to the top of the file, and gqG to format the entire file according to my standards. If I only need to reformat a single function, I can go to the top of the function, then type gq][ and it will reformat just that function.
The options I have for astyle, -T4pb, are just my preferences. You can look through their docs, and change the options to have it format the code however you like.
Here's a demo. Before astyle:
int main(){if(x<2){x=3;}}
float test()
{
if(x<2)
x=3;
}
After astyle (gggqG):
int main()
{
if (x < 2)
{
x = 3;
}
}
float test()
{
if (x < 2)
x = 3;
}
The builtin command for properly indenting the code has already been mentioned (gg=G). If you want to beautify the code, you'll need to use an external application like indent. Since % denotes the current file in ex mode, you can use it like this:
:!indent %
I find that clang-format works well.
There are some example keybindings in the clang documentation
I prefer to use the equalprg binding in vim. This allows you to invoke clang-format with G=gg or other = indent options.
Just put the following in your .vimrc file:
autocmd FileType c,cpp setlocal equalprg=clang-format
The plugin vim-autoformat lets you format your buffer (or buffer selections) with a single command: https://github.com/vim-autoformat/vim-autoformat. It uses external format programs for that, with a fallback to vim's indentation functionality.
I like indent as mentioned above, but most often I want to format only a small section of the file that I'm working on. Since indent can take code from stdin, its really simple:
Select the block of code you want to format with V or the like.
Format by typing :!indent.
astyle takes stdin too, so you can use the same trick there.
I wanted to add, that in order to prevent it from being messed up in the first place you can type :set paste before pasting. After pasting, you can type :set nopaste for things like js-beautify and indenting to work again.
Maybe you can try the followings
$indent -kr -i8 *.c
Hope it's useful for you!
Their is a tool called indent. You can download it with apt-get install indent, then run indent my_program.c.
For a good overview and demo of many of the options mentioned here, #Gavin-Freeborn has a great video on YouTube:
https://www.youtube.com/watch?v=tM_uIwSucPU
It covers some Vim plugins as well as built-in capabilities such as =, gq, and formatprg.

Resources