Vim Tips Wiki
Register
(Added categories, clean up)
No edit summary
 
(6 intermediate revisions by one other user not shown)
Line 1: Line 1:
 
{{review}}
 
{{review}}
  +
{{TipImported
{{Tip
 
 
|id=625
 
|id=625
  +
|previous=624
|title=Typing print statements faster and more ergonomically (esp in C__PLUS____PLUS__)
 
  +
|next=626
|created=December 23, 2003 16:31
+
|created=December 23, 2003
 
|complexity=basic
 
|complexity=basic
 
|author=Kartik Agaram
 
|author=Kartik Agaram
 
|version=5.7
 
|version=5.7
 
|rating=5/6
 
|rating=5/6
  +
|category1=C
|text=
 
  +
|category2=C++
Guess what the most common word is in the C++ language? I wager it is 'cout'.
 
 
}}
 
Guess what the most common word is in the C++ language? I wager it is 'cout'.
   
Interactive debuggers and logging libraries are all very well, but most of us still have in our debugging toolboxes the technique of adding short-lived statements to our programs whose only purpose is to help us figure out the bug currently occupying us by printing the value of a variable. If you find yourself often typing such statements they are worth optimizing for.
+
Interactive debuggers and logging libraries are all very well, but most of us still have in our debugging toolboxes the technique of adding short-lived statements to our programs whose only purpose is to help us figure out the bug currently occupying us by printing the value of a variable. If you find yourself often typing such statements they are worth optimizing for.
   
For example, strings in most languages are surrounded by double quotes which require an extra motion and keystroke for the left shift key. With my coding style I multiply that motion towards the shift key by 4-6 times per print statement and 10-100 print statements everyday. The result is to significant slow me down and bring me closer to the Home for the Aged Wrist. The analogous cout statement in c++ is even more egregious in this regard. Consider statements like the following that I frequently find myself typing:
+
For example, strings in most languages are surrounded by double quotes which require an extra motion and keystroke for the left shift key. With my coding style I multiply that motion towards the shift key by 4-6 times per print statement and 10-100 print statements everyday. The result is to significant slow me down and bring me closer to the Home for the Aged Wrist. The analogous cout statement in c++ is even more egregious in this regard. Consider statements like the following that I frequently find myself typing:
   
  +
<pre>
cout &lt;&lt; "AAA: " &lt;&lt; someVarName &lt;&lt; ": " &lt;&lt; someOtherVarName &lt;&lt; "\n" ;
+
cout << "AAA: " << someVarName << ": " << someOtherVarName << "\n" ;
  +
</pre>
   
That's *12* times my left hand moves towards the shift key for *one* statement! Unacceptable. My solution is to remap keys to interchange '&lt;' and ',' as well as double quotes and single quotes. Rather than force myself to learn a new keyboard mapping within vim I cause the mappings to trigger in a context-sensitive mannter, within only a cout statement, from the time I type 'cout' to the time I type the ';' in the end.
+
That's *12* times my left hand moves towards the shift key for *one* statement! Unacceptable. My solution is to remap keys to interchange '<' and ',' as well as double quotes and single quotes. Rather than force myself to learn a new keyboard mapping within vim I cause the mappings to trigger in a context-sensitive manner, within only a cout statement, from the time I type 'cout' to the time I type the ';' in the end.
   
Here's my code fragment to do this:
+
Here's my code fragment to do this:
   
 
<pre>
 
<pre>
 
function! CppSetupCout ()
 
function! CppSetupCout ()
inoremap , <Space><<
+
inoremap , <Space><<
inoremap < ,
+
inoremap < ,
inoremap ' "
+
inoremap ' "
inoremap " '
+
inoremap " '
imap ; <Esc>:call CppResetCout ()<CR>a;
+
imap ; <Esc>:call CppResetCout ()<CR>a;
map <Esc>, :call CppResetCout ()<CR>
+
map <Esc>, :call CppResetCout ()<CR>
imap <Esc>, <C-o>:call CppResetCout ()<CR>
+
imap <Esc>, <C-o>:call CppResetCout ()<CR>
 
endf
 
endf
  +
 
function! CppResetCout ()
 
function! CppResetCout ()
iunmap ,
+
iunmap ,
iunmap <
+
iunmap <
iunmap '
+
iunmap '
iunmap "
+
iunmap "
iunmap ;
+
iunmap ;
imap <Esc>, <C-o>:call CppSetupCout ()<CR>
+
imap <Esc>, <C-o>:call CppSetupCout ()<CR>
map <Esc>, :call CppSetupCout ()<CR>
+
map <Esc>, :call CppSetupCout ()<CR>
 
endfunction
 
endfunction
   
 
function! AuCpp ()
 
function! AuCpp ()
inoremap cout <End><Esc>:call CppSetupCout ()<CR>acout <<
+
inoremap cout <End><Esc>:call CppSetupCout ()<CR>acout <<
imap <Esc>, <C-o>:call CppSetupCout ()<CR>
+
imap <Esc>, <C-o>:call CppSetupCout ()<CR>
map <Esc>, :call CppSetupCout ()<CR>
+
map <Esc>, :call CppSetupCout ()<CR>
 
endfunction
 
endfunction
   
Line 50: Line 56:
 
</pre>
 
</pre>
   
Notice that I use &lt;Esc&gt;, (or Alt-,) to quickly toggle these mappings on or off in other situations.
+
Notice that I use <Esc>, (or Alt-,) to quickly toggle these mappings on or off in other situations.
   
For some other languages it's prob sufficient to simply switch single- and double-quotes. Java might call for switching '+' and '=' within system.out.println. Come to think of it:
+
For some other languages it's probably sufficient to simply switch single- and double-quotes. Java might call for switching '+' and '=' within system.out.println. Come to think of it:
   
  +
<pre>
autocmd FileType java iabbrev sop system.out.println
+
autocmd FileType java iabbrev sop system.out.println
  +
</pre>
   
 
==Comments==
You get the idea.
 
 
My solution for C is the line in my vimrc:
}}
 
  +
