16

When installing the TextWrangler in OSX you also get an edit command which allows you to open any text file from the command line.

Is it possible to have a similar functionality in Ubuntu to type some command on the terminal to open a file in a specific text editor (say Kate)?

6 Answers 6

26

You can open (Up to my knowledge) any of the editors like this:

NAME_OF_EDITOR FILENAME

gedit filename (Ubuntu)
kate filename (Kubuntu)
bluefish filename
kwrite filename
libreoffice filename

You can even open a web page the same way
firefox filename.html
chrome filename.html
banshee filename.ogg or .mp3

You can see the tendency here..

3
  • Actually it should be gedit filename & otherwise the terminal remains unusable. The same thing happens with kate but, even with the & it makes the terminal unusable because it starts spitting debug (or whatever) information to the terminal. Is there a way to avoiding the & in the end at all? As I do with edit from TextWrangler? Apr 7, 2011 at 15:56
  • Nevermind, the answer from Lekensteyn is (at least closer) to what I was looking for. Apr 7, 2011 at 16:18
  • 1
    True, you can add the & or even add 2>/dev/null to it to not output error and stuff in the console if you want to keep using it. Apr 7, 2011 at 16:29
12

To open a file using kate, you can run something like:

kate filename

This might show some messages like:

kate(3702)/kdecore (services) KMimeTypeFactory::parseMagic: Now parsing  "/usr/share/mime/magic"
kate(3702)/kdecore (services) KMimeTypeFactory::parseMagic: Now parsing  "/home/user/.local/share/mime/magic"
Bus::open: Can not get ibus-daemon's address. 
IBusInputContext::createInputContext: no connection to ibus-daemon

To remove these messages, redirect the error output stream to /dev/null:

kate filename 2>/dev/null

If you want to continue using the same terminal, add an & after the command:

kate filename 2>/dev/null &

If you want to run edit filename to open it, you could create a bash function in your ~/.bashrc file. Add the next code to your ~/.bashrc file:

edit() { kate "$@" 2>/dev/null & }
3
  • This gets closer to what I was looking for. Thanks for actually running the commands and see the problems that would arise with the most obvious solutions. I guess what I'll do is to create a new command which I can just call as edit filename to run the whole kate filename 2>/dev/null &. Apr 7, 2011 at 15:59
  • 1
    @Juan: I've added an example for doing that. The $@ is needed in case you need to pass additional options like line number. As an alternative, you can tweak this function to support edit filename linenumber.
    – Lekensteyn
    Apr 7, 2011 at 16:34
  • You have a bug in your answer, and I can't seem to edit it. You forgot the quotes around $@ (should be "$@"), which means that your script will fail with filenames what contain spaces. Apr 7, 2011 at 18:34
3

If you prefer to use the command edit in Ubuntu also because you are used to do so you could also define an alias for your favourite editor like for Kate:

alias edit='kate'

To make this alias permant just add this line to ~/.bash_aliases.

2

If you don't have any graphics environment and you are running on console you can always use:

vim foo.txt
nano bar.txt
pico foo.html
emacs bar.xml
...

and so on falls back to the first answer..

1
  • 1
    "emacs -nw" or "xemacs -nw" if you want VT100 (x)emacs even inside a windowed environment.
    – user11553
    Apr 7, 2011 at 16:18
1

You can use CLI command for the desired editor to open and edit files.For example gedit in gnome or kate in KDE.

Just type:

kate filename

to open file in kate.

1

I sometimes use a classical terminal where mcedit is my prefered editor, and often like to pass a line number, to correct a program/script.

To uniformely call them edit source.sh 123 I wrote this script, which I placed as 'edit' in the path:

#!/bin/bash
#   - edit a file using mcedit or gedit, depending on X11 or console invoking.
#   - jump to specified line, if any.

Xedit=/usr/bin/gedit

if [[ $TERM = "linux" ]]; then
    if [ $# -eq 1 ]; then
        mcedit $1
        else if [ $# -eq 2 ]; then
#           echo "edit invoked\t/usr/bin/mcedit +$2 $1" >> /tmp/edit.log
            /usr/bin/mcedit +$2 $1
            else if [ $# -eq 0 ]; then
                /usr/bin/mcedit
            fi
        fi
    fi
    else if [[ $TERM = "xterm" ]]; then
        # scheint nicht zu helfen
        # LANGUAGE=C
        export LC_ALL=C
        if [ $# -eq 1 ]; then
            $Xedit $1
            else if [ $# -eq 2 ]; then
#               echo "edit invoked\t/usr/bin/scite -open:$1 -goto:$2" >> /tmp/edit.log
                # $Xedit -open:$1 -goto:$2
                $Xedit +$2 $1 
                else if [ $# -eq 0 ]; then
                    $Xedit
                fi
            fi
        fi
    fi
fi

Use see old debug instructions from when I used scite, not gedit, as graphical editor.

Something, which doesn't work this way, is opening of multiple files like this:

 edit *.html

if there is more than one html-File, so the pattern gets expanded to multiple files.

Valid invocations are:

 edit 
 edit foofile 
 edit foofile 123

from X or terminal.

You must log in to answer this question.

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