VSCode auto format on paste removes leading new lines - visual-studio-code

Whenever I paste copied text in vscode, it removes all leading newlines.
Copied Text:
Cursor Position:
Pasted Result:
I can't seem to tell VSCode to preserve the leading newlines. I know I can add a new line before I paste, but that is dumb and I shouldn't have to do that. I still want auto format on to fix my indentation and such.
Everything I search for brings up questions on trailing spaces and other stuff like that. No one else else seems to have issue.

This 'feature' you've mentioned is part of "Format On Paste" option.
The whole feature can be turned off in File -> Preferences -> Settings -> Text Editor -> Formatting -> Format On Paste
As far as I know sadly there is no option of disabling only newline removal from "Format On Paste" feature.

Related

Syntax highlighting copying "non-breaking spaces" in VSCode

For whatever reason, VSCode is copying non-breaking spaces, when I copy/paste code with syntax highlighting. This is no problem as long as I work inside VSCode, but it's a nightmare when copy/pasting stuff in and out of VSCode.
I'm using MSWord with the "hidden characters" feature activated as an easy means to understand what is happening. I can reproduce it like this:
Create a new file (no extension, not saved) in VSCode, type some text, select
all and copy/paste in MSWord -> spaces are normal U+0020 spaces,
NO syntax highlighting was copied.
Save the file as a ".txt" file, select all and copy/paste in MSWord -> spaces are normal U+0020 spaces, NO syntax highlighting was copied.
Save as a python file ".py" (or Markdown, or HTML...), select all and copy/paste in MSWord -> spaces are non-breaking-spaces U+00A0 spaces, syntax highlighting was copied.
Save as a text file ".txt", select all and copy/paste in MSWord -> spaces are non-breaking-spaces U+00A0 spaces, syntax highlighting was copied.
Since a picture is better than 1000 words, here's the output from MSWord:
Any idea?
It seems that VSCode copy styled or unstyled content into the clipboard depending on the context. That may cause trouble when pasting into some kind of rich text editor.
In the settings JSON file add editor.copyWithSyntaxHighlighting set to false in order to disable this behaviour for good.
Note that there is still a separate command Copy With Syntax Highlighting in the pallete which you can bind to some other keys in case you need it. This one does not affected by the option above.
Refer to this PR https://github.com/microsoft/vscode/pull/54155

Use BBedit to replace CR with LF

I have a text file where I would like to change all the Carriage Return to Line Feed. I am working on a Mac, and it seems like BBedit should be able to easily do this. However, in the search function it does not appear to differentiate between CR (\r) and LF (\n). Searching for either character gets the same hits, and a search for "\r" to replace with "\n" does not work.
Is there some other way to represent CR and LF, so that BBedit can differentiate between them?
A possibly simpler way to do this is to make sure you enable "Line Break Types" in BBedit's Preferences -> Appearance.
This will add a Line Types popup menu at the bottom of the window.
Not only does it show you what the current format is, but it makes it trivial to switch between them.
(I also turn on "Text Encoding" as well, in case I need to switch between UTF8 and UFT16 and others.)
For a single file:
Text -> Apply Text Transform
Select Change Line Endings
Click the Configure button and choose your desired line endings
For multiple files:
File -> New -> Text Factory
Select Change Line Endings
Click the Options button and choose your desired line endings
Save the Text Factory and apply it to the files you want changed through the Choose and Options buttons

Convert Automatically Spaces to Tabs in Visual Studio Code

I have a file in Visual Studio Code in which I use only tabs for indentation. When I copy the contents of that file somewhere else and edit them, then when I paste them back all tabs are gone and spaces have taken their places.
How can I prevent the use of spaces for indentation and automatically spaces will be converted to tabs?
Also I have changed the preferences:
"editor.insertSpaces": false,
"editor.detectIndentation": false,
Unfortunately you can't (yet). I think it would be nice to get spaces/tabs converted upon file opening - like some editors do. You can quickly convert spaces by tabs by selecting the number of indentation spaces, pressing cmd+F2 in mac or ctrl-F2 in linux/windows (to find and select all occurrences of that amount of spaces), and then pressing tab key (only once). You can also give Untabfy (or Whitespacer) a try.

Can NetBeans remove trailing whitespace and change tabs to spaces on save?

I just started to use NetBeans, and am trying to work out how to:
Make it so that trailing whitespace is removed on save. I know that you can invoke it manually, but it’s annoying if you forget. Can it be done automatically on save? I want this done because it keeps the code neat, because it’s really annoying when you’re moving through code and the text cursor goes all over the place because of trailing whitespace you don’t see, and because trailing whitespace has unintended effects on HTML layouts and other stuff.
Make it so that tabs are automatically made into 4 spaces while you’re typing rather than converting on save, or, worse, only when manually invoked.
For the first question, NetBeans 7.0+ has now a built-in solution. You can find it in:
On NetBeans 7:
Tools > Options > Editor > General > When saving files > Remove Trailing Whitespace: Never / Always / For Modified Lines Only
On NetBeans 8 / 9:
Tools > Options > Editor > On Save > Remove Trailing Whitespace From: None / All Lines / Modified Lines Only
[Original] For your first question, how to set NetBeans to remove trailing whitespace, the answer is that there is no setting (at the moment) to control that. It happens automatically, but not to every line. Whitespace is only removed from the lines you edit (or from the entire file if invoked manually). See Petr Pisl's blog entry titled "Removing trailing spaces" for a more complete explaination. (Note that even though he is blogging about NetBeans' PHP features, this particular feature applies to the basic editor shared by all NetBeans supported languages.)
[Update] This feature has been made available as of version 7.0, as pointed out by others in comments and answers below.
For your second question, set the editor to automatically make tabs into spaces as you type, you can find that setting in the Editor Formatting options:
Click on the Tools menu, and choose Options
Click on Editor in the top row of icons
Click on the Formatting tab
In the Language field, ensure that All Languages is selected
In the Category field, ensure that Tabs And Indents is selected
Check the box for Expand Tabs to Spaces
For each additional language that is available in the Language field, check that either Override Global Options is not checked, or that Expand Tabs to Spaces is also checked for that language.
Click OK
That will make sure that when you hit the tab key the specified number of spaces is inserted rather than a tab character. As above with whitespace removal, any existing tabs in the file are left as-is unless you invoke the manual command to change the entire file. (Moreover, even if you edit a line with tabs on it--leaving the tabs in place--the editor will not convert them to spaces. Only new instances of hitting the TAB key are controlled by this option.)
Tools > Options > Editor > On Save > Remove Trailing Whitespace From: None / All Lines / Modified Lines Only
This is as of Netbeans 7.3, according to Webmut.
For netbeans 7.4+
Windows - Tools > Options > Editor > On Save > Remove Trailing Whitespace From: None / All Lines / Modified Lines Only
Mac - Netbeans > preferences > Options > Editor > On Save > Remove Trailing Whitespace From: None / All Lines / Modified Lines Only
Also you can set this as language specific.
You can also set reformat while saving.
if you want to remove all trailing spaces in the file manually ONLY ONCE, you can invoke action 'Remove Trailing Spaces' from Source menu manually.
Source -> Remove Trailing Spaces

How to auto-remove trailing whitespace in Eclipse?

