- Open application to edit text files from the command line
- 6 Answers 6
- How to open, create, edit, and view a file in Linux
- GUI text editors
- NEdit
- Geany
- Gedit
- Terminal-based text editors
- pico
- nano
- vim
- emacs
- Redirecting command output into a text file
- Creating an empty file with the touch command
- Redirecting text into a file
- Redirecting to the end of a file
- Related information
Open application to edit text files from the command line
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
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..
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?
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.
To open a file using kate , you can run something like:
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 :
If you want to continue using the same terminal, add an & after the command:
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:
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: 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 .
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.
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:
To make this alias permant just add this line to ~/.bash_aliases .
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..
You can use CLI command for the desired editor to open and edit files.For example gedit in gnome or kate in KDE.
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:
if there is more than one html-File, so the pattern gets expanded to multiple files.
edit edit foofile edit foofile 123
How to open, create, edit, and view a file in Linux
One thing GNU/Linux does as well as any other operating system is give you the tools you need to create and edit text files. Ask ten Linux users to name their favorite text editor, and you might get ten different answers. On this page, we cover some text editors available for Linux.
GUI text editors
This section discusses GUI (graphical user interface) text editing applications for the Linux windowing system, X Windows, more commonly known as X11 or X.
If you are coming from Microsoft Windows, you are no doubt familiar with the classic Windows text editor, Notepad. Linux offers many similar programs, including NEdit, gedit, and geany. Each of these programs are free software, and they each provide roughly the same functionality. It’s up to you to decide which one feels best and has the best interface for you. All three of these programs support syntax highlighting, which helps with editing source code or documents written in a markup language such as HTML (hypertext markup language) or CSS (cascading style sheets).
NEdit
NEdit, which is short for the Nirvana Editor, is a straightforward text editor that is similar to Notepad. It uses a Motif-style interface.
The NEdit homepage is located at https://sourceforge.net/projects/nedit/. If you are on a Debian or Ubuntu system, you can install NEdit with the following command:
sudo apt-get install nedit
For more information, see our NEdit information page.
Geany
Geany is a text editor that is a lot like Notepad++ for Windows. It provides a tabbed interface for working with multiple open files at once and has nifty features like displaying line numbers in the margin. It uses the GTK+ interface toolkit.
The Geany homepage is located at http://www.geany.org/. On Debian and Ubuntu systems, you can install Geany by running the command:
sudo apt-get install geany
Gedit
Gedit is the default text editor of the GNOME desktop environment. It’s a great, text editor that can be used on about any Linux system.
The Gedit homepage is located at https://wiki.gnome.org/Apps/Gedit. On Debian and Ubuntu systems, Gedit can be installed by running the following command:
sudo apt-get install gedit
Terminal-based text editors
If you are working from the Linux command line interface and you need a text editor, you have many options. Here are some popular ones:
pico
pico started out as the editor built into the text-based e-mail program pine, and it was eventually packaged as a stand-alone program for editing text files. («pico» is a scientific prefix for small things.)
The modern version of pine is called alpine, but pico is still called pico. You can find more information about how to use it in our pico command documentation.
On Debian and Ubuntu Linux systems, you can install pico using the command:
sudo apt-get install alpine-pico
nano
nano is the GNU (GNU’s Not Unix) version of pico and is essentially the same program under a different name.
On Debian and Ubuntu Linux systems, nano can be installed with the command:
vim
vim, which stands for «vi improved,» is a text editor used by millions of computing professionals all over the world. Its controls are a little confusing at first, but once you get the hang of them, vim makes executing complex editing tasks fast and easy. For more information, see our in-depth vim guide.
On Debian and Ubuntu Linux systems, vim can be installed using the command:
emacs
emacs is a complex, highly customizable text editor with a built-in interpreter for the Lisp programming language. It is used religiously by some computer programmers, especially those who write computer programs in Lisp dialects such as Scheme. For more information, see our emacs information page.
On Debian and Ubuntu Linux systems, emacs can be installed using the command:
sudo apt-get install emacs
Redirecting command output into a text file
When at the Linux command line, you sometimes want to create or make changes to a text file without actually running a text editor. Here are some commands you might find useful.
Creating an empty file with the touch command
To create an empty file, it’s common to use the command touch. The touch command updates the atime and mtime attributes of a file as if the file’s contents had been changed — without actually changing anything. If you touch a file that doesn’t exist, the system creates the file without putting any data inside.
The above command creates a new, empty file called myfile.txt if that file does not already exist.
Redirecting text into a file
Sometimes you need to stick the output of a command into a file. To accomplish this quickly and easily, you can use the > symbol to redirect the output to a file.
For instance, the echo command is used to «echo» text as output. By default, this goes to the standard output — the screen. So the command:
The above command prints that text on your screen and return you to the command prompt. However, you can use > to redirect this output to a file. For instance:
echo "Example text" > myfile.txt
The above command puts the text «Example text» into the file myfile.txt. If myfile.txt does not exist, it is created. If it already exists, its contents will be overwritten, destroying the previous contents and replacing them.
Be careful when redirecting output to a file using >. It will overwrite the previous file’s contents if it already exists. There is no undo for this operation, so make sure you want to completely replace the file’s contents before you run the command.
Here’s an example using another command:
The above command executes ls with the -l option, which gives a detailed list of files in the current directory. The > operator redirects the output to the file directory.txt, instead of printing it to the screen. If directory.txt does not exist, it is created first. If it already exists, its contents will be replaced.
Redirecting to the end of a file
The redirect operator >> is similar to >, but instead of overwriting the file contents, it appends the new data to the end of the file. For instance, the command:
The above command takes the output of ls -l and adds it to directory.txt. If directory.txt does not exist, it is created first. If it already exists, the output of ls -l is added to the end of the file, one line after the existing content.