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
Terminal Basics #9: Editing Files in Linux Terminal
Learn about editing text files in the Linux terminal using the beginner friendly Nano editor in the second last chapter of this series.
You have learned a bunch of file operations so far in this Terminal Basics series. You learned to create new files, delete existing ones, and copy and move them. It is time to take it to the next level. Let’s see how to edit files in the Linux terminal. If you are writing bash shell scripts, you can use the GUI text editors like Gedit and run them in the terminal. But at times, you’ll find yourself in a situation where you have to edit existing files in the terminal itself. For example, modifying config files located in the /etc directory. As a desktop Linux user, you could still use GUI editors for editing config files even as root. I’ll show it to you later. However, knowing how to edit files in the command line is better.
Editing files in Linux terminal
You may use the cat command if you just have to add a few lines at the bottom of an existing file. But in order to properly edit a file, you’ll need a proper text editor. There is simply no shortage of terminal-based text editors in Linux. Vi, Vim, Nano, Emacs are just a few of the most popular ones out there. But here is the thing. All of them have a learning curve involved. You don’t have the comfort of the GUI. You don’t have menus to interact with the editor with your mouse. Instead, you have to use (and remember) keyboard shortcuts. I find Nano to be a good starting point for new users. It is the default text editor in Ubuntu and many other Linux distributions. Of course, there is a learning curve, but it is not as steep as that of Vim or Emacs. It keeps on displaying the most relevant keyboard shortcuts at the bottom. This helps you navigate even if you don’t remember the exact shortcut. For this reason, I’ll be covering the absolute basics of the Nano editor here. You’ll learn all the essentials you need to know to start using Nano for editing files in the Linux terminal.
Using Nano editor
Nano can be used to edit text files, script files, program files etc. Please remember that it is not a word processor and cannot be used to edit docs or PDF files. For simple text editing of conf files, scripts, or text files, Nano is a great choice.
I’ll be using a text file named agatha_complete.txt. It consists of the names of all Agatha Christie’s books under her name. You can download it from this link if you plan to follow the steps on your system.
How to edit a text file in my terminal
I’m using Linux mint and using the vi command to create text files, now that I created a text file and saved it. How do I get back into to edit the text file again?
6 Answers 6
it, will open up a text editor to edit your file.
Here, you can edit your file in the terminal window.
Open the file again using vi. and then press » i » or press insert key ,
and write the following command
Open the file again using vi. and then press the insert button to begin editing it.
could also run ‘vimtutor’ to learn how to use vi/vim. This of course assumes you have vim (vi’s big brother) if you don’t run ‘sudo apt-get install vim’
If you are still inside the vi editor, you might be in a different mode from the one you want. Hit ESC a couple of times (until it rings or flashes) and then «i» to enter INSERT mode or «a» to enter APPEND mode (they are the same, just start before or after current character).
If you are back at the command prompt, make sure you can locate the file, then navigate to that directory and perform the mentioned «vi helloWorld.txt». Once you are in the editor, you’ll need to check the vi reference to know how to perform the editions you want (you may want to google «vi reference» or «vi cheat sheet»).
Once the edition is done, hit ESC again, then type :wq to save your work or :q! to quit without saving.
For quick reference, here you have a text-based cheat sheet.