So in my rifle.conf I have
ext org, has emacs, terminal, flag f = emacs "$#"
However it opens the file with the GUI version of emacs?
How do I open it with 'emacs -nw filename'?
I tried what Tom had said but I found that there were too many steps to open my org file. I wanted to be able to just press enter on an orgfile within ranger and have it open emacs -nw file.org, within the tmux pane running ranger.
The following solved my dilemma:
!mime ^text, label editor, ext org = emacs -nw -- "$#"
Edit: Interesting dig - after a look at OPs solution above: It turns out ranger does indeed open a terminal program 'on top of itself', so to speak.
The culprit in OPs first attempt is the flag f, in fact the minimal
ext org = emacs -nw "$#"
works like a charm.
Old attempts:
As far as I read the docs, you cannot open a file with a program running in the same terminal as ranger itself is running
in (I may be wrong, though).
Following the documentation I exported TERMCMD, since I'm using tmux with TERM=screen-256color, ranger cannot infer the terminal to use from that:
# just a test, I don't use xterm
export TERMCMD=xterm
And simply added the t flag and -nw to the command in rifle.conf:
❯ cat ~/.config/ranger/rifle.conf
ext org, has emacs, terminal, flag tf = emacs -nw "$#"
Hitting r in ranger
0 | emacs -nw "$#"
:open_with 0
This opens a new xterm, starting a new emacs editing the file in question. You can simply press the Return key while on the file in question with this setup; using r would show all available options.
If you are living in the terminal and don't want to start another graphical terminal, I suggest using something like tmux - you can use tmux commands to open files, in this case like so
ext org, has emacs, terminal, flag f = tmux splitw -h emacs -nw "$#"
splitting the current pane (with ranger running), launching a new pane to the right of the current one and starting emacs there. The pane will be closed when you quit emacs. I'd probably start an emacsserver and use emacsclient -nw in this scenario.
Related
I'm running ubuntu 1804 on windows using the WSL. Everything is set up fine and works correctly. I've also installed ZSH and oh-my-zsh, again this is all good and everything looks like its working fine. Everything except the arrow keys whilst using vim or man pages or some other command line tools.
The up and down keys work on the command line when scrolling through history and also for select commands like nano. Also if I boot into bash rather than zsh the arrow keys do work in vim and man pages, in fact they work everywhere.
If i boot into bash, then switch to zsh on the command line manually the arrow keys then work everywhere.
So my cmder config for zsh
c:/_distros/ubuntu1804/ubuntu1804.exe -c zsh -cur_console:pm
and for bash
set "PATH=%ConEmuBaseDirShort%\wsl;%PATH%" & %ConEmuBaseDirShort%\conemu-cyg-64.exe --wsl -cur_console:pm:/mnt
The one for bash uses the conemu-cyg-64.exe program that comes from conemu which is a symbiont of POSIX enabled pty and WinAPI full-featured terminal.
Apparently you can use this tool with zsh but i cant manage to make it work i get the error
{PID:10592} failed to run shell (2): No such file or directory
{PID:10592} shell: `/usr/bin/zsh` `-l` `-i`
{PID:10592} dir: `/cygdrive/c/Program Files/cmder`
ConEmuC: Root process was alive less than 10 sec, ExitCode=0.
Press Enter or Esc to close console...
and this is the task in cmder
set "PATH=%ConEmuBaseDirShort%\wsl;%PATH%" & %ConEmuBaseDirShort%\conemu-cyg-64.exe /usr/bin/zsh -l -i -cur_console:pm:/mnt
So I think that if i can boot into zsh using conemu-cyg-64 that the cursor keys will probably work in commands like vim and the man pages. Any help or advice getting that working would be brilliant.
EDIT:
On my ubuntu install zsh is installed at /usr/bin/zsh, but there is no file or folder /cygdrive/c/Program Files/cmder
Many thanks to #Maximus for pointing me in the right direction. The answer was right under my nose at the bash on windows page of conemu. A small change to the command i was using before. the zsh needs to go on the end rather than before the --wsl.
The correct task to ensure that cursor keys work on all apps in the terminal is:
set "PATH=%ConEmuBaseDirShort%\wsl;%PATH%" & %ConEmuBaseDirShort%\conemu-cyg-64.exe --wsl -cur_console:pnm:/mnt -t zsh -l
Consider following:
$ cd /home/mydir
$ jupyter notebook --port=8888
In plain English, I am running jupyter server from /home/mydir directory.
Is there a simple way to get this directory from within a notebook regardless if it's a R notebook or a Python notebook or whatever? Maybe there is some magic command or variable?
NOTE: getwd() is not an answer as it returns directory of a current notebook but not the jupyter server root.
I have a similar problem and found your post, although I don't see a viable solution yet. Eventually I did found a solution, although it works only because I only care about Linux and I only care about Python. The solution is this magic line:
J_ROOT = os.readlink('/proc/%s/cwd' % os.environ['JPY_PARENT_PID'])
(I put it in a module in my PYTHONPATH so that I can easily use it in any Python notebook.) See if it is good for you.
Remember that your iPtyhon is just a Python module, so you can execute any valid Python code in a cell. So, if you started your notebook and haven't executed any directory changes in your code, you should be able to retrieve your cwd with the following in a cell:
import os
os.getcwd()
But furthermore, you can execute shell commands in cells, so you can retrieve other information in the cell. For example:
!which jupyter
should give you the path to your jupyter executable.
Which then leads you to running something like:
!jupyter --paths
which should give you something similar to:
config:
/Users/yourdir/.jupyter
/usr/local/etc/jupyter
/etc/jupyter
data:
/Users/yourdir/Library/Jupyter
/usr/local/share/jupyter
/usr/share/jupyter
runtime:
/Users/yourdir/Library/Jupyter/runtime
Frankly I'm surprised that all this time later there is still no built-in way to do this. I have used Isaac To's solution on Linux but recently had to make a jupyter notebook portable to Windows as well. Simply using os.getcwd() is fragile because a cell using it to set your JUPYTER_ROOT_DIRECTORY can potentially be called again, after you have changed your working directory.
Here is what I came up with:
try:
JUPYTER_ROOT_DIRECTORY
except NameError:
JUPYTER_ROOT_DIRECTORY = os.getcwd()
I put it in one of the first couple cells with the initial import statements. If the cell gets called again, it will not re-set the variable value because the variable exists and does not throw an exception.
It should be noted that unlike Isaac To's solution it sets the value to the directory the current .ipynb was run from, which is not necessarily the same directory as the top-level dir the user can access in the left hand file pane.
My suggestion is to use an intuitive approach.
Create a new folder within the Jupyter environment with a very unique name, for example, T246813579.
You can now locate the Jupyter working path by searching in your file explorer. For example, you can use the Windows Explorer in order to locate your new folder.
The expected result should look something like this:
C:\Users\my_user_name\JupyterHome\T246813579
The answer from #Isaac works well for Linux, but not all systems have /proc. For a solution that works on macOS and Linux, we can use shell commands, taking advantage of the ! shell assignment syntax in Jupyter:
import os
JPY_ROOT = ! lsof -a -p {os.environ['JPY_PARENT_PID']} -d cwd -F n | tail -1 | cut -c 2-
JPY_ROOT = JPY_ROOT[0]
print(JPY_ROOT) # prints Jupyter's dir
Explanation:
Get the process ID (pid) of the current jupyter instance with os.environ['JPY_PARENT_PID']
Call lsof to list the process's open files, keeping only the current working dir (cwd)
Parse the output of lsof using tail and cut to keep just the directory name we want
The ! command returns a list, here having only one element
Alternate Version
To save the os import, we could also use shell commands to get the PID. We could also do the subsequent string wrangling in python, rather than calling tail and cut from the shell, as:
JPY_ROOT = ! lsof -a -p $(printenv | grep JPY_PARENT_PID | cut -d '=' -f 2) -d cwd -F n
JPY_ROOT = JPY_ROOT[2][1:]
Let's say you run a command like grunt serve on a tmux pane,
and you kill the pane on which the command is running. I found that
the process is not killed:
ps aux | grep grunt
still shows that grunt is running even though the pane is gone.
How do you kill a tmux pane along with the process(es)?
To stop the program running you can close the pane by entering <C-B> x and then entering y.
Just do: <CTRL-B>:kill-pane
Use Ctrl + b¹, > to get a menu with this and other useful commands:
This is what uses the Spotify plugin.
¹ Or the chosen tmux escape sequence.
Other notes and sources
tmux list-keys | grep display-menu #
curl cht.sh/tmux # Online cheatsheet
https://wiki.archlinux.org/title/tmux
You may find the tmux-safekill plugin useful.
I wanted it to kill Ruby processes, so I had to fork the repo to add that functionality in, so I'm sure you could do the same for grunt processes if you don't get all the functionality you need from the repo directly.
I wrote this thing and I'd like to source a Python virtualenv activate script whenever a new window is created in a running session.
Is it possible? The default-command option is related to the shell, not an additional command.
I am making the following assumptions:
You are using bash
Your .bashrc file is source for each new window that starts a shell
You only want to start your virtualenv in a window that runs a shell
Add the code you want to run to your .bashrc:
if [[ $TMUX ]]; then
# code here
fi
This code will only run for new shells which are in an existing tmux session.
I'm trying to move most of my editing and shell activities into emacs, so that I can easily continue my workflow from different computers. I also have different emacs instances for my different projects. E.g., I start up:
emacs --daemon=project1
emacs --daemon=project2
Then whenever I want to start a frame to work on project1, I do:
emacsclient -s project1 -c
Mostly it works great, but I have trouble with executing tools from my emacs shells that are trying to fire up an editor using $EDITOR. Of course, what I would want in this case is that a new buffer opens in my current emacs instance, exactly what emacsclient is designed for. So if in a shell buffer in project1's emacs I say:
export EDITOR='emacsclient -s project1'
then in that particular shell everything works perfectly.
My question is, how could I automatically set the EDITOR variable in a way that it points to the emacs instance the current shell is running in?
Here is one way to do it:
(add-hook 'shell-mode-hook
(lambda ()
(comint-send-string
(get-buffer-process (current-buffer))
(concat "export EDITOR='emacsclient -s "
(daemonp)
" -c'\n"))))