Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 )

share|improve this question
up vote 1 down vote accepted

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 (you can use any other name) and make it executable by running:

chmod +x

To use it:

./ <command>


./ ls -l


  • 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.
  • 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
share|improve this answer
'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 '12 at 8:44
updated the answer. – green Oct 11 '12 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 '12 at 11:03
did you create the temp folder in your home directory? – green Oct 11 '12 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 '12 at 17:18

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
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

share|improve this answer
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 '12 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 '12 at 8:58

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.