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

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.:

Related

vi/vim - custom formatting depending on presence of special file or tag inside code

Question
Is there a simple/reliable way to have VIM, on a project/directory specific base, either detect a special file (ie: custom .vimrc with a couple settings), or to change run-time settings based on the presence of a special tag/string/hash in a comment at the beginning of a c source (.c) or header (.h) file? The string/hash must map to a function/setting in the .vimrc file, and must not contain the actual settings themselves.
Background
I have a mutli-developer project where we all have a common set of code style settings for our various editors (emacs and vim, primarily), and we all adhere strictly to these settings, such as newline style (CR versus CR+LF), indentation (length, hard-tabs versus expanded-as-spaces), and so on.
Problem
I'm creating a few new projects that, for reasons beyond our control (ie: static code analysis tool we have to use), will require different style settings than ours. There are ways to bypass this in the static code analysis tool, but there's a non-technical/legal requirement that we avoid disabling "features" of this tool.
For each of these new projects, I would like to somehow make vi/vim aware of some special flag, either by the presence of a special file in the root of the project's directory structure, or by a special keyword/tag/hash/etc I could put inside a /* C-style block comment */. When vi/vim is aware of the presence of this "trigger", I would want it to invoke a function to override the style settings for newlines, indentation, etc. If this is possible, is it also possible to have several, mutually exclusive such "triggers" so that everyone has a common .vimrc and the project determines which style to utilize?
Question - redux
Is there a straightforward way to accomplish this?
One solution: modelines (:help modeline) for Vim and file variables for Emacs.
Those are special comments you put in your files that are interpreted by your editor. You can use them to set indent style, file encoding, etc.
In my opinion, modelines are ugly noise.
One solution for Vim: .exrc (:help 'exrc').
You can put your project-specific settings in a .exrc file at the root of your project. The manual claims this solution is insecure but I fail to see how normal functioning adult could be beaten by it. YMMV.
One solution for Vim: directory-specific autocommands.
That's the safer alternative mentioned at the end of :help 'exrc' but it requires each contributor to add stuff to his own vimrc so… not that useful I guess.
The definitive solution: editorconfig.
You put your settings in a .editorconfig at the root of your project and let each contributor's IDE/editor deal with it.
... to change run-time settings based on the presence of a special
tag/string/hash in a comment at the beginning of a c source (.c) or
header (.h) file?
Yes, they're called modelines. http://vim.wikia.com/wiki/Modeline_magic
They can appear at the start or end of files.
An example from some C sources of mine:
/* vim:ft=c:expandtab:sw=4:ts=4:sts=4:
*/
See :help modeline in vim for more info.
Central configuration
If it's okay to configure the specific commands / local exceptions centrally, you can put such autocmds into your ~/.vimrc:
:autocmd BufRead,BufNewFile /path/to/dir/* setlocal ts=4 sw=4
It is important to use :setlocal instead of :set, and likewise :map <buffer> ... and :command! -buffer ....
On the other hand, if you want the specific configuration stored with the project (and don't want to embed this in all files via modelines), you have the following two options:
Local config with built-in functionality
If you always start Vim from the project root directory, the built-in
:set exrc
enables the reading of a .vimrc file from the current directory. You can place the :set ts=4 sw=4 commands in there.
Local config through plugin
Otherwise, you need the help of a plugin; there are several on vim.org; I can recommend the localrc plugin (especially with my own enhancements), which even allows local filetype-specific configuration.
Note that reading configuration from the file system has security implications; you may want to :set secure.
I've covered the main alternatives in this answer : https://stackoverflow.com/a/456889/15934 (Yes, your question is almost a duplicate: different formulation, but same solutions).
Modelines are really limited: you have to use a plugin to set things that are not vim options.
.exrc doesn't look behind the current directory
editorconfig is restricted to very specific options: don't expect to forward plugin specifications like where compilation directories are (this is how I support multiple compilation modes with CMake -- others prefer playing with ccache and tuning the CMakeCache, but this doesn't work well when using g++ and clang++ one after the other), how the linter shall be called, your naming conventions...
autocommand don't scale and cannot be ported easily from one directory to the other.
In the end, the best solutions are plugin based IMO: There a plenty plugin solutions see the non exhaustive list at the end of my local_vimrc plugin's README
Note also that since my previous answer, I've initiated another experiment to simplify project management. For instance, I introduce p:variables which are variables shared among all buffers belonging to a project.

Sublime Text 2 plugin editorconfig does not work on Mac OSX

I am used to work with an .editorconfig file at the root of my projects and this one works great with phpstorm. But for some reasons I cannot make it work with Sublime Text 2 on Mac OSX mavericks.
Sublime 2.0.2 build 2221
I tried the official editorconfig package installation here https://sublime.wbond.net/packages/EditorConfig with the console and manually but no change after restart of Sublime or even my Mac:
Sublime just use his default settings for all indentations.
Any idea what I should check ? I am stuck.
Here is my .editorconfig file for info:
# EditorConfig is awesome: http://EditorConfig.org
# top-most EditorConfig file
root = true
# Unix-style newlines with a newline ending every file
[*]
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
charset = utf-8
# 4 space indentation
[*.php]
indent_style = space
indent_size = 4
# 2 space indentation
[**.md]
indent_style = space
indent_size = 2
# 2 space indentation
[**.yaml]
indent_style = space
indent_size = 2
# Tab indentation (no size specified)
[**.js]
indent_style = tab
[**.html]
indent_style = tab
[**.less]
indent_style = tab
indent_size = 4
You don't use editor config files. You use sublime settings JSON files. These can be specific to your project, a language, or the whole editor. Go to "Sublime Text" next to the apple symbol (on a Mac, sorry I don't know where this is on PC or Linux), go to preferences and then Settings - Default to see what you can change (it's well commented). See the documentation here for all the places you can put settings for different things (like settings for one language or one project).
EDIT: I don't know what plugin you're talking about. You are linking to the install page to the excellent Package Control. Once you have typed that snippet into your console, you can press Command+Shift+P, type insp, hit enter, and search for your plugin to install it. After you install it (I assume you are talking about this plugin), you'll need to create an .editorconfig file at the highest-level directory for your project.
EDIT 2: I have a feeling your directory structure is wrong. Would you mind showing the directory structure of your project (feel free to change filenames if you feel the need to)? If you're on OS X, you can install a tree command with brew install tree. (If you don't have the excellent home brew package manager, go get that.) On Linux or even Windows, this should work out of the box. Anyways, grab the output of that. Otherwise, there could very well be a bug in the plugin, which you can' treacly do anything about unless you have the time to look at the source, hack on it and if your the kind of person who does this kind of thing make a pull request.
I found an answer here, but I think there is an error in that the translate_tabs_to_spaces must be set to true, not false.
So, basically, edit your Sublime settings-user file and add "translate_tabs_to_spaces": true
such that it overrides the default settings. I think this is a bug with the plugin as I would have expected EditorConfig to override the Sublime defaults. After all, is this not what you use EditorConfig to do in the first place?

C: library for parsing configuration files and command line

I'm looking for some library with support for strict set of options (so non-existent options couldn't be set in config file) and possibility to also parse command line to override options from config file. Any ideas?
For command line, there is getopt or plentiful of code, some with more, some with less strange APIs, some in the form of open-codedness like getopt, others in table form with or without callback ability. As for config file, there is (lib)augeas if you need support for almost arbitrary formats.
Assuming you can use LGPL stuff in your project, there's http://www.hyperrealm.com/libconfig/, which appears, according to the docs, to have support in the API for setting values after a file has been parsed.

Xcode change/remove comment template

Recently I've been learning how to program C. For most of the time, I have been using the C version of Eclipse. Recently, I tried out Xcode. I am using a Mac running Mac OS X Lion - Xcode version 4.1.
There is one grievance I have: at the top of every file that I create, there is this little section of comments that I wish to remove or better yet, change.
When I create a file, something like this is put at the top of the file by default:
//
// FILE.c
// PROJECT NAME
//
// Created by Martin Tuskevicius on DATE.
// Copyright YEAR ORGANIZATION (my school name for some reason). All rights reserved.
//
Obviously the things in capitals would be replaced with an actual value. For those of you have use, or have used Xcode, for programming C - do you know a way of how to change or remove these default comments?
I really appreciate any help.
Thanks!
UPDATE:
According to #Michael Dautermann 's comment below, change templates in Xcode.app bundle is not a good way. Check https://stackoverflow.com/a/33743/380774 for more information.
You can remove or change the header in File Templates, I'm using Xcode 4.3, and the File Templates is in /Applications/Xcode.app/Contents/Developer/Library/Xcode/Templates/File Templates.
Please do not edit files inside Xcode, that will break the application signature and will cause Xcode to refuse to start up after the next restart or so.
Create an IDETemplateMacros.plist file containing a dictionary with a FILEHEADER key (string) instead.
You can put the file in
for all users on a single project by dropping it in your project's or workspace's xcshareddata folder (e.g. MyAppWorkspace.xcworkspace/xcshareddata/IDETemplateMacros.plist)
for yourself for a single project by copying it into e.g MyAppWorkspace.xcworkspace/xcuserdata/YOURNAMEHERE.xcuserdatad
global for all projects that you open in your account by dropping the file in ~/Library/Developer/Xcode/UserData/
You can change it in Xcode project File.
This is my image for tutorial :D
Very easy!!!

Any way to auto-indent a c/c++ code with Kdevelop?

I've a source code that's not mine, there are not comment and it's completely not indented. Is it a really mess. Do you know if there is some option, addon for kdevelop that would auto-indent it?
Assuming you're using KDevelop 4 and not the (no longer supported) KDevelop 3...
You can use "Edit > Reformat Source" to format the current file source.
You can also right click on one or more files in Projects view on the left and select "Format Files"
You can configure your source formatting settings in "Settings > Configure KDevelop > Source Formatter".
KDevelop 4 uses AStyle to handle the formatting.
Edit:
It should be noted that there are also some settings in "Settings > Configure Editor" which configures the embedded KatePart.
These are the "as you edit" settings with no effect on the source formatting component detailed above. This would control the auto-indentation when inserting newlines after a bracket, for example.
Good old command line indent : http://linux.die.net/man/1/indent
In KDevelop if I remember correctly you can go to Settings -> Configure -> Indentation. (Or something similar). Though I can't remember if that's settings for Auto-Indentation as you write or whether its for formatting code in general.
Tools like indent are quite powerful but has tons of switches to be flexible enough to support all coding style.
If it's an editor you're after, vim does syntax-sensitive indention for C nicely. Open the file with Vim and do "gg=G" and you're done.

Resources