The question has two parts, one of which I already have the answer for.
How to auto-remove trailing whitespace from the entire file being edited? -> Answer: use the AnyEdit plugin, which can be set to do that on any save to the file.
How to auto-remove trailing whitespace only from the lines I changed? -> This I don't know and would appreciate any help.
I assume your questions is with regards to Java code. If that's the case, you don't actually need any extra plugins to accomplish 1). You can just go to Preferences -> Java -> Editor -> Save Actions and configure it to remove trailing whitespace.
By the sounds of it you also want to make this a team-wide setting, right? To make life easier and avoid having to remember setting it up every time you have a new workspace you can set the save action as a project specific preference that gets stored into your SCM along with the code.
In order to do that right-click on your project and go to Properties -> Java Editor -> Save Actions. From there you can enable project specific settings and configure it to remove trailing whitespace (among other useful things).
NB: This option has been removed in Eclipse Kepler (4.3) and following releases.
NB #2: The option seems to be back in Eclipse Luna - Luna Service Release 1a (4.4.1)
Removing whitespace from the entire file being edited:
Preferences -> Java -> Editors -> Save Actions -> check Perform the selected actions on save -> check Additional actions -> click Configure.. -> go to Code organizing tab -> check Remove trailing whitespace -> select All lines.
Removing whitespace only from the lines I changed:
Preferences -> Java -> Editor -> Save Actions -> check "Perform the selected actions on save -> check Format source code -> select Format edited lines.
Note it is not necessary to click Configure the formatter settings on the Formatter page - all the code styles automatically include removing trailing whitespace. At least I couldn't find a setting for this in Formatter configuration, and it worked out of the box for built-in Java Conventions, Eclipse, Eclipse 2.1 styles, as well as GoogleStyle.
When using this set-up, you obviously need to also turn off the solution to part 1 of the question.
Eclipse version checked: 4.5.2, 4.11
You don't need any plugin to do so. For instance, if you code JAVA, you can erase trailing whitespaces configuring save actions:
Eclipse 3.6
Preferences -> Java -> Editors -> Save Actions -> Check Perform the selected actions on save -> Check Additional actions -> Click the Configure.. button.
In the Code organizing tab, check Remove trailing whitespace
PyDev can do it by either Ctrl+Shift+F if you have code formatter option set to do it, or by during saving:
Eclipse -> Window -> Preferences -> PyDev -> Editor -> Code Style -> Code Formatter:
I use at least these:
Auto format before saving
Right trim lines?
Add new line at end of file
Do following:
Preferences -> Java -> Editor -> Save Actions
You can map a key in Eclipse to manually remove trailing whitespaces in the whole file, but only on request instead of automatically at save. (Preference/Keys and then map a set of keys to File/Remove Trailing Whitespace) This can be useful if you want to sanitize all new files, but keep legacy code untouched.
Another strategy is to activate visual display of whitespace, so at least you'll know when you're adding some trailing whitespace. As far as I know, there's no way to display only trailing whitespace though, but I'll be glad to be proved wrong.
In a pinch, for those editors that don't support removal of trailing whitespace at all (e.g. the XML editor), you can remove it from all lines by doing a find and replace, enabling regular expressions, then finding "[\t ]+$" and replacing it with "" (blank). There's probably a better regex to do that but it works for me without needing to install AnyEdit.
I would say AnyEdit too. It does not provide this specific functionalities. However, if you and your team use the AnyEdit features at each save actions, then when you open a file, it must not have any trailing whitespace.
So, if you modify this file, and if you add new trailing spaces, then during the save operation, AnyEdit will remove only these new spaces, as they are the only trailing spaces in this file.
If, for some reasons, you need to keep the trailing spaces on the lines that were not modified by you, then I have no answer for you, and I am not sure this kind of feature exists in any Eclipse plugin...
It is impossible to do it in Eclipse in generic way right now, but it can be changed given with basic Java knowledge and some free time to add basic support for this https://bugs.eclipse.org/bugs/show_bug.cgi?id=180349
The dependent issue: https://bugs.eclipse.org/bugs/show_bug.cgi?id=311173
For php there is also an option:
There is a really easy way to do this with sed, the Unix command line tool. You could probably create a macro in Eclipse to run this:
sed -i 's/[[:space:]]*$//' <filename>
As #Malvineous said, It's not professional but a work-around to use the Find/Replace method to remove trailing space (below including tab U+0009 and whitespace U+0020).
Just press Ctrl + F (or command + F)
Find [\t ][\t ]*$
Replace with blank string
Use Regular expressions
Replace All
extra:
For removing leading space, find ^[\t ][\t ]* instead of [\t ][\t ]*$
For removing blank lines, find ^\s*$\r?\n
I am not aware of any solution for the second part of your question. The reason is that it is not clear how to define I changed. Changed when? Just between 2 saves or between commits... Basically - forget it.
I assume you would like to stick to some guideline, but do not touch the rest of the code. But the guideline should be used overall, and not for bites and pieces. So my suggestion is - change all the code to the guideline: it is once-off operation, but make sure that all your developers have the same plugin (AnyEdit) with the same settings for the project.
I used this command for git: git config --global core.whitespace cr-at-eol
It removes ^M characters that are trailing.