Zshbrowser script

When start, it will spawn ranger from zsh browse() function. This way you can
quit ranger, return to the shell, do some shell stuff, and go back to ranger
with the browse() function.

The big advantage of using ranger this way is that the browse() function syncs
the pwd between ranger and Zsh.
master
Pierre Neidhardt 2013-07-27 11:12:25 +02:00
parent 2480efc164
commit ceea7cdcdb
6 changed files with 52 additions and 29 deletions

View File

@ -333,7 +333,7 @@ globalkeys = awful.util.table.join(
awful.key({ modkey, }, "e", function () awful.util.spawn(os.getenv("EDITOR")) end), awful.key({ modkey, }, "e", function () awful.util.spawn(os.getenv("EDITOR")) end),
-- Directory browser -- Directory browser
awful.key({ modkey, }, "d", function () awful.util.spawn(termcmd .. "ranger") end), awful.key({ modkey, }, "d", function () awful.util.spawn("zshbrowser") end),
awful.key({ }, "XF86Explorer", function () awful.util.spawn(termcmd .. "ranger") end), awful.key({ }, "XF86Explorer", function () awful.util.spawn(termcmd .. "ranger") end),
awful.key({ }, "XF86MyComputer", function () awful.util.spawn(termcmd .. "ranger") end), awful.key({ }, "XF86MyComputer", function () awful.util.spawn(termcmd .. "ranger") end),

View File

@ -152,5 +152,6 @@ map J move down=5
## New tab in current folder. Use 'gn' for home folder. ## New tab in current folder. Use 'gn' for home folder.
map <c-n> eval fm.tab_new('%d') map <c-n> eval fm.tab_new('%d')
## Start shell in current folder. Needs zshcd script. ## Start shell in current folder. Needs zshcd script. This is useful if you have
## multiple tabs and you do not want to lose them just to run a shell.
map S shell zshcd %d map S shell zshcd %d

28
.scripts/zshbrowser Executable file
View File

@ -0,0 +1,28 @@
#!/bin/sh
## Start terminal, invoke Zsh and launch browser automatically.
## It is important to invoke the terminal from here, and not run the terminal
## over this script, otherwise cleaning line will not run when window is killed.
if [ ! -f ~/.zshrc ]; then
echo '~/.zshrc not found. Exiting.'
exit
fi
HOOK_FILE_GREP="$(grep "HOOK_FILE=" ~/.zshrc | cut -f2 -d=)"
[ -n "$HOOK_FILE_GREP" ] && HOOK_FILE="$(eval echo "$HOOK_FILE_GREP")"
if [ -z "$HOOK_FILE" ]; then
echo 'HOOK_FILE variable could not be set. Exiting.'
exit
fi
if [ -z "$TERMCMD" ]; then
echo 'TERMCMD not set. Exiting.'
exit
fi
[ -f "$HOOK_FILE" ] && sed -ni '/###ZSHBROWSER/!p' "$HOOK_FILE"
echo 'browse ###ZSHBROWSER' >> "$HOOK_FILE"
$TERMCMD -e zsh
[ -f "$HOOK_FILE" ] && sed -ni '/###ZSHBROWSER/!p' "$HOOK_FILE"
[ $(du "$HOOK_FILE" | cut -f1) -eq 0 ] && rm "$HOOK_FILE"

View File

@ -1,5 +1,5 @@
#!/bin/sh #!/bin/sh
## Start zsh in directory specified as first argument. ## Start zsh in the directory specified as first argument.
if [ ! -d "$1" ]; then if [ ! -d "$1" ]; then
zsh zsh
@ -10,36 +10,27 @@ fi
ZDIRS="$HOME/.cache/zsh/dirs" ZDIRS="$HOME/.cache/zsh/dirs"
## Fetch file location dynamically. ## Fetch file location dynamically.
if [ ! -f "$ZDIRS" ]; then if [ ! -f ~/.zshrc ]; then
if [ ! -f ~/.zshrc ]; then echo '~/.zshrc not found. Exiting.'
echo '~/.zshrc not found. Exiting.' exit
exit
fi
SHELL_DIR="$HOME/.shell.d"
if [ ! -d "$SHELL_DIR" ]; then
SHELL_DIR_GREP="$(grep "SHELL_DIR=" ~/.zshrc | cut -f2 -d=)"
[ -n "$SHELL_DIR_GREP" ] && SHELL_DIR="$(eval echo "$SHELL_DIR_GREP")"
fi
if [ ! -d "$SHELL_DIR" ]; then
echo 'DIRSTACKFILE and SHELL_DIR not found. Exiting.'
exit
fi
ZDIRS_GREP="$(grep -rm1 'DIRSTACKFILE=' "$SHELL_DIR" | cut -f2 -d=)"
[ -n "$ZDIRS_GREP" ] && ZDIRS="$(eval echo "$ZDIRS_GREP")"
fi fi
SHELL_DIR_GREP="$(grep "SHELL_DIR=" ~/.zshrc | cut -f2 -d=)"
[ -n "$SHELL_DIR_GREP" ] && SHELL_DIR="$(eval echo "$SHELL_DIR_GREP")"
if [ ! -d "$SHELL_DIR" ]; then
echo 'SHELL_DIR not found. Could not search for DIRSTACKFILE. Exiting.'
exit
fi
ZDIRS_GREP="$(grep -rm1 'DIRSTACKFILE=' "$SHELL_DIR" | cut -f2 -d=)"
[ -n "$ZDIRS_GREP" ] && ZDIRS="$(eval echo "$ZDIRS_GREP")"
if [ ! -f "$ZDIRS" ]; then if [ ! -f "$ZDIRS" ]; then
echo 'DIRSTACKFILE not found. Exiting.' echo 'DIRSTACKFILE not found. Exiting.'
exit exit
fi fi
if [ -n "$(grep "$1" "$ZDIRS")" ]; then BUF="$(realpath "$1")
BUF="$(grep -v "$1" "$ZDIRS")" $(grep -v "$1" "$ZDIRS")"
echo "$BUF" echo "$BUF" > "$ZDIRS"
echo "$BUF" > "$ZDIRS"
fi
prepend "$1" "$ZDIRS"
zsh zsh

View File

@ -411,7 +411,7 @@ browse ()
{ {
# Compatible with ranger 1.4.2 through 1.6.* # Compatible with ranger 1.4.2 through 1.6.*
# #
# Automatically change the directory in bash after closing ranger # Automatically change the directory in bash after closing ranger.
# #
# This is a shell wrapper script to automatically change the directory to the # This is a shell wrapper script to automatically change the directory to the
# last visited one after ranger quits. To undo the effect of this function, you # last visited one after ranger quits. To undo the effect of this function, you

5
.zshrc
View File

@ -10,6 +10,9 @@
SHELL_CURRENT="$(ps -o command="" $$)" SHELL_CURRENT="$(ps -o command="" $$)"
SHELL_DIR="$HOME/.shell.d" SHELL_DIR="$HOME/.shell.d"
## Need to be expanded for external scripts to fetch zsh hook file location.
HOOK_FILE="$HOME/.shell.d/hook"
## .profile is sourced automatically when X is started, but we need to source it ## .profile is sourced automatically when X is started, but we need to source it
## manually to TTY. ## manually to TTY.
[ -z "$DISPLAY" ] && [ -f "$HOME/.profile" ] && . "$HOME/.profile" [ -z "$DISPLAY" ] && [ -f "$HOME/.profile" ] && . "$HOME/.profile"
@ -26,4 +29,4 @@ SHELL_DIR="$HOME/.shell.d"
[ -f "${SHELL_DIR}/keys_zsh" ] && . "${SHELL_DIR}/keys_zsh" [ -f "${SHELL_DIR}/keys_zsh" ] && . "${SHELL_DIR}/keys_zsh"
## Should be sourced last ## Should be sourced last
[ -f "${SHELL_DIR}/hook" ] && . "${SHELL_DIR}/hook" [ -f "$HOOK_FILE" ] && . "$HOOK_FILE"