--//抽一点点时间,重新编写了bccalc_linux的插件,实际上与windows的不同之处在与echo的处理方式上.
R:\>echo obase=16;254 | bc
FE

$ echo obase=16;254 | bc
obase=16
-bash: 254: command not found

$ echo 'obase=16;254' | bc
FE

--//必须加入分号.

--//源代码如下,我就不测试了.
"" calculate expression entered on command line and give answer, e.g.:
"" :Calculate sin (3) + sin (4) ^ 2
command! -nargs=+ Calculate echo "<args> = " . Calculate ("<args>",0)

"" calculate expression from selection using bc -l, pick a mapping, or use the Leader form
vnoremap ;bc "ey`>:call CalcLines(0)<CR>
vnoremap ;bb "ey`>:call CalcLines(0)<CR>

"" convert hexdecimal to decimal
vnoremap ;10 "ey`>:call CalcLines(10)<CR>

"" convert hexdecimal to decimal
vnoremap ;16 "ey`>:call CalcLines(16)<CR>

"" computer hexdecimal to hexdecimal
vnoremap ;16x "ey`>:call CalcLines(1016)<CR>

"" split event P1 to TYPE and MODE
vnoremap ;tx "ey`>:call CalcLines(1616)<CR>

"" split dba(10) or dba(16) to file# and block#
vnoremap ;22 "ey`>:call CalcLines(22)<CR>
vnoremap ;dba "ey`>:call CalcLines(22)<CR>

"" split scn(10) or scn(16) into scn_wrap,scn_base
vnoremap ;32 "ey`>:call CalcLines(32)<CR>
vnoremap ;scn "ey`>:call CalcLines(32)<CR>

"" convert scn_wrap,scn_base(10) or scn_wrap,scn_base(16) to 10 or 16 base
vnoremap ;ss "ey`>:call CalcLines(10016)<CR>

"" convert file#,block# dba(10) or file#,block# dba(16) to 10 or 16 base
vnoremap ;rr "ey`>:call CalcLines(20016)<CR>

"" convert hexdecimal to decimal or decimal to hexdecimal
vnoremap ;hd "ey`>:call CalcLines(30016)<CR>
vnoremap ;hh "ey`>:call CalcLines(30016)<CR>
vnoremap ;dh "ey`>:call CalcLines(30016)<CR>

"" --------------------------------------------------------------------
"" calculate expression on current line using bc -l, pick a mapping, or use the Leader
nnoremap  <Leader>bx <Esc>"eyy$:call CalcLines(0)<CR>
nnoremap  <Leader>bc <Esc>"eyy$:call CalcLines(0)<CR>
nnoremap  <Leader>bb <Esc>"eyy$:call CalcLines(0)<CR>
noremap   <Leader>cc Yp!!bc -lq\| tr -d '\n\\\r' \| sed -e "s/\.\([0-9]*[1-9]\)0\+$/.\1/" -e "s/\.0\+$//"<CR>kA = <ESC>J

"" calculate expression on current line using bc , pick a mapping, or use the Leader
noremap   <Leader>c0 Yp!!bc -q\| tr -d '\n\\\r' \| sed -e "s/\.\([0-9]*[1-9]\)0\+$/.\1/" -e "s/\.0\+$//"<CR>kA = <ESC>J

"" calculate expression on current line ( delete ,) using bc -l,  pick a mapping, or use the Leader
noremap   <Leader>c, Yp!!sed "s/,//g" \|bc -lq\| tr -d '\n\\\r' \| sed -e "s/\.\([0-9]*[1-9]\)0\+$/.\1/" -e "s/\.0\+$//"<CR>kA = <ESC>J

"" convert hexdecimal to decimal
nnoremap  <Leader>10 <Esc>"eyy$:call CalcLines(10)<CR>

"" convert decimal to hexdecimal
nnoremap  <Leader>16 <Esc>"eyy$:call CalcLines(16)<CR>

