terminal command for running sublime text 3 from on ubuntu - linux

I've installed a new dev machine using Ubuntu 14.02. I have also installed all the relevant software. php/apache2/sublime/composer etc.
I'd like to be able to open files with a sublime or subl command in the terminal, but can't seem to find the command to point things correctly.
My sublime executable resides here...
/opt/sublime_text/sublime_text
in /usr/bin/subl I have the command
#!/bin/sh
exec /opt/sublime_text/sublime_text "$#"
I just can't find the command in the terminal to make it happen. The sublime documentation points to mac instructions, but I'm too newby to translate.
Any suggestions?

$# in a shell script basically copies all arguments given to the shell script (after the name of the script) and places them at that point during execution of the script.
For example,
if you were to run subl test.txt, it would be as though you are running exec /opt/sublime_text/sublime_text "test.txt".
Now, /opt/sublime_text/sublime_text --help or subl --help gives us help text which shows the usage as well.
Sublime Text build 3065
Usage: sublime_text [arguments] [files] edit the given files
or: sublime_text [arguments] [directories] open the given directories
Arguments:
--project <project>: Load the given project
--command <command>: Run the given command
-n or --new-window: Open a new window
-a or --add: Add folders to the current window
-w or --wait: Wait for the files to be closed before returning
-b or --background: Don't activate the application
-h or --help: Show help (this message) and exit
-v or --version: Show version and exit
Filenames may be given a :line or :line:column suffix to open at a specific
location.
If you pass more than one parameter, then all of them are also treated the same. So, if you ran subl file1.txt file2.cpp file3.html then it'd open all 3 of those files.

As per Mathias comment in my question I found the answer here...
How can I open Sublime Text 2 files from the command line in linux to a tab, not a new window

Related

Open sublime text file at a line number using command line

I'm trying to open a file using sublime text 3, and I would like to open it with the cursor at a concrete line number.
I've been checking subl --help but I could find no option for a concrete line. Therefore I'm just using:
subl filename
Is there any option to do it?
You can use :line to open at the given line:
subl file.txt:345
In my version of sublime-text 3, subl --help does give me the indication (last line):
subl --help
Sublime Text build 3143
Usage: sublime_text [arguments] [files] edit the given files
or: sublime_text [arguments] [directories] open the given directories
Arguments:
--project <project>: Load the given project
--command <command>: Run the given command
-n or --new-window: Open a new window
-a or --add: Add folders to the current window
-w or --wait: Wait for the files to be closed before returning
-b or --background: Don't activate the application
-h or --help: Show help (this message) and exit
-v or --version: Show version and exit
Filenames may be given a :line or :line:column suffix to open at a specific location.

Shortcut Command "ed" to START Sublime Text

I'm used to creating the following ed.cmd file on my Windows PATH which launches my favorite editor:
START "Editor" "C:\Program Files\Sublime Text\sublime.exe" %1
I'd like to do the same thing in Linux but run up against a few challenges:
ed seems to be reserved/exist. I can compromise on this an use edi if need be.
I need to launch the editor asynchronously (as in START in Windows). Typing subl myfile.js from the console "interrupts" my console session until sublime is closed again.
I don't know how the .cmd file equivalent (in Bash) should look
So I'd be grateful for a bash script to do the above and instructions on how to install it on my path so it's globally accessible.
ed isn’t reserved; it just already exists. You can prioritize your own script by listing its directory earlier in $PATH than /usr/bin (or wherever ed is – see command -v ed).
It’d be fun to use ed for this, so, in your shell of choice:
$ cd
$ mkdir -p .bin
$ ed
i
#!/bin/sh
subl "$#" &
.
wq .bin/ed
$ chmod +x .bin/ed
where $ is the prompt and everything else is something you type. Now just put PATH=~/.bin:$PATH in your .profile.
You could also make it a function in your .bashrc. That’s probably cleaner.
ed() {
subl "$#" &
}

How can I open Sublime Text 2 files from the command line in linux to a tab, not a new window

