Vim key bindings in Xcode? - xcode

Is there any way to have Vim key bindings in Xcode?

If you still interested in Vim keybinding plugin for Xcode I made one. Here it is.
http://programming.jugglershu.net/softwares/xvim.html
This is currently developed for personal (my) use. So you may feel bad with some lack of implementation. Give me a feed back(feature request) then. I'll add some keybinds if I have enough time.

#pkamb's answer is correct, but slightly out of date. In the Xcode 13 GM, Apple tweaked how to enable Vim keybindings.
To improve access to the Vim Mode, Xcode replaced the Enable Vim key bindings preference with an Edit > Vim Mode menu item. (75491567)

The next best thing is JetBrains' excellent Objective-C IDE AppCode. They have a great Vim plugin called IdeaVIM that is actively maintained.

I use MacVim as editor instead xcode. For code completion i use vim plugin named clang_complete - awesome plugin.

Xcode 13 in 2021 has added native Vim key binding support:
Xcode > Preferences > Text Editing > Editing > Enable Vim key bindings
https://developer.apple.com/xcode/
Vim mode
Many common key combinations and editing modes familiar to Vim users are supported directly within the code editor, using the new bottom bar to show mode indicators.

I myself use and really appreciate Shu's XVim, but for completeness and in case you're still interested, just recently viemu has opened its beta for Xcode, you can check it out at
http://www.viemu.com/blog/2013/05/01/viemu-for-xcode-public-beta-available/

I haven't tried it yet, but there's also $20 ViCiOUS. Right now, I use Shu's XVim plugin (cf. his answer), and recommend it.

ViEmu is finally available for Xcode, check it out. It uses the same vim emulation engine that they are using for their Visual Studio plug-in, so all the polishing that has been added to the product since 2005 is there.
I'm not affiliated to them, but I tested ViEmu and if it was available for Xcode 5, I would have paid for a license.

Try Editor -> Vim Mode in Xcode =>13.
It supports most of the common key bindings but there are exceptions, which is understandable given it's a relatively new feature. Most notably for me is the lack of support for a .vimrc file so if you want that (or just want more features) you can still use https://github.com/XVimProject/XVim2. Judging by the current maintainer it might eventually be made redundant by Xcode's default vim support but it still works well for me.

Related

Anyway to use someone else's neovim/vim keybindings config on VS Code

I have just migrated from Atom, there, I was using Vim-plus but now on VS Code I have switched to neovim, but the problem is, there are many conflicting keybindings.
I tried to change them manually (something like, to close editor, 'Ctrl+c+t', which I mapped to close editor's tab, similarly others).
But I feel that's very unproductive and also can cause strain in my fingers. So any suggestions, or keybindings config of anyone which I can use alongside the NeoVim to increase my productivity. Or any other suggestions how to properly configure my VS Code?
NOTE: I know about the existence of an Atom Keybindings Extension which I honestly don't care, as I was mainly using Vim keybindings there and never bother to learn atom specific keybindings.
If you want VS Code to act like VIM, which is super unique in its interface and keybindings, there is an extension called:
VSCode Vim
VSCode Vim's Marketplace ID: vscodevim.vim
Personally I can't stand the VIM keybindings, so I don't know how great the extension is, but I know people who use it. It has 2.4 million downloads and counting. I also know, because of my buddy, that its not perfect, but it's close.
As with any other extension, don't just install it and hope it's what you wanted "out-of-the-box", this theme is customizable, and requires you to configure it. Make sure you read the README.md, and set it up so you know that it is best suited for your expectations.
If you are already using the VSCode VIM Extension, and you find that you are having conflicting issues using the keybindings associated with it. You can troubleshoot them using the keybindings troubleshooting tool by selecting it from the quick input menu.
Press F1
Type the phrase: "Keyboard Shortcut Troubleshooting"
Select the option "DEVELOPER: Toggle Keyboard Shortcut Troubleshooting Tool"
The tool should open in the console below. The menu might look like gibberish at first, however; the output of the newly opened console should make more sense once you use a familiar keybinding. Make sure that the console window is scrolled to the bottom and opened wide enough so your able to read everything logged. The tool will tell you what is attached to the keybinding your using, so you can see any conflicts that are happening, and what the key is set to do by you, by extensions, and by default.
Make changes to your keybindings.json file as necessary.

Xcode 6.3 shows random files instead of counterparts in assistant editor

I always have my Xcode assistant editor open with counterparts option. It's very handy to have an *.m file opened at standard editor while having corresponding *.h file in assistant editor.
After recent update to version 6.3 Xcode started to show random instead of corresponding headers quite often. Several clicks to "four squares" icon and mode usually brings it back (but it's very annoying).
Do you experience this too? How to fix it?
Note: deleting DerivedData didn't helped
As a workaround, one could use the Reset Editor command with the keyboard shortcut Command+Option+Shift+Z (US keyboard layout). If your keyboard layout doesn't match, you can see the key binding in View->Assistant Editor->Reset Editor. A word of caution, though, this will close any additional Assistant Editors you may have opened (the ones that open by clicking "+").
Another thing I noticed is the bug happens when you are switching Xcode windows or tabs but not when you are switching files within the same tab.
It looks like it's a just bug after all. I filed a bug report rdar://20684654. For a workaround see the accepted answer.
UPDATE: it's fixed in Xcode 6.4 beta

How to use code formatter in Xcode for Swift?

Xcode code formatter Swift
I'm trying to auto format my code written in Swift. It's necessary if you're working in team and using CVS and don't want to merge for hours.
Ctrl+I just makes an indent, but for example i want to have proper space between functions and just all the stuff like in Clang.
The reformat code command in xcode is Ctrl-I, not Cmd-I and it appears to work just fine.
I write a extension app about format Swift code: Swimat
There has two way to install
brew cask install swimat
Clone this repo, build and restart Xcode.
See https://github.com/Jintin/Swimat for more information
In Xcode [Objective-C, Swift]:
1) ⌘+A: highlight code
2) ⌃+I: reformat code
⌃+i: reformat code
Ctrl+i: fix code indentation
Eventhough it has good IntelliSense, xcode sucks at code formatting generally.Wish it was better.
Apple has to spend so much effort in this in order to make it VS level good.
Xcode and Swift 2.0: Per file, Cmd-A then Ctl-I. Ctl-I works for what is highlighted, Cmd-A highlights all. Just be careful not to accidentally delete all your source after the Ctl-A!
SwiftFormat is a popular code formatter for swift. It has an extension for Xcode, which can be accessed from the Editor menu.
Quote from the SwiftFormat readme:
SwiftFormat is a code library and command-line tool for reformatting
swift code on macOS or Linux.
SwiftFormat goes above and beyond what you might expect from a code
formatter. In addition to adjusting white space it can insert or
remove implicit self, remove redundant parentheses, and correct many
other deviations from the standard Swift idioms.
(I know the question says Xcode but)
personally this is another reason to use AppCode alongside Xcode.
While Xcode only fixes the indentation for Swift code, as mentioned in the question, AppCode at least handles spaces too. Somewhat relatedly it removes indents from empty lines by default.
AppCode's Swift support definitely isn't perfect yet and you still need to check the code in both, but it's still worth it because of better navigation and things like this.
The command in xcode is ctrl-i to format code, not cmd-i.
I would suggest using sublime text with the swift plugin, which will allow you to hotkey the code formatting.
https://sublime.wbond.net/packages/Swift
Select lines, then press Ctrl+I.

"Edit in Emacs..." input manager for OSX

Is there anything like the input manager for TextMate (at the very end of the page) which allows to edit from any Cocoa text edit controls in TextMate? It basically installs a menu "Edit in TextMate...". I tried to search for it, but I have not find anything. I think it would be very nice to have such a feature.
Thanks.
I haven't tried it (I keep meaning to), but QuickCursor is supposed to do that (for any editor, not just Emacs).
As noted in the comment, this requires ODB support, for which EditorClient can be used.
Aquamacs just added ODB support. You should be able to try it in a nightly build.
I don't know of one, but it should be fairly straightforward to hack one. You'd simply want to call emacslient with the path.

Code Formatting In Xcode

Is there anyway to format the code in Xcode like you can do in Eclipse or Netbenas. I have not found any option in menu, is there any hot key for this or its simply not in Xcode?
In Xcode 4, it's been moved to Editor > Structure > Re-Indent command (and has a default shortcut of CTRL+I ).
Select some text and then: Edit->Format->Re-Indent
You can bind this to a hotkey in the preferences.
Well, first of all welcome to 2021. I know it's a very old question but still people like me visit this and for those people, I'm putting out multiple available solutions in one answer that worked for me after 2 hours of research and testing.
Re-Indent
As everyone else is suggesting, it's one of the default solution if you just want to re-indent the lines of code:
Goto: Editor -> Structure -> Re-Indent or use shortcut control + I
Using Xcode Extensions
There are 2 extensions I found and used which works charmingly for me.
1) SwiftFormat: mentioning the steps to install the xcode extension here from the readme.
Like the command-line tool, you can install the SwiftFormat for Xcode
extension application via Homebrew. Assuming you already have Homebrew
installed, type:
$ brew install --cask swiftformat-for-xcode
open the SwiftFormat for Xcode.app that you just installed. there you can change the rules of formatting.
After that, open the system preference -> extensions -> xcode source editor and mark the checkbox for SwiftFormat.
Restart the Xcode
Voila! You're good to go. you can use three options. format selected code, format entire file OR just lint file.
2) Swimat: (it is already mentioned in one of the answer here by #Guy Daher) steps to install from readme
Install via homebrew-cask
Homebrew latest version
brew install --cask swimat
Similar to above extension installation, open the Swimat.app if it does not open then try to sove it from system preferences -> security & privacy -> open swimat app
after opening, click on install in ./bin folder to make it work globally (not sure about this step but I just did it)
Restart Xcode
now you can goto editor -> swimat -> format
Bingo! You're good to go.
Additional uninvited help
You can simply set the keybinding to use any of this command. i.e. I've set the android studio default command to reformat the code as option + command + L
ps If anyone else found a better extension, please let me know in comment, I'll add it to the list.
Other than re-indentation (Edit > Format > Re-Indent), not really. However, Xcode does have support for scripts (the menu to the right of the Window menu), so you could conceivably write a script that formats your code how you like it.
Edit: here are some links that touch on this subject:
http://hackertoys.com/2008/09/18/adding-a-code-beautifier-script-to-xcode (dead link)
http://8020world.com/jcmendez/2006/11/geeky-stuff/software/adding-a-script-menu-to-xcode-to-reformat-code-using-astyle (dead link)
Here are the shortcuts, to format the code in Xcode
1.Format entire code (entire class/controller)
select the entire code and press ⌘+| on mac to format your code.
2.Format particular block of code
select the code and press
⌘+] for right move and ⌘+[ for left move
Note : as per #JavierGiovannini sugesstion you can do using Editor Menu option
3.Select code --> Go to Editor --> Structure --> Re-Indent
My personal favorite code formatter is Uncrustify.
It has many, many options, so I also suggest you download UniversalIndentGUI, a GUI to help set Uncrustify's behavior to your liking.
XCode 8 Extensions
Another option is to use an extension like Swimat. Does the job for me. However, the caveat is that it does not format on save due to the restrictive Xcode Extension APIs, but they're trying to find a workaround.
Not saying this is best approach, but for completeness, if you cut and then paste the code back in, Xcode will automatically format it for you.
In v.4 you can make some adjustments through xCode preferences...
Fix code indentation in Xcode
^-I (control - capital i)
: format the current line
⌘-A + ^-I
: format the whole code file
Try this solution: ⌘A [⌘K ⌘F]

Resources