V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Distributions
Ubuntu
Fedora
CentOS
中文资源站
网易开源镜像站
jiyinyiyong
V2EX  ›  Linux

cd 用来日常操作太丑陋了, 我要写个书签功能

  •  
  •   jiyinyiyong ·
    jiyinyiyong · 2012-10-14 20:32:37 +08:00 · 5870 次点击
    这是一个创建于 4204 天前的主题,其中的信息可能已经有所发展或是发生改变。
    自言自语这个节点有点搞不懂,, 不好意思重帖了.. http://v2ex.com/t/49968

    ----------

    用 Node 写了个粗糙的命令, 现在至少能动做了
    https://github.com/jiyinyiyong/cd-link
    Bash 部分实在太难受了, 和同学一起想办法凑了很多次才成功的..
    要强大 Bash 大概真的能很强大, 的对于我这样的用户来说真设计太恶心了
    下面是一些演示:

    ➤➤ g -l
    cd-link: /home/chen/code/cd-link
    ➤➤ pwd
    /home/chen/code/cd-link
    ➤➤ g -l
    cd-link: /home/chen/code/cd-link
    ➤➤ g home /home/chen/
    'home' -> '/home/chen/' saved!
    ➤➤ g -l
    cd-link: /home/chen/code/cd-link
    home: /home/chen/
    ➤➤ g home
    code/ data/ desk/ dl/ doc/ kit/ music/ pic/ pub/ tmp/ video/ zjut/
    ∮ 12 /home/chen
    ➤➤ g nginx /etc/nginx/
    'nginx' -> '/etc/nginx/' saved!
    ➤➤ g code /home/chen/code/
    'code' -> '/home/chen/code/' saved!
    ➤➤ g nginx
    fastcgi.conf fastcgi_params koi-utf koi-win mime.types nginx.conf scgi_params uwsgi_params win-utf
    ∮ 9 /etc/nginx
    ➤➤ g -l
    cd-link: /home/chen/code/cd-link
    home: /home/chen/
    nginx: /etc/nginx/
    code: /home/chen/code/
    ➤➤ g demo /var/lib/mongodb/
    'demo' -> '/var/lib/mongodb/' saved!
    ➤➤ g dem
    link 'dem' not found!
    ➤➤ g demo
    bash: cd: /var/lib/mongodb/: Permission denied
    fastcgi.conf fastcgi_params koi-utf koi-win mime.types nginx.conf scgi_params uwsgi_params win-utf
    ∮ 9 /etc/nginx
    ➤➤ g -d demo
    removed link 'demo'
    ➤➤ g -l
    cd-link: /home/chen/code/cd-link
    home: /home/chen/
    nginx: /etc/nginx/
    code: /home/chen/code/
    ➤➤

    我使用 Node 处理数据书签映射的, 平台兼容性很差吧
    但我想这个思路在服务器段可以借鉴..
    就是用 Bash 调用外部程序, 然后形成 cd 跳转功能
    因为直接在外部程序使用 cd 是无法起效的, 烦了我好久... >_<

    代码比较烂, 如果有高人弄个 C 的版本 Patch 下 Bash 就好了
    28 条回复    1970-01-01 08:00:00 +08:00
    SErHo
        1
    SErHo  
       2012-10-14 20:44:05 +08:00
    在 zsh 里直接使用 hash -d xxx=xxx 就行了,没有这么麻烦。如果真要写一个,可以考虑使用 Python 或者 perl,Linux 发行版里面基本上自带这俩。
    cabbala
        2
    cabbala  
       2012-10-14 20:45:22 +08:00
    这个写个bash脚本就好了,而且有现成的。

    我在zsh下用https://github.com/sjl/z-zsh,不用自己去累死累活的手动添加bookmark,会自动计算目录的频率,然后跳转过去

    # USE:
    # * z foo # goes to most frecent dir matching foo
    # * z foo bar # goes to most frecent dir matching foo and bar
    # * z -r foo # goes to highest ranked dir matching foo
    # * z -t foo # goes to most recently accessed dir matching foo
    # * z -l foo # list all dirs matching foo (by frecency)
    cabbala
        3
    cabbala  
       2012-10-14 20:46:07 +08:00
    @SErHo 我擦,学到一招。。lz苦逼了
    loading
        4
    loading  
       2012-10-14 20:49:30 +08:00
    alias cdn="cd /bla/bla"
    当然,zsh就不说了。
    jiyinyiyong
        5
    jiyinyiyong  
    OP
       2012-10-14 21:03:00 +08:00
    @cabbala autojump 我在用.. 真心感慨这都是 cd 命令缺失的功能啊
    jiyinyiyong
        6
    jiyinyiyong  
    OP
       2012-10-14 21:10:19 +08:00
    @SErHo 本来的考虑 Python, 但是最初卡在 Bash 上, 我 py 不熟, 就先用 Node 尝试可行性了.
    毕竟 Bash 和 Python 脚本还是分开的, 服务器上不熟也不会很方便...
    不会写 C 的菜鸟也只能到这种程度了..
    jiyinyiyong
        7
    jiyinyiyong  
    OP
       2012-10-14 21:11:04 +08:00
    "服务器上不熟也不会很方便" -> "服务器部署不方便"
    reus
        8
    reus  
       2012-10-14 21:11:16 +08:00
    duhastmich
        9
    duhastmich  
       2012-10-14 21:40:23 +08:00
    补充个bashmark,其实用了tmuxinator之后,就抛弃了这类东西
    yetone
        10
    yetone  
       2012-10-14 22:38:20 +08:00
    alias 难道满足不了楼主吗?
    cabbala
        11
    cabbala  
       2012-10-14 22:40:35 +08:00
    @jiyinyiyong 你这个用shell脚本改写也是很容易的,没必要用Python Node.js之类。。
    jiyinyiyong
        12
    jiyinyiyong  
    OP
       2012-10-14 22:42:49 +08:00
    @duhastmich 赞 bashmark, 如果早知道的话我就不用手写了, 反而习惯冲突了
    话说 tmuxinator 看了下是管理 Ruby 项目的.. 也有这方面功能?
    jiyinyiyong
        13
    jiyinyiyong  
    OP
       2012-10-14 22:46:06 +08:00
    @yetone 写 alias 就想写网站手写 html.
    jiyinyiyong
        14
    jiyinyiyong  
    OP
       2012-10-14 22:46:52 +08:00
    @cabbala bashmark? 对的.. 不过我实力差好多.. 呵呵
    lyxint
        15
    lyxint  
       2012-10-15 02:00:09 +08:00
    bash有dirs pushd popd
    chone
        16
    chone  
       2012-10-15 06:10:42 +08:00
    使用频率很高的用ln弄个链接不就行了吗。。使用频率还要更高的估计早就已经写好脚本来跑了。不过这些都是个人喜好,自己觉得爽就好了。

    另外我确实是手写html的,css,js也是手写的。
    lowstz
        17
    lowstz  
       2012-10-15 08:09:09 +08:00
    @lyxint +1
    dirs pushd popd 就能满足Lz要求了
    jiyinyiyong
        18
    jiyinyiyong  
    OP
       2012-10-15 08:27:18 +08:00
    @lyxint @lowstz 我认为 pushd 不够灵活


    @chone 我的意思是如果只有一个网页, 手写是好的, 甚至用 DW 也好的
    但是网站的页面可能多可能少, 就需要用程序管局模板生成 HTML 而不能手写
    sectic
        19
    sectic  
       2012-10-15 08:28:03 +08:00
    cd -
    sectic
        20
    sectic  
       2012-10-15 08:30:56 +08:00
    cd - ,就是简化的push和pop,要是还不顺手ls就去装 , Oh-My-Zsh,那里的123456789绑定你会喜欢的
    humiaozuzu
        21
    humiaozuzu  
       2012-10-15 10:12:07 +08:00
    同用z
    brew install z 然后解决所有问题了!
    jiyinyiyong
        22
    jiyinyiyong  
    OP
       2012-10-15 12:03:55 +08:00
    @humiaozuzu z 不就 autojump 一样么..
    coderoar
        23
    coderoar  
       2012-10-15 14:39:25 +08:00
    在inputrc添加一行:

    # history search
    "\C-o": history-search-backward
    "\C-j": history-search-forward

    这样输入“cd xxx”后就能按快捷键搜索命令历史。
    xlaok
        24
    xlaok  
       2012-10-15 14:44:15 +08:00
    @SErHo lz苦逼了+1
    jiyinyiyong
        25
    jiyinyiyong  
    OP
       2012-10-15 15:07:37 +08:00
    @coderoar 我的场景里这个用法不方便. 记下了, 能用在别的地方.

    @xlaok 的确. 了+1
    ekayxu
        26
    ekayxu  
       2012-10-15 15:25:49 +08:00   ❤️ 1
    zhanglistar
        27
    zhanglistar  
       2012-10-19 15:18:10 +08:00
    bash里alias搞定直接进入目录的需要。

    function cd1() {
    if [ -n $1 ]; then
    cd $1 && ls
    else
    cd && ls
    fi
    }
    alias cd='cd1'

    进入目录自动显示。也是ok的。
    jiyinyiyong
        28
    jiyinyiyong  
    OP
       2013-03-09 11:14:41 +08:00
    现在回头看有了 Bashmarks 真的轻松多了, 感谢诸位
    https://github.com/coffee-js/languages/issues/53
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3725 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 10:22 · PVG 18:22 · LAX 03:22 · JFK 06:22
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.