I have ST2 setup so that I can do 'sublime file.txt' and it will open in a ST2 window. But how can I make it open in a new tab in the currently open window?
Try Sublime command line help
subl --help
Sublime Text 2 Build 2217
Usage: subl [arguments] [files] edit the given files
or: subl [arguments] [directories] open the given directories
or: subl [arguments] - edit stdin
Arguments:
--project <project>: Load the given project
--command <command>: Run the given command
-n or --new-window: Open a new window
-a or --add: Add folders to the current window
-w or --wait: Wait for the files to be closed before returning
-b or --background: Don't activate the application
-s or --stay: Keep the application activated after closing the file
-h or --help: Show help (this message) and exit
-v or --version: Show version and exit
--wait is implied if reading from stdin. Use --stay to not switch back
to the terminal when a file is closed (only relevant if waiting for a file).
Filenames may be given a :line or :line:column suffix to open at a specific
location.
After you have opened a new window the subsequent files should be added there as per the default behavior.
This works on Ubuntu 12.04:
subl filename
or
subl -n filename (if you want it to open in a seperate window)
subl fileName
and if you want to open a new file use
subl -n new_file_name
another option is:
to open file.
[sublime index.html script.js styles.css]
to open a folder [ sublime folderName ]

Execute a command in a new cygwin console

I want to do something similar to this using cmd in windows:
start dir c:\
A new console should open with the output of dir c:.
For Cygwin i tried this:
cmd /c start bash 'ls c:\\'
The first part will open a new console, but I dont know how to output the result in the new console.
You can use cygstart to start a program in a new console. Or run it in one of Cygwin's other terminals: mintty, rxvt(-unicode), xterm.
cygstart is good, but not really meant for what is being asked. You can think of cygstart <filename> as doing whatever would happen if you double-clicked something in Windows Explorer -- which means it can open files in the default program as well as start executables. However, both cygstart ... and cmd /c start ... will lose your nice terminal environment, so I'd recommend using something that starts a new terminal window, such as run mintty.
For example:
man ()
{
run mintty --title="man $*" bash --norc -c "command man $#"
}
would open man pages in new windows so that you can view them while still working in your current window.

Gnome-Terminal, how to start in a different directory?