"" computer hexdecimal to hexdecimal
nnoremap  <Leader>16x <Esc>"eyy$:call CalcLines(1016)<CR>

"" split event P1 to TYPE and MODE
nnoremap  <Leader>tx  <Esc>"eyy$:call CalcLines(1616)<CR>

"" split dba(10) or dba(16) to file# and block#
nnoremap  <Leader>22  <Esc>"eyy$:call CalcLines(22)<CR>
nnoremap  <Leader>dba <Esc>"eyy$:call CalcLines(22)<CR>

"" split scn(10) or scn(16) into scn_wrap,scn_base
nnoremap  <Leader>32  <Esc>"eyy$:call CalcLines(32)<CR>
nnoremap  <Leader>scn <Esc>"eyy$:call CalcLines(32)<CR>

"" convert scn_wrap,scn_base(10) or scn_wrap,scn_base(16) to 10 or 16 base
nnoremap  <Leader>ss <Esc>"eyy$:call CalcLines(10016)<CR>

"" convert file#,block# dba(10) or file#,block# dba(16) to 10 or 16 base
nnoremap  <Leader>rr <Esc>"eyy$:call CalcLines(20016)<CR>

"" convert hexdecimal to decimal or decimal to hexdecimal
nnoremap  <Leader>hd <Esc>"eyy$:call CalcLines(30016)<CR>
nnoremap  <Leader>hh <Esc>"eyy$:call CalcLines(30016)<CR>
nnoremap  <Leader>dh <Esc>"eyy$:call CalcLines(30016)<CR>

"" --------------------------------------------------------------------
"" calculate from insertmode
inoremap =: =<Esc>"eyy$:call CalcLines(0)<CR>a
inoremap =- =<Esc>"eyy$:call CalcLines(30016)<CR>a

"" --------------------------------------------------------------------
""  Calculate:
""    clean up an expression, pass it to bc, return answer
function! Calculate (s,flag)

let has_hex = 0
    let str = a:s

" remove newlines and trailing spaces
    let str = substitute (str, "\n",   "", "g")
    let str = substitute (str, '\s*$', "", "g")

" sub common func names for bc equivalent
    let str = substitute (str, '\csin\s*(',  's (', 'g')
    let str = substitute (str, '\ccos\s*(',  'c (', 'g')
    let str = substitute (str, '\catan\s*(', 'a (', 'g')
    let str = substitute (str, "\cln\s*(",   'l (', 'g')
    let str = substitute (str, '\clog\s*(',  'l (', 'g')
    let str = substitute (str, '\cexp\s*(',  'e (', 'g')

" alternate exponitiation symbols
    let str = substitute (str, '\*\*', '^', "g")
    let str = substitute (str, '`', '^',    "g")

if has("windows")
        let str = substitute (str, '\^', '^^^^',    "g")
    endif

" escape chars for shell
    if has("unix")
        let str = escape (str, '*();&><|^')
    endif

let preload = exists ("g:bccalc_preload") ? g:bccalc_preload : ""

" run bc
    " return str
    " let answer = system ("echo " . str . " \| bc -l " . preload)

if a:flag == 0
         let answer = system ("echo " . str . " \| bc -l " . preload)
         " let answer = answer . " --- ". str
    endif

if a:flag == 10
        let str = toupper (str)
        let str = substitute (str, "0x", "", "g")
        let answer = system ("echo 'ibase=16 ;" . str .  "' \| bc -l " . preload)
    endif

if a:flag == 16
        let answer = system ("echo 'obase=16 ;" . str .  "' \| bc -l " . preload)
        let answer = "0x" . tolower ( answer )
    endif

if a:flag == 1016
        let str = toupper (str)
        let str = substitute (str, "0x", "", "g")
        let answer = system ("echo obase=16 ;ibase =16;" . str .  " \| bc " . preload)
        let answer = "0x" . tolower ( answer )
    endif

let has_hex = Check_hex(str)

if a:flag == 1616
        if has_hex == 1
            let str = toupper (str)
            let str = substitute (str, "0x", "", "g")
            " 0x10000 hexdecimal = 65536 (10) = 2^16(10)
            let answer  = system ("echo 'ibase=16 ;" . str . "/10000" . "' \| bc " . preload)
            let answer1 = system ("echo 'ibase=16 ;" . str . "%10000" . "' \| bc " . preload)
            let answer2 = system ("echo 'ibase=16 ;" . str .  "' \| bc -l " . preload)
        else
            let answer  = system ("echo " . str . "/65536" . " \| bc " . preload)
            let answer1 = system ("echo " . str . "%65536" . " \| bc " . preload)
            let answer2 = "0x" . system ("echo 'obase=16 ;" . str .  "' \| bc -l " . preload)
        endif
        let answer = "/2^16  %2^16 (Type | Mode) = " . answer . "," . answer1 ." = " . tolower(answer2)
    endif

if a:flag == 22
        if has_hex == 1
            let str = toupper (str)
            let str = substitute (str, "0x", "", "g")
            " 0x400000 hexdecimal = 4194304 (10) = 2^22(10)
            let answer  = system ("echo 'ibase=16 ;" . str . "/400000" . "' \| bc " . preload)
            let answer1 = system ("echo 'ibase=16 ;" . str . "%400000" . "' \| bc " . preload)
            let answer2 = system ("echo 'ibase=16 ;" . str .  "' \| bc -l " . preload)
        else
            let answer  = system ("echo " . str . "/4194304" . " \| bc " . preload)
            let answer1 = system ("echo " . str . "%4194304" . " \| bc " . preload)
            let answer2 = "0x" . system ("echo 'obase=16 ;" . str .  "' \| bc -l " . preload)
        endif
        " let answer = "set dba " . answer . "," . answer1
        let answer = "set dba " . answer . "," . answer1 ." = alter system dump datafile " . answer . " block " . answer1 ." = " . tolower(answer2)
    endif

if a:flag == 32
        if has_hex == 1
            let str = toupper (str)
            let str = substitute (str, "0x", "", "g")
            " 0x100000000 hexdecimal = 4294967296(10) = 2^32(10)
            let answer  = system ("echo 'ibase=16 ;" . str . "/100000000" . "' \| bc " . preload)
            let answer1 = system ("echo 'ibase=16 ;" . str . "%100000000" . "' \| bc " . preload)
            let answer2 = system ("echo 'obase=16 ;ibase=16 ;" . str . "/100000000" . "' \| bc " . preload)
            let answer3 = system ("echo 'obase=16 ;ibase=16 ;" . str . "%100000000" . "' \| bc " . preload)
        else
            let answer  = system ("echo " . str . "/4294967296" . " \| bc " . preload)
            let answer1 = system ("echo " . str . "%4294967296" . " \| bc " . preload)
            let answer2 = system ("echo 'obase=16 ;" . str . "/4294967296" . "' \| bc " . preload)
            let answer3 = system ("echo 'obase=16 ;" . str . "%4294967296" . "' \| bc " . preload)
        endif
        let answer = "scn_wrap,scn_base(10): " . answer . "," . answer1 . " = scn_wrap,scn_base(16): " . "0x" . tolower (answer2) . "," . "0x" . tolower(answer3)
    endif

if a:flag == 10016
        if has_hex == 1
            let str = toupper (str)
            let str = substitute (str, "0x", "", "g")
            " 0x100000000 hexdecimal = 4294967296(10) = 2^32(10)
            let str = substitute (str, "[,.]", "*100000000+", "g")
            let answer  = system ("echo 'obase=10 ;ibase=16 ;" . str .  "' \| bc -l " . preload)
            let answer1 = system ("echo 'obase=16 ;ibase=16 ;" . str .  "' \| bc -l " . preload)
        else
            let str = substitute (str, "[,.]", "*4294967296+", "g")
            let answer  = system ("echo " . str . " \| bc -l " . preload)
            let answer1 = system ("echo 'obase=16 ;" . str .  "' \| bc -l " . preload)
        endif
        let answer = "scn(10): " . answer . " = scn(16): " . "0x" . tolower (answer1)
    endif

if a:flag == 20016
        if has_hex == 1
            let str = toupper ( str )
            let str = substitute (str, "0x", "", "g")
            " 0x400000 hexdecimal = 4194304 (10) = 2^22(10)
            let str = substitute (str, "[,.]", "*400000+", "g")
            let answer  = system ("echo 'obase=10 ;ibase=16 ;" . str .  "' \| bc -l " . preload)
            let answer1 = system ("echo 'obase=16 ;ibase=16 ;" . str .  "' \| bc -l " . preload)
        else
            let str = substitute (str, "[,.]", "*4194304+", "g")
            let answer  = system ("echo " . str . " \| bc -l " . preload)
            let answer1 = system ("echo 'obase=16 ;" . str .  "' \| bc -l " . preload)
        endif
        let answer = "file#,block# dba(10): " . answer . " = file#,block# dba(16): " . "0x" . tolower (answer1)
    endif

if a:flag == 30016
        if has_hex == 1
            let str = substitute (str, "0x", "", "g")
            let str = toupper ( str )
            let answer = system ("echo 'ibase=16 ;" . str .  "' \| bc -l " . preload)
        else
            let answer = system ("echo 'obase=16 ;" . str .  "' \| bc -l " . preload)
            let answer = "0x" . tolower ( answer )
        endif
    endif

" strip newline and \
    let answer = substitute (answer, "[\\\n]", "", "g")

" strip trailing 0s in decimals
    let answer = substitute (answer, '\.\(\d*[1-9]\)0\+$', '.\1', "")
    let answer = substitute (answer, '\.0\+$', '', "")

return answer
endfunction

" ---------------------------------------------------------------------
" CalcLines:
"
" take expression from lines, either visually selected or the current line,
" pass to calculate function, echo or past answer after '='
function! CalcLines(flag)

let has_equal = 0

" remove newlines and trailing spaces
    let @e = substitute (@e, "\n", "",   "g")
    let @e = substitute (@e, '\s*$', "", "g")

" if we end with an equal, strip, and remember for output
    if @e =~ "=$"
        let @e = substitute (@e, '=$', "", "")
        let has_equal = 1
    endif

" if there is another equal in the line, assume chained equations, remove leading ones
    let @e = substitute (@e, '^.\+=', '', '')

let answer = Calculate (@e,a:flag)

" append answer or echo
    if has_equal == 1
        exec "normal a " . answer
    else
        exec "normal a " . "= " . answer
        "" echo "answer = " . answer
    endif
endfunction

" ---------------------------------------------------------------------
" Check_hex:
"
" Check if the string contains 0x, a, b, c, d, e, f  return has_hex=1
function! Check_hex(str)
    let has_hex = 0
    let ss = a:str
    let ss = tolower ( ss )

if ss =~ "0x"
        let has_hex = 1
        return has_hex
    endif

if ss =~ "[abcdef]"
        let has_hex = 1
        return has_hex
    endif

endfunction

完善vim bccalc_linux插件相关推荐

  1. vim插件的安装方式 -- vim注释插件和doxygen函数注释生成插件-ctrlp插件-tabular等号对齐 插件...

    使用unzip的时候 指定 -d选项, 是说明解压到的 目标地址. 这个参数还是比较方便的, 比直接unzip到当前目录, 然后在去拷贝到目标目录, 然后再删除当前目录中的解压文件夹, 方便多了. 使 ...

  2. Vim Vundle 插件管理器

    /*********************************************************************** Vim Vundle 插件管理器* 说明:* 话说Vi ...

  3. Vim安装插件在命令行下看代码

    这几天我又想抛弃source insight 了,主要是太慢了 安装如下 VIM万能插件 或者使用 sudo apt-get installexuberant-ctags 安装 我主要是使用函数跳转功 ...

  4. vim模板插件vim-template的使用

    vim模板插件vim-template的使用 之前使用IDE编程,模板是最基本的功能,现在切换到vim,用惯了模板的我,对于每次写代码都来上 # -*- coding: utf-8 -*- # # 日 ...

  5. vim golang 插件

    最好用的vim golang 插件 可自动缩进 git clone git@github.com:aimin/InstallvimGo.git 转载于:https://www.cnblogs.com/ ...

  6. 【Linux vim 入门及渐进过程2 - vim 常用插件配置】

    文章目录 1.1 vim 常用配置 1.1.2 vim 别名 alias 配置 1.1.3 vim 终端颜色配置 1.1.3 vim cscope 函数配置 1.1.4 vimrc 配置 1.2 vi ...

  7. vim常用插件安装及使用

    vim常用插件安装及使用 vim常用插件安装 一. Vim8内置插件管理方案 二.vim插件推荐及安装 2.1 NERDTree插件安装及使用 2.1.1 下载NERDTree插件 2.1.2 NER ...

  8. Mac下安装vim的插件YouCompleteMe及注意事项

    mac下的YouCompleteMe安装还是很麻烦的,所以我安装完之后写下此篇blog,以供诸位一起学习 此篇借鉴了网上一些文章的情况下结合了我自己的一点心得: 1.基本准备(我默认已经安装好了以下软 ...

  9. 神级编辑器 Vim 使用-插件篇

    在这篇中, 会列举各种实用的插件, 包括他们的安装, 配置及使用方法 注意: 不是本部分的所有插件都是你需要装的, 如果盲目安装插件只会导致你 vim 功能混乱, 速度底下, 所以适时整理真正需要的插 ...

最新文章

  1. 压缩可以卸载吗_不可错过!螺杆压缩机故障分析详解(2)
  2. node.js 搭建blog
  3. codevs1251 括号
  4. GetClientRect()和GetWindowRect()
  5. mac下使用自带的apache与php
  6. WinPcap pcap_next_ex抓包
  7. feign 能干什么:
  8. 电子商务实战课程-张晨光-专题视频课程
  9. QMainWindow中的布局管理
  10. Linux ${}表达式详解
  11. Windows Forms 实现安全的多线程详解
  12. Firefox下Add-ons推荐
  13. 大数据工程师必备之蓄水池抽样算法
  14. 方舟生存进化掉落物代码
  15. HP LaserJet 1010 安装方法
  16. Linux网络协议之旅,用西游记的故事串联整个网络世界(必看!)
  17. 我儿喜欢摸奶奶 什么原因_我是奶奶,我的编码职业才刚刚开始
  18. 专家建议将“元宇宙”更名为“元网络”或“灵境网络” 回归本质 规范发展
  19. 解决报错:OSError: Failed to open file b‘D:\\\xe5\xad\xa6\xe4\xb9\xa0\\scipy-_7cm39vc‘(图文并茂版详细版!!)
  20. 精灵与精灵组的常用属性

热门文章

  1. tp3.2php开启事务,ThinkPHP 3.2.2实现事务操作的方法
  2. python中立方表示_在Python中表示一个对象的方法
  3. 目前最常用的计算机机箱类型为_常用的计算机设备
  4. Prim和Dijkstra居然写起来一模一样
  5. Linux 将文件打包、压缩并分割成指定大小
  6. 十种经济的方法帮你建立企业品牌
  7. 【Java例题】5.3 线性表的使用
  8. 1 客户端性能--浏览器页面处理
  9. Raw 暗场校准 II
  10. apache+php安装配置