2

I want to open the output of terminal commands in sublime2.

e.g. when I do $ list , it lists all files/folders in terminal. I want that when I run this command a temporary text file should be created and output is opened in sublime2 editor. I have no intention to save it. I just want to open output in sublime2.

I've seen that you can open in textmate in Mac by piping out the output.

$ls | mate ( I am not sure about the syntax )

2 Answers 2

2

You can use a very simple script to do this.

Here is the script:

#! /bin/bash

"$@" > $$.txt
sublime-text $$.txt
rm $$.txt

Save it as sublime.sh (you can use any other name) and make it executable by running:

chmod +x sublime.sh

To use it:

./sublime.sh <command>

Example:

./sublime.sh ls -l

Explanation:

  • Line 1 is the shebang, which just specifies the interpreter.
  • Line 3 runs the command that you passed and saves it in a file named $$.txt. $$ returns the PID of the terminal you're using (feel free to use any other file name).
  • Line 4 opens the file in sublime-text
  • Line 5 deletes the file, since you don't want to save the file permanently.

Update: You can also put it in your ~/.bashrc file to access it easily.

  • Open a terminal.
  • Go to home folder.
cd
  • Open .bashrc file.
gedit .bashrc
  • Copy the following at the end of the file.
function sublime()
{
    "$@" > ~/$$.txt
    sublime-text ~/$$.txt
    rm ~/$$.txt
}
  • Run the following command to refresh:
. .bashrc
  • Done!! Now use it:
sublime ls -l
10
  • 'I am new to ubuntu, so pardon my ignorance'.How can I make an entry in environment variable for this .sh file will be available in any directory (even after restart). I dont want to give path everytime.
    – Jashwant
    Oct 11, 2012 at 8:44
  • updated the answer.
    – green
    Oct 11, 2012 at 10:37
  • The command to open to sublime in my ubuntu is sublime. So, I did this function sublimec() { "$@" > ~/tmp/$$.txt sublime ~/tmp/$$.txt rm -rf ~/tmp/$$.txt } I added a /tmp because it was saying permission denied, and thus I created a /tmp folder with permission for everyone. But, when I run sublimec ls , it continuously prints permission denied message
    – Jashwant
    Oct 11, 2012 at 11:03
  • did you create the temp folder in your home directory?
    – green
    Oct 11, 2012 at 15:45
  • ofcourse, I've created it and thus changed the permission too. Strange thing is that , it creates a <rand_no>.txt file in tmp folder along with the output of command. But whenever I run the command, the error shows up continuously (i.e. the error line is printed again and again like an infinite loop)
    – Jashwant
    Oct 11, 2012 at 17:18
1

I have used this method in the past to achieve what you want (I think)

ls -lrt > tmp.txt && gedit tmp.txt

obviously you need to change the reference to gedit so that it uses your prefered editor, in this case sublime.

My question would be what are you trying to achieve with this? It sounds like you are trying to record the output of your terminal sessions. If this is the case the you can use the "script" command

user@machine $ script ~/temp/terminal.txt

just type exit to quit recording

user@machine ~/temp $ exit
exit
Script done, file is /home/user/temp/terminal.txt

This should now mean terminal.txt contains a complete log of that session hope this helps

2
  • what does this -lrt option do ? If I change your command for sublime like ls -lrt > tmp.txt && sublime tmp.txt it gives permission denied in directory where I dont have permission to create file. I dont want to do a sudo for that. I want to keep it simple.
    – Jashwant
    Oct 11, 2012 at 8:47
  • -lrt is an option for the ls command it just orders the output, you can prefix the tmp.txt file location to a directory where you have write permission, such as ~/tmp.txt
    – apacheuk
    Oct 11, 2012 at 8:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .