Open application to edit text files from the command line
Solution 1
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 & }
Solution 2
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..
Solution 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
.
Solution 4
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..
Solution 5
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.
Related videos on Youtube
Bilal Mujeeb
Academic background in computer science, currently working in software analysis/testing in industry. I'm a very enthusiastic TeX/LaTeX user, most days. I also run a small site for LaTeX beginners in spanish: LaTeX Fácil.
Updated on September 18, 2022Comments
-
Bilal Mujeeb over 1 year
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)?
-
Bilal Mujeeb about 13 yearsActually it should be
gedit filename &
otherwise the terminal remains unusable. The same thing happens withkate
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 withedit
from TextWrangler? -
Bilal Mujeeb about 13 yearsThis 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 wholekate filename 2>/dev/null &
. -
Admin about 13 years"emacs -nw" or "xemacs -nw" if you want VT100 (x)emacs even inside a windowed environment.
-
Bilal Mujeeb about 13 yearsNevermind, the answer from Lekensteyn is (at least closer) to what I was looking for.
-
Luis Alvarado about 13 yearsTrue, 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.
-
Lekensteyn about 13 years@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 supportedit filename linenumber
. -
Scott Severance about 13 yearsYou 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.