15

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

Juan A. Navarro
  • 255
  • 1
  • 2
  • 8

6 Answers6

25

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

Luis Alvarado
  • 209,003
  • 167
  • 543
  • 707
  • 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? – Juan A. Navarro Apr 07 '11 at 15:56
  • Nevermind, the answer from [Lekensteyn](http://askubuntu.com/questions/33950/open-application-to-edit-text-files-from-the-command-line/33954#33954) is (at least closer) to what I was looking for. – Juan A. Navarro Apr 07 '11 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. – Luis Alvarado Apr 07 '11 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 & }
Lekensteyn
  • 171,743
  • 65
  • 311
  • 401
  • 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 &`. – Juan A. Navarro Apr 07 '11 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 07 '11 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. – Scott Severance Apr 07 '11 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.

Takkat
  • 140,996
  • 54
  • 308
  • 426
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..

topless
  • 7,255
  • 9
  • 37
  • 43
  • 1
    "emacs -nw" or "xemacs -nw" if you want VT100 (x)emacs even inside a windowed environment. –  Apr 07 '11 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.

Pedram
  • 5,621
  • 3
  • 30
  • 38
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.

user unknown
  • 6,447
  • 3
  • 34
  • 63