<pre>
 
 
nmap _if ofprintf(0<C-d>stderr, "{%s} {%d} - \n", __FILE__, __LINE__);<Esc>F\i
== Comments ==
 
  +
</pre>
my solution for C is the line in my .vimrc:
 
 
nmap _if ofprintf(0&lt;C-d&gt;stderr, "{%s} {%d} - \n", __FILE__, __LINE__);&lt;Esc&gt;F\i
 
   
so i type _if, and then start typing the error message i want. adding __FILE__ and __LINE__ is invaluable when i come to remove the debugging i have scattered across half a dozen files. the 0&lt;C-d&gt; is so that these debug statements arent indented, which makes them easier to pick out visually and to remove.
+
so I type _if, and then start typing the error message I want. adding __FILE__ and __LINE__ is invaluable when I come to remove the debugging I have scattered across half a dozen files. the 0<C-d> is so that these debug statements aren't indented, which makes them easier to pick out visually and to remove.
   
'''Anonymous'''
 
, December 24, 2003 3:52
 
 
----
 
----
<!-- parsed by vimtips.py in 0.486229 seconds-->
 
[[Category:C]]
 
[[Category:C plus plus]]
 

Latest revision as of 01:14, 26 September 2009

Tip 625 Printable Monobook Previous Next

created December 23, 2003 · complexity basic · author Kartik Agaram · version 5.7


Guess what the most common word is in the C++ language? I wager it is 'cout'.

Interactive debuggers and logging libraries are all very well, but most of us still have in our debugging toolboxes the technique of adding short-lived statements to our programs whose only purpose is to help us figure out the bug currently occupying us by printing the value of a variable. If you find yourself often typing such statements they are worth optimizing for.

For example, strings in most languages are surrounded by double quotes which require an extra motion and keystroke for the left shift key. With my coding style I multiply that motion towards the shift key by 4-6 times per print statement and 10-100 print statements everyday. The result is to significant slow me down and bring me closer to the Home for the Aged Wrist. The analogous cout statement in c++ is even more egregious in this regard. Consider statements like the following that I frequently find myself typing:

cout << "AAA: " << someVarName << ": " << someOtherVarName << "\n" ;

That's *12* times my left hand moves towards the shift key for *one* statement! Unacceptable. My solution is to remap keys to interchange '<' and ',' as well as double quotes and single quotes. Rather than force myself to learn a new keyboard mapping within vim I cause the mappings to trigger in a context-sensitive manner, within only a cout statement, from the time I type 'cout' to the time I type the ';' in the end.

Here's my code fragment to do this:

function! CppSetupCout ()
  inoremap , <Space><<
  inoremap < ,
  inoremap ' "
  inoremap " '
  imap ; <Esc>:call CppResetCout ()<CR>a;
  map <Esc>, :call CppResetCout ()<CR>
  imap <Esc>, <C-o>:call CppResetCout ()<CR>
endf

function! CppResetCout ()
  iunmap ,
  iunmap <
  iunmap '
  iunmap "
  iunmap ;
  imap <Esc>, <C-o>:call CppSetupCout ()<CR>
  map <Esc>, :call CppSetupCout ()<CR>
endfunction

function! AuCpp ()
  inoremap cout <End><Esc>:call CppSetupCout ()<CR>acout <<
  imap <Esc>, <C-o>:call CppSetupCout ()<CR>
  map <Esc>, :call CppSetupCout ()<CR>
endfunction

autocmd FileType cpp call AuCpp()

Notice that I use <Esc>, (or Alt-,) to quickly toggle these mappings on or off in other situations.

For some other languages it's probably sufficient to simply switch single- and double-quotes. Java might call for switching '+' and '=' within system.out.println. Come to think of it:

autocmd FileType java iabbrev sop system.out.println

Comments[]

My solution for C is the line in my vimrc:

nmap _if ofprintf(0<C-d>stderr, "{%s} {%d} - \n", __FILE__, __LINE__);<Esc>F\i

so I type _if, and then start typing the error message I want. adding __FILE__ and __LINE__ is invaluable when I come to remove the debugging I have scattered across half a dozen files. the 0<C-d> is so that these debug statements aren't indented, which makes them easier to pick out visually and to remove.