zsh: Added function inplaceMkDirs().
authorMichael Prokop <mika@grml.org>
Wed, 20 Jul 2011 15:09:06 +0000 (17:09 +0200)
committerMichael Prokop <mika@grml.org>
Thu, 21 Jul 2011 21:26:03 +0000 (23:26 +0200)
Thanks: Bernhard Tittelbach <bernhard@tittelbach.org>

doc/grmlzshrc.t2t
etc/zsh/zshrc

index 7f62c1b..70573ca 100644 (file)
@@ -424,6 +424,12 @@ Deletes a word left of the cursor; seeing '/' as additional word separator.
 : **CTRL-x-1**
 Jump right after the first word.
 
+: **CTRL-x-M()**
+Create directory under cursor or the selected area.
+To select an area press ctrl-@ and use the cursor.
+Use case: you type "mv abc ~/testa/testb/testc/" and remember that the
+directory does not exist yet -> press **CTRL-xM** and problem solved.
+
 : **CTRL-x-p**
 Searches the last occurence of string before the cursor in the command history.
 
index 3e20e5b..1f8cdaa 100644 (file)
@@ -2667,6 +2667,40 @@ cdt() {
 mdiff() {
     diff -udrP "$1" "$2" > diff.`date "+%Y-%m-%d"`."$1"
 }
+
+#f5# Create directory under cursor or the selected area
+# Press ctrl-xM to create the directory under the cursor or the selected area.
+# To select an area press ctrl-@ or ctrl-space and use the cursor.
+# Use case: you type "mv abc ~/testa/testb/testc/" and remember that the
+# directory does not exist yet -> press ctrl-XM and problem solved
+inplaceMkDirs() {
+    local PATHTOMKDIR
+    if ((REGION_ACTIVE==1)); then
+        local F=$MARK T=$CURSOR
+        if [[ $F -gt $T ]]; then
+            F=${CURSOR}
+            T=${MARK}
+        fi
+        # get marked area from buffer and eliminate whitespace
+        PATHTOMKDIR=${BUFFER[F+1,T]%%[[:space:]]##}
+        PATHTOMKDIR=${PATHTOMKDIR##[[:space:]]##}
+    else
+        local bufwords iword
+        bufwords=(${(z)LBUFFER})
+        iword=${#bufwords}
+        bufwords=(${(z)BUFFER})
+        PATHTOMKDIR="$bufwords[iword]"
+    fi
+    [[ -z "${PATHTOMKDIR}" ]] && return 1
+    if [[ -e "${PATHTOMKDIR}" ]]; then
+        zle -M " path already exists, doing nothing"
+    else
+        zle -M "$(mkdir -p -v "${PATHTOMKDIR}")"
+        zle end-of-line
+    fi
+}
+zle -N inplaceMkDirs && bindkey '^XM' inplaceMkDirs
+
 #f5# Memory overview
 memusage() {
     ps aux | awk '{if (NR > 1) print $5; if (NR > 2) print "+"} END { print "p" }' | dc