Whenever I start my console gnome-terminal in Ubuntu, it starts in the home directory. How can I make it start in a different directory say ~/myfolder?
I tried to write cd ~/myfolder in ~/.profile but nothing happens.
If you start gnome-terminal like gnome-terminal --working-directory=myfolder it will start with the working directory at ~/myfolder so you could add a new entry to your menu to use that command instead of the other one.
I did this way - with script:
open 3 tabs in the same window size 170x40, each "tab" starts in a different directory.
gnome-terminal --geometry=170x40 --working-directory=myfolder1 \
--tab --working-directory=myfolder2 \
--tab --working-directory=myfolder3
Add the following to your ~/.bashrc
cd ~/myfolder
You could use the nautilus-open-terminal extension. This allows you to right-click on a folder in nautilus and open a terminal window with that directory as its working directory.
You can also run a terminal in the normal way, type "cd ", and drag a folder icon from nautilus to the window. This will paste the path of the folder into the command line and you then type return to change to that directory. You can do the same thing with regular files to paste their path and run commands on them.
Directory option
There is the option --working-directory to specify the startup directory of the terminal (no short option form).
The basic approach to open the terminal in /some/dir is
gnome-terminal --working-directory=/some/dir
but there is a trap...
Bad trap
Assuming we want to start the terminal in the directory ~/dir.
This does not work:
gnome-terminal --working-directory=~/dir
The command looks perfectly fine according to the option syntax, but the terminal starts in the home directory.
It's because it does not expand the tilde (~), for confusing reasons - see below.
Thesse do work:
gnome-terminal --working-directory=/home/auser/dir
gnome-terminal --working-directory=$HOME/dir
gnome-terminal --working-directory ~/dir
Tilde expansion
Note there is no = in the last variant. Because of this, the ~ is at the start of a shell word, and therefore is handeled by tilde expansion.
The problem is that ~ does not get expanded everywhere, but only in certain places. One of them is in variable assignments, like directory=~/dir. That's ok, ~ gets expanded to $HOME, but --working-directory=~/dir does not expand ~, because that is not a variable assignment, it only looks very similar.
Actually, this is how I turn it off for everyone by default.
gconftool-2 --direct \
--config-source xml:readwrite:/etc/gconf/gconf.xml.mandatory/ \
--set -- type=bool /apps/nautilus-open-terminal/desktop_opens_home_dir true
From GNOME Terminal - Getting Started:
You can also specify a command that runs automatically when you start GNOME Terminal in the profile.
If the folder has a complicated path, ie not just ~/myfolder, you could create a symlink to it in your home directory so you can get to it quickly. You can also set the CDPATH environment variable to tell bash to search a list of directories when you type cd myfolder.
To open in my desired directory as root user I ran:
gnome-terminal --working-directory=/home/my-project/ -x bash -c "sudo su"
I'm impressed by
Neil's Mayhew comment
and
Volker's Siegel answer. I've tried to not only set default directory for
gnome-terminal but preserve habitual behavior of desktop environment as well (I'm using Linux Mint 17.1 Cinnamon,
GNOME Terminal 3.6.2, perhaps it also can be applied for other Gnome-congenered DEs). So let me put my two cents in.
Adding cd ~/myfolder at the very end of ~/.bashrc does the job. But as already mentioned it will affect every
interactive shell. Even more, if you open some directory in a file manager (Nemo or Nautilus or something like
this) and appeal to the context menu from there (e.g. right click and then select Open in Terminal) new instance of
gnome-terminal will be started in ~/myfolder regardless of the folder which was loaded in the file manager.
Even if you run gnome-terminal --working-directory=/some/other/folder explicitly it will still open ~/myfolder. Seems that the
approach with .bashrc is unusable.
gnome-terminal --working-directory=myfolder works fine but only when you use custom menu entry in you DE
(or custom shortcut on desktop) which runs terminal with this parameter. If you would like to run gnome-terminal from command line
or from mini-launcher (press Alt + F2), you have to type the parameter every time. Anyway this approach is more-or-less usable.
How gnome-terminal determines which folder to open? When --working-directory is not specified it opens current
working directory (e.g. $PWD) otherwise it opens directory specified explicitly.
I've found the following solution.
Create a file named gnome-terminal in your ~/bin folder. It will act as shortcut but from everywhere (start menu,
mini-launcher, other terminal instance, etc) because ~/bin is already in $PATH (at least in Linux Mint...). Make this file
executable. Then put the following content into the script:
#!/bin/bash
home_directory=~
if [ "$PWD" == "$home_directory" ]; then
# When 'gnome-terminal' was ran from either
# - start menu
# - mini-launcher
# ...
# parent directory is set to $HOME.
#
# We respect original command line arguments.
# For example, when terminal is ran from another
# terminal instance and '--working-directory' is
# specified explicitly we should left it as it is.
# If there are two '--working-directory' switches
# in the command line 'gnome-terminal' will pick up
# the last one.
#
# Also we use full path to executable here in order
# to prevent recursive calling of 'gnome-terminal' from
# '~/bin'.
/usr/bin/gnome-terminal --working-directory=/ "$#"
else
# 'gnome-terminal' was from another directory.
# We don't change anything.
/usr/bin/gnome-terminal "$#"
fi
If you run gnome-terminal from start menu you will see that current folder in new terminal is / (you can use any folder, for example, ~/myfolder because our custom wrapper is a Bash-script, so shell's expansion with work fine).
If you appeal to Open in Terminal in a file manager you will get current directory in the new terminal.
If you run gnome-terminal with explicit --working-directory parameter (perhaps, from existing terminal instance) new terminal instance will be opened in the directory you specified.

Resources