After hours of searching and trying different gedit plugins I did not manage to add a session saver to gedit to open some files in Ubuntu 14.04 (see here).

So, I am trying to create a permanent bash alias, editing ~/.bashrc to add it.

The alias is:

alias a="
         cd /home/konstantinosubuntu/Desktop/abc && gedit test1.c
      && cd /home/konstantinosubuntu/Desktop/xyz && gedit test2.c"

Then I use . ~/.bashrc to reload the changes I made to the aliases. But this alias only opens the first file (here, test1.c) and if I interchange the lines 2 and 3 it opens test2.c. That means there is no problem with the path. Instead, the error I get is at the second && which is bash: syntax error near unexpected token `&&'. Why is this error appearing? What alias should I use to open multiple files at once?


On how to write this alias:

The alias does not need to open the files separately - gedit can take multiple files.

alias a="gedit ~/Desktop/abc/test1.c ~/Desktop/xyz/test2.c"

This also replaces the path of your home directory by ~. If it should work for other users, use ~konstantinosubuntu instead of the ~.

To make the command independent of the terminal, so that it keeps running if you close the terminal, you can add & to run it in the background, and nohup to make it not exit when the terminal closes:

alias a="nohup gedit ~/Desktop/abc/test1.c ~/Desktop/xyz/test2.c &"

On the syntax of your example:

The problem with the && is that the && needs to be put at the end of the previous line. Otherwise the end of the line is just the end of the command - no problem so far - and the start of the next line has a "&&" that makes no sense in this place.

alias a="
         cd /home/konstantinosubuntu/Desktop/abc && gedit test1.c &&
         cd /home/konstantinosubuntu/Desktop/xyz && gedit test2.c"

As alternative, the whole alias could be put in one line. Or, very similar, a line continuation with \ at the end of the line could be used. That would be writing it as two lines, but executing it as one line, logically.

You note in your comments that the first gedit needs to be closed before the second file is opended, and that you do not want this. The command gedit file.txt runs gedit in foreground if it's the first instance. If gedit is already running, it opens a new tab in that instance, and exits. To make the alias open both files in gedit at the same time, run the first instance in background with &. That does not work together with &&, that needs to be removed:

alias a="
         cd /home/konstantinosubuntu/Desktop/abc && gedit test1.c &
         cd /home/konstantinosubuntu/Desktop/xyz && gedit test2.c"

(Interesting how it is only different by one character, but very different in meaning.)

In case you intended to have multiple current working directories in some way associated with the running gedit by using the cd commands, that can not work. A process has exactly one current working directory. It will be the one of the first gedit instance here.

Just to be clear after discussing these examples, I think you should better use something like in the first section, opening one gedit with multiple files at once.

  • Thanks, the first method worked. But, when I corrected the syntax of the alias I had to close the first text file and then the second one appeared in a new window. – mgus Aug 16 '14 at 22:24
  • Hmm... I do not understand what you mean. Which commands or alias did you use? – Volker Siegel Aug 16 '14 at 22:32
  • I just added a \ at the end of the second line. – mgus Aug 16 '14 at 22:34
  • Ok; gedit file.txt runs in foreground if it's the first instance. If gedit is already running, it opens a new tab in that, and exits. Try to run the alias when there is already a open gedit with some other file. If you want to make the alias work like that, the first instance needs to run in background with &. That does not work together with &&, so remove that. – Volker Siegel Aug 16 '14 at 23:12
  • OK, I also prefer the 1st method. But why do I have to leave an open terminal as long as files are opened in gedit? Now, if I terminate the process gedit closes too. Is there any workaround to deal with this? – mgus Aug 17 '14 at 7:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.