Vim Tips Wiki
(Adjust previous/next navigation)
(Change <tt> to <code>, perhaps also minor tweak.)
 
(3 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{review}}
 
 
{{TipImported
 
{{TipImported
 
|id=872
 
|id=872
|previous=870
+
|previous=869
 
|next=873
 
|next=873
|created=February 10, 2005
+
|created=2005
 
|complexity=intermediate
 
|complexity=intermediate
 
|author=Bertram Scharpf
 
|author=Bertram Scharpf
Line 12: Line 11:
 
|category2=
 
|category2=
 
}}
 
}}
  +
When editing a file containing phone numbers, the following can be used to dial the number under the cursor. It uses a shell script (not shown) to make a modem dial the number.
I have a little script making my modem dial phone numbers. As the numbers are contained in a file I edit with Vim, I call the script with:
 
 
 
<pre>
 
<pre>
 
" Return phone number under cursor.
func CPhone( ...)
 
  +
" If no number, return an empty string (if argument is zero),
" Get phone number under cursor
 
  +
" otherwise throw an error to abort script.
let s="[-+./()0-9 ]*"
 
  +
function! CPhone(check)
let nr=matchstr( getline("."), "\\s*\\zs".s."\\%".col(".")."c".s)
 
let nr=substitute( nr, "\\s\\+$", "", "")
+
let s = '[-+./()0-9 ]*'
 
let nr = matchstr(getline('.'), '\s*\zs'.s.'\%'.col('.').'c'.s)
if nr == "" &amp;&amp; a:0 &amp;&amp; a:1
 
  +
let nr = substitute(nr, '\s\+$', '', '')
throw "No phone number under cursor."
 
  +
if a:check && empty(nr)
 
throw 'No phone number under cursor.'
 
endif
 
endif
 
return nr
 
return nr
  +
endfunction
endf
 
   
let @p=":exec \":!dial '\".CPhone(1).\"'\"\&lt;cr&gt;"
+
let @p = ":execute \":!dial '\".CPhone(1).\"'\"\<CR>"
   
 
augroup LocalUser
 
augroup LocalUser
autocmd BufRead phone* nnoremap &lt;buffer&gt; &lt;cr&gt; :exec ":!dial '".CPhone(1)."'"&lt;cr&gt;
+
autocmd BufRead phone* nnoremap <buffer> <CR> :execute ":!dial '".CPhone(1)."'"<CR>
 
augroup END
 
augroup END
 
</pre>
 
</pre>
   
  +
After sourcing the above script:
==Comments==
 
This makes no sense? Does Vim dial the number?
+
*In any file, put the cursor on a phone number and type <code>@p</code> to dial the number.
  +
*In any file with a name starting with "<code>phone</code>", put the cursor on a phone number and press Enter to dial the number.
   
  +
The shell script <code>dial</code> (not shown) uses its argument to write modem control sequences to <code>/dev/ttysS0</code> to cause the modem to dial the phone number. The script removes all non-digit characters from the number, then does something like this shell command:
----
 
  +
<pre>
My shell script 'dial' does that. I omitted it because writing "ATDT" sequences to '/dev/ttysS0' has nothing to do with Vim.
 
  +
echo -ne 'atdt,<number>;h0\r' >/dev/ttyS0
  +
</pre>
   
 
==Comments==
Roughly spoken, the script does an ":!echo -ne 'atdt,&lt;number&gt;;h0\r' &gt;/dev/ttyS0", having stripped off all non-digit characters before.
 
  +
Not many people will have a modem (or the required <code>dial</code> script) to try this tip, but there are some useful ideas that might be useful for other applications. [[User:JohnBeckett|JohnBeckett]] 10:54, May 11, 2012 (UTC)
 
----
 

Latest revision as of 05:53, 13 July 2012

Tip 872 Printable Monobook Previous Next

created 2005 · complexity intermediate · author Bertram Scharpf · version 6.0


When editing a file containing phone numbers, the following can be used to dial the number under the cursor. It uses a shell script (not shown) to make a modem dial the number.

" Return phone number under cursor.
" If no number, return an empty string (if argument is zero),
" otherwise throw an error to abort script.
function! CPhone(check)
  let s = '[-+./()0-9 ]*'
  let nr = matchstr(getline('.'), '\s*\zs'.s.'\%'.col('.').'c'.s)
  let nr = substitute(nr, '\s\+$', '', '')
  if a:check && empty(nr)
    throw 'No phone number under cursor.'
  endif
  return nr
endfunction

let @p = ":execute \":!dial '\".CPhone(1).\"'\"\<CR>"

augroup LocalUser
  autocmd BufRead phone* nnoremap <buffer> <CR> :execute ":!dial '".CPhone(1)."'"<CR>
augroup END

After sourcing the above script:

  • In any file, put the cursor on a phone number and type @p to dial the number.
  • In any file with a name starting with "phone", put the cursor on a phone number and press Enter to dial the number.

The shell script dial (not shown) uses its argument to write modem control sequences to /dev/ttysS0 to cause the modem to dial the phone number. The script removes all non-digit characters from the number, then does something like this shell command:

echo -ne 'atdt,<number>;h0\r' >/dev/ttyS0

Comments[]

Not many people will have a modem (or the required dial script) to try this tip, but there are some useful ideas that might be useful for other applications. JohnBeckett 10:54, May 11, 2012 (UTC)