Keybinding a terminal command?


#1

Hey I was wondering if it is possible to keybind a command that I run in the terminal?

Second question, if the above question is possible is there a way to get the current file in focus in the editor and pass it into the command.

What I’m trying to run:

prettier --write [file_in_focus]

I want to run that command when a certain keybinding is triggered. Is this possible? Basically I want a shortcut so I don’t have to keep typing this command over and over, and I need it to know what file is in focus when the keybind is triggered. Thanks!


#2

You can use init script to add the following command

var tabs = services.tabManager
services.commands.addCommand({
    name: "prettier",
    bindKey: {win: "F7", mac: "F7"},
    isAvailable: function() {
         var editor = tabs.focussedTab && tabs.focussedTab.editor;
         return editor && editor.type == "ace" && editor.ace.session.syntax == "javascript"
    },
   exec: function(e) {
         var tab = tabs.focussedTab && tabs.focussedTab;
         var path = tab.path
         services.vfs.execFile("bash", {args: ["-c", "prettier --write \"$1\"", path], function() {
               // show error if any
         })
   }
})

but a better approach is to use project settings to configure the custom formatter for javascript to prettier

Screen Shot 2017-07-27 at 12.25.43 PM.png


Add support for Prettier JS formater
#3

Thank you so much! I’m amazed this is built into c9. The second option works great!


#4

im at this same stage. i have prettier globally installed and working. 1 - not sure how to implement an init script - or where to put it. 2 - tried typing “prettier --write “$file”” into the custom code formatter, that didn’t work. is there a deeper explanation of this stuff anywhere?


#5

This can happen, if the PATH variable is different in interactive and non-interactive terminals.
Try running which prettier in the terminal, and use the full path in the custom code formatter.


#6

i kind of get what you mean, but i must admit, i’m still a relative novice. i think i’ve got something working though. i disabled the option to format on save, and disabled jsBeatify, then the next option - custom code formatter i used prettier --write "$file". now when i hit ctrl+shift+B it appears to be using prettier. i test by running the same command from the command line and it returns grey data - the name of file and the ms it took to do its thing. now when i prettier on a file that gets changed that same data is white in my terminal. i guess thats a way prettier tells you if it did something. long story short. i globally installed prettier to my workspace and configured the options like i mentioned above, in my case it wont work on save but all i have to do is the shortcut. beats navigating to the dir and running it manually. cool stuff c9.


#7

I think the issue for you could have been coming from “Use Built-in JSBeautify as Code Formatter” which you mentioned to have disabled.

I disabled this, left the “Format Code on Save” on, then input npx prettier --write "$file" in the “Custom Code Formatter”, with node 8 of course, and saving the file runs the prettier.


#8

Hey @harutyun, can I apply prettier to a specific folder so that I would be able to format the files of that folder only.
If there is a way tell me. :slight_smile: