How to auto-indent code in the Atom editor? - code-formatting

How do you auto-indent your code in the Atom editor? In other editors you can usually select some code and auto-indent it.
Is there a keyboard shortcut as well?

I found the option in the menu, under Edit > Lines > Auto Indent. It doesn't seem to have a default keymap bound.
You could try to add a key mapping (Atom > Open Your Keymap [on Windows: File > Settings > Keybindings > "your keymap file"]) like this one:
'atom-text-editor':
'cmd-alt-l': 'editor:auto-indent'
It worked for me :)
For Windows:
'atom-text-editor':
'ctrl-alt-l': 'editor:auto-indent'

The accepted answer works, but you have to do a "Select All" first -- every time -- and I'm way too lazy for that.
And it turns out, it's not super trivial -- I figured I'd post this here in an attempt to save like-minded individuals the 30 minutes it takes to track all this down. -- Also note: this approach restores the original selection when it's done (and it happens so fast, you don't even notice the selection was ever changed).
1.) First, add a custom command to your init script (File->Open Your Init Script, then paste this at the bottom):
atom.commands.add 'atom-text-editor', 'custom:reformat', ->
editor = atom.workspace.getActiveTextEditor();
oldRanges = editor.getSelectedBufferRanges();
editor.selectAll();
atom.commands.dispatch(atom.views.getView(editor), 'editor:auto-indent')
editor.setSelectedBufferRanges(oldRanges);
2.) Bind "custom:reformat" to a key (File->Open Your Keymap, then paste this at the bottom):
'atom-text-editor':
'ctrl-alt-d': 'custom:reformat'
3.) Restart Atom (the init.coffee script only runs when atom is first launched).

Package auto-indent exists to apply auto-indent to entire file with this shortcuts :
ctrl+shift+i
or
cmd+shift+i
Package url : https://atom.io/packages/auto-indent

I prefer using atom-beautify, CTRL+ALT+B (in linux, may be in windows also) handles better al kind of formats and it is also customizable per file format.
more details here: https://atom.io/packages/atom-beautify

You can just quickly open up the command palette and do it there
Cmd + Shift + p and search for Editor: Auto Indent:

This works for me:
'atom-workspace atom-text-editor':
'ctrl-alt-a': 'editor:auto-indent'
You have to select all with ctrl-a first.

This is the best help that I found:
https://atom.io/packages/atom-beautify
This package can be installed in Atom and then CTRL+ALT+B solve the problem.

On Linux
(tested in Ununtu KDE)
There is the option in the menu, under Edit > Lines > Auto Indent or press Cmd + Shift + p, search for Editor: Auto Indent by entering just "ai"
Note: In KDE ctrl-alt-l is already globally set for "lock screen" so better use ctrl-alt-i instead.
You can add a key mapping in Atom:
Cmd + Shift + p, search for "Settings View: Show Keybindings"
click on "your keymap file"
Add a section there like this one:
'atom-text-editor':
'ctrl-alt-i': 'editor:auto-indent'
If the indention is not working, it can be a reason, that the file-ending is not recognized by Atom. Add the support for your language then, for example for "Lua" install the package "language-lua".
If a File is not recognized for your language:
open the ~/.atom/config.cson file (by CTRL+SHIFT+p: type ``open config'')
add/edit a customFileTypes section under core for example like the following:
core:
customFileTypes:
"source.lua": [
"conf"
]
"text.html.php": [
"thtml"
]
(You find the languages scope names ("source.lua", "text.html.php"...) in the language package settings see here)

If you have troubles with hotkeys, try to open Key Binding Resolver Window with Cmd + .. It will show you keys you're pressing in the realtime.
For example, Cmd + Shift + ' is actually Cmd + "

You could also try to add a key mapping witch auto select all the code in file and indent it:
'atom-text-editor':
'ctrl-alt-l': 'auto-indent:apply'

I was working on some groovy code, which doesn't auto-format on save. What I did was right-click on the code pane, then chose ESLint Fix. That fixed my indents.

If you are used to the Eclipse IDE or the Netbeans, you can use the package eclipse-keybindings (https://atom.io/packages/eclipse-keybindings):
This Atom package provides Eclipse IDE key mappings for Atom. Currently, the Eclipse shortcuts are directly mapped to existing Atom commands.
To format all lines from a file, just use: Ctrl+Shift+F.

Ctrl+Shift+i worked for me in PHP under Windows ... but some files did not react. Not being the brightest it took me a while to work out that it was the include files that were the problem. If you are using echo(' ... PHP ...') then the PHP does not get re-formatted. To get over this, create a temporary PHP file, say t.php, copy the PHP part into that, reindent it (Ctrl+Shift+i ... did I mention that?) and then copy the newly reformatted PHP back into the original file. Whilst this is a pain, it does give you correctly formatted PHP.

Related

Issue with Overriding Default Keybinding in Atom Text Editor

I'm attempting to modify my keybindings in Atom.
So far I've been successful in adding a custom new keybindings via the keymap.cson file. However, I've encountered a problem when attempting to override a default one.
I'm trying to replace the default Ctrl + Shift + S, which is bound to Save as by default, to perform the Save all command instead. Of course, merely adding didn't work, so after a while I figured out I should unset! the key combination first. Now it looked way more promissing in the Key Binding Resolver.
This keymap produced following output in the Key Binding Resolver:
'body':
'unset!': 'core:save-as'
'ctrl-shift-S': 'core:save-all' # Key Binding Resolver claims this works fine
# but it doesn't do anything
'ctrl-shift-E': 'editor-stats:toggle' # works fine
'atom-text-editor':
'ctrl-shift-A': 'bracket-matcher:select-inside-brackets' # works fine
Despite that, however, the key binding Ctrl + Shift + S doesn't save anything at all. Accessing the command from the context menu works fine. I've searched the web, the core:save-all command seems to exist, it even has a default keybinding on Mac.
I am using the latest version of Atom on Windows 10, made sure no package conflicts with the keybind.
I'm really confused about this.
You can find commands by using the command palette.
Mac: CmdShiftP
Windows/Linux: CtrlShiftP
Looking for "save all" there, the only match for me is
Window: Save All
Ignore whatever key binding it may show next to it. What is important is the command name, "Window: Save All". You can convert that to a command selector with these steps:
Lowercase everything
Remove the space after :
Replace other spaces with -
This yields your selector: window:save-all.
So your key binding should be:
'ctrl-shift-s': 'window:save-all'
As the command's name suggests, this will save all tabs in all panes in the current window. It should not affect anything in other Atom windows.

Atom IDE - Keybinding

I have disabled the default Atom IDE keybinding for the combination alt + f (In my keymap.cson):
'atom-text-editor':
'alt-f': 'unset!'
How to make it to show [ character when I click alt + f?
Here's how to do this, based on this forum entry:
Open the editor's init.coffee script file (Menu: Atom > Init Script...)
Insert the following text (make sure to use the right indentation:
atom.commands.add 'atom-text-editor',
'custom:insert-bracket': ->
atom.workspace.getActiveTextEditor()?.insertText('[')
Save the file.
Open the keybinding file (Menu: Atom > Keymap...)
Add the following mapping (replacing yours) to call the new command:
'atom-text-editor':
'alt-f': 'custom:insert-bracket'
Save the file.
As a last step, restart Atom.
This worked for me, pressing Alt+f now inserts an opening bracket (which is automatically completed with a closing bracket by Atom).

How to make IntelliJ IDEA insert a new line at every end of file?

How do I make IntelliJ IDEA insert a new line at every end of file,
so that GitHub doesn't complain for example?
Change your Editor settings:
Settings → Editor → General → Ensure line feed at file end on Save
For MAC users:
Preferences > Editor > General > Ensure every saved file ends with a line break
IntelliJ IDEA 2016.3
Approach 1
File > Settings... > Editor > General > Ensure line feed at file end on Save
Approach 2
Help > Find Action... (Ctrl+Shift+A) > type "Ensure line feed" > switch the toggle to ON (using the mouse click or Enter) for "Other: Ensure line feed at file end on Save" line
Possible alternative with a number of handy features is EditorConfig
Just submit an .editorconfig file to your repo
[*]
insert_final_newline = true
And it will work natively not only in Idea, but in all major IDEs (some require a plugin).
Now all team members would have same configuration, eol, eof, and no more tabs vs spaces :)
For Mac Users: IntelliJ Idea version 2020.2
Option1:
IntelliJ Idea -> Preferences -> General -> Ensure an empty line at the end of a file on save
Option2:
⬆️ + ⌘ + A or Or just click on Help from menu bar -> Find Action and then type Ensu and choose Ensure an empty line at the end of a file on save
General -> Save Files For IntelliJ IDEA 2020.
Check the Bottom Right Corner:
In latest versions of IntelliJ, the setting has been renamed to 'Ensure an empty line at the end of a file on save', and it has been moved under Setting>Editor>General>Save Files
This should have been a comment, but I wanted to add the screenshot as well so wrote as an answer.
As Rider (IDEA's cousin for .NET) is driving me crazy, this might be helpful for those writing C# as Ensure line feed at file end on Save alone won't work. It needs
File → Settings → Editor → Code Style → C# → Line Breaks and Wrapping → Line feed at end of file.
I don't remember changing it and I haven't imported any settings for sure, so I guess it's by default disabled.
With the IntelliJ Idea version 2020.3:
Go to File > Sttings > Editor > General > On Save
And then select/deselect "Ensure every saved file ends with a line break"
With the Intellij version 2022.3.1
Preferences (cmd + ,) > Editor > General > Ensure every saved file ends with a line break
Check, apply, and click ok
intellij

How can I find particular file in IntelliJ IDEA?

I need to find test.xml in IntelliJ IDEA. How can I do that? Keyboard shortcut if any?
cmd+shift+o (cmd+shift+n in older versions) leads to the file open dialog (on mac).
You can use * chars as wildcards. If there is more than 1 file with the name, you will see the directory of the file for each entry to help you make the choice.
In Windows:
Ctrl + Shift + N
or
Navigate > File...
My IntelliJ version is,
IntelliJ IDEA 2016.2.4
For me, cmd+shift+n doesn't work.
But,
command + shift + o
works perfectly for me.
The above shortcut will give you the dialog box to search a file.
For more help,
On the main menu, point to Navigate, and then choose Class, File, or Symbol respectively, or use the following shortcuts:
Class: ⌘O
File (directory): ⇧⌘O
Symbol: ⌥⌘O
Source : JetBrains.com
Just hit quickly Shift Shift in a row to reveal a pop-up window to search for everything. You can then start typing to search for files. It's a fuzzy finder, so to match e.g. SomeClassWithALongName.java you can just type: sclwlna (or other letters combination, which has the same order of occurance in the target file name).
Well, this is very frequently asked a question by users who switch OS. I recently switched from Windows to MAC.
To open file shortcuts go as follows
Windows : ctrl+shift+n
MAC : command + shift + o
command + E
It Will Open Recent Files

Is there any shortcut key to align code content in Xcode?

Is there any shortcut key to align code in Xcode? Just like Ctrl+K+D in Visual Studio?
⌃ + i (ctrl + i) is the shortcut key equivalent to Editor > Structure > Re-Indent ( in Xcode 6.4)
This will re-indent the line the cursor is on if no text is selected, or it will re-indent your selection.
1.) Select the text you want to align,
then
2.) press ctrl(^) + I.
If you want more than just indentation Xcode does not yet offer built in code formatting but you can use external tools like Uncrustify to apply a consistent code style.
I described how I last set this up here: http://blog.carbonfive.com/2009/08/07/code-formatting-in-xcode/
Turn on automatic code indentation, then Command+X, Command+V. In other words, cut and paste in place. If your syntax is correct Xcode shouldn't have trouble formatting it (unless you're missing newlines).
There's a menu command:
Edit > Format > Re-Indent
I don't think there's a shortcut on it by default, but you can easily create one in the Key Bindings tab in the Xcode Preferences.
Use default, "COMMAND [" and "COMMAND ]"
Simple!
as #McGordon says, Control + i works on indenting.
(As of Xcode 4.1) its there in the menu at
Editor --> Structure --> Re-Indent
Select the text you want to Align And then press Control(ctrl)+I.
The defaults way Xcode is set up is to use Control + i (also known as ⌃ + i)
If you want to change this then:
Open xcode preferences
Go to Keybindings
Search for "Indent"
Edit and change it to whatever you like.
=> Select All Code by (command + A)
=> Press (control + ]) for indent.