home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / vim53os2.zip / vim-5.3 / doc / tips.txt < prev    next >
Text File  |  1998-08-30  |  18KB  |  438 lines

  1. *tips.txt*      For Vim version 5.3.  Last modification: 1998 Aug 23
  2.  
  3.  
  4.           VIM REFERENCE MANUAL    by Bram Moolenaar
  5.  
  6.  
  7. Tips and ideas for using Vim                *tips*
  8.  
  9. Editing C programs                |C-editing|
  10. Finding where identifiers are used        |ident-search|
  11. Editing local HTML files (WWW)            |html-editing|
  12. Editing paragraphs without a line break        |edit-no-break|
  13. Switching screens in an xterm            |xterm-screens|
  14. Scrolling in Insert mode            |scroll-insert|
  15. Smooth scrolling                |scroll-smooth|
  16. Correcting common typing mistakes        |type-mistakes|
  17. Counting words, lines, etc.            |count-items|
  18. Renaming files                    |rename-files|
  19. Speeding up external commands            |speed-up|
  20. Useful mappings                    |useful-mappings|
  21. Compressing the help files            |gzip-helpfile|
  22. Executing shell commands in a window        |shell-window|
  23. Using <> notation in autocommands        |autocmd-<>|
  24.  
  25. ==============================================================================
  26. Editing C programs                    *C-editing*
  27.  
  28. There are quite a few features in Vim to help you edit C program files.  Here
  29. is an overview with tags to jump to:
  30.  
  31. |C-indenting|        Automatically set the indent of line while typing
  32.             text.
  33. |=|            Re-indent a few lines.
  34. |format-comments|    Format comments.
  35.  
  36. |:checkpath|        Show all recursively included files.
  37. |[i|            Search for identifier under cursor in current and
  38.             included files.
  39. |[_CTRL-I|        Jump to match for "[i"
  40. |[I|            List all lines in current and included files where
  41.             identifier under the cursor matches.
  42. |[d|            Search for define under cursor in current and included
  43.             files.
  44.  
  45. |CTRL-]|        Jump to tag under cursor.
  46. |CTRL-T|        Jump back from tag.
  47. |:tselect|        Select one of a list of matching tags.
  48.  
  49. |gd|            Go to Declaration of local variable under cursor.
  50. |gD|            Go to Declaration of global variable under cursor.
  51.  
  52. |gf|            Go to file name under the cursor.
  53.  
  54. |%|            Go to matching (), {}, [], /* */, #if, #else, #endif.
  55. |[/|            Go to previous start of comment.
  56. |]/|            Go to next end of comment.
  57. |[#|            Go back to unclosed #if, #ifdef, or #else.
  58. |]#|            Go forward to unclosed #else or #endif.
  59. |[(|            Go back to unclosed '('
  60. |])|            Go forward to unclosed ')'
  61. |[{|            Go back to unclosed '{'
  62. |]}|            Go forward to unclosed '}'
  63.  
  64. |v_ab|            Select "a block" from "[(" to "])", including braces
  65. |v_ib|            Select "inner block" from "[(" to "])"
  66. |v_aB|            Select "a block" from "[{" to "]}", including brackets
  67. |v_iB|            Select "inner block" from "[{" to "]}"
  68.  
  69. ==============================================================================
  70. Finding where identifiers are used            *ident-search*
  71.  
  72. Sometimes you wish you could jump to where a function or variable is being
  73. used.  This is possible in two ways:
  74. 1. Using "grep" and quickfix commands.  This should work on most Unix systems,
  75.    but can be slow and only searches in one directory.
  76. 2. Using ID utils.  This is fast and works in multiple directories.  You will
  77.    need some additional programs for this to work.
  78.  
  79. 1. Using grep.
  80.  
  81. Add one long line to your .vimrc:
  82. >  map _g :let efsave=&ef<Bar>let &ef=tempname()<Bar>exe ':!grep -n -w "<cword>" *.[ch] *.txt >'.&ef<CR>:cf<CR>:exe ":!rm ".&ef<CR>:let &ef=efsave<Bar>unlet efsave<CR><CR>:cc<CR>
  83.  
  84. NOTE: This requires that the '<' flag is excluded from 'cpoptions'.
  85.  
  86. That's all!  You can use this by placing the cursor on any identifier, and
  87. hitting "_g".  Then go to further matches with ":cn".  ":cc" and ":cp" can be
  88. used to move around in the list of matches.  Use ":cl" to see all matches.
  89. Also see |quickfix|.
  90.  
  91. This depends on the format of what grep produces and 'errorformat'.  It should
  92. work with the default settings.
  93.  
  94. Not all versions of "grep" accept the "-w" argument.  You can try if this
  95. works instead:
  96. >    :map _g :!grep -n "\<<cword>\>" *.[ch] >errors.vim<CR>:cf errors.vim<CR>
  97.  
  98.  
  99. 2. With a combination of Vim and the GNU id-tools.
  100.  
  101. This is still is a rather simple tool, but it works.
  102.  
  103. What you need:
  104. - Vim 5.0 or later.
  105. - The GNU id-tools installed (mkid is needed to create ID and lid is needed to
  106.   use the macros).
  107. - An identifier database file called "ID" in the current directory.
  108.  
  109. Put this in your .vimrc:
  110. >    map _u :source $HOME/.vim/such.vim<CR>
  111. >    map _n :n<CR>n
  112.  
  113. Put this in the "$HOME/.vim/such.vim" file:
  114. >    " such.vim
  115. >    " lets you find arbitrary strings from the GNU mkid utility
  116. >    " kind of a reverse tags system
  117. >    let o = expand("<cword>")
  118. >    execute "let b = \"`lid --key=none " o "`\""
  119. >    let x = expand( b )
  120. >    execute "n " . x
  121. >    execute "/\\<".o."\\>"
  122.  
  123. To use it, just step on a word with the cursor, type "_u" and vim should load
  124. the file[s] that contain the word.  Search for the next occurence with "n" and
  125. go to the next file with "_n".
  126.  
  127. This has been tested with id-utils-3.2 (which is the name name of the id-tools
  128. archive file on your closest gnu-ftp-mirror).
  129.  
  130. [the idea for this comes from Andreas Kutschera]
  131.  
  132. ==============================================================================
  133. Editing local HTML files (WWW)                *html-editing*
  134.  
  135. Vim has some features which can help simplify the creation and maintenance of
  136. HTML files, provided that the files you are editing are available on the local
  137. file system.  The "]f", "gf" and "CTRL-W f" commands can be used to jump to
  138. the file whose name appears under the cursor, thus not only checking that the
  139. link is valid (at least the file name part of the URL) but also providing a
  140. quick and easy way to edit many related HTML pages at once.  |gf|
  141. A set of macros to help with editing html can be found on the Vim pages. |www|
  142.  
  143. If you want to view your HTML file, after writing a new version with Vim, have
  144. a look at the "atchange" program.  It can be used to automatically start a
  145. viewer when the last-changed date/time of a file changes.  You can find info
  146. at: <URL:http://www-lmmb.ncifcrf.gov/~toms/atchange.html>
  147.  
  148. ==============================================================================
  149. Editing paragraphs without a line break            *edit-no-break*
  150.  
  151. If you are typing text in Vim that will later go to a word processor, it is
  152. useful to keep a paragraph as a single line.  To make this more easy:
  153. - set 'wrap' on, to make lines wrap                |'wrap'|
  154. - set 'linebreak' on, to make wrapping happen at a blank    |'linebreak'|
  155. - set 'textwidth' to zero, to avoid linebreaks            |'tw'|
  156. - use "gk" and "gj" to move one screen line up or down        |gj|
  157.  
  158. ==============================================================================
  159. Switching screens in an xterm        *xterm-screens* *xterm-save-screen*
  160.  
  161. (From comp.editors, by Juergen Weigert, in reply to a question)
  162.  
  163. :> Another question is that after exiting vim, the screen is left as it
  164. :> was, i.e. the contents of the file I was viewing (editting) was left on
  165. :> the screen. The output from my previous like "ls" were lost,
  166. :> ie. no longer in the scrolling buffer. I know that there is a way to
  167. :> restore the screen after exiting vim or other vi like editors,
  168. :> I just don't know how. Helps are appreciated. Thanks.
  169. :
  170. :I imagine someone else can answer this.  I assume though that vim and vi do
  171. :the same thing as each other for a given xterm setup.
  172.  
  173. They not necessarily do the same thing, as this may be a termcap vs.
  174. terminfo problem. You should be aware that there are two databases for
  175. describing attributes of a particular type of terminal: termcap and
  176. terminfo. This can cause differences when the entries differ AND when of
  177. the programs in question one uses terminfo and the other uses termcap
  178. (also see |+terminfo|).
  179.  
  180. In your particular problem, you are looking for the control sequences
  181. ^[[?47h and ^[[?47l. These switch between xterms alternate and main screen
  182. buffer. As a quick workaround a command sequence like
  183. >    echo -n "^[[?47h"; vim ... ; echo -n "^[[?47l"
  184. may do what you want. (My notation ^[ means the ESC character, further down
  185. you'll see that the databases use \E instead).
  186.  
  187. On startup, vim echoes the value of the termcap variable ti (terminfo:
  188. smcup) to the terminal. When exiting, it echoes te (terminfo: rmcup). Thus
  189. these two variables are the correct place where the above mentioned control
  190. sequences should go.
  191.  
  192. Compare your xterm termcap entry (found in /etc/termcap) with your xterm
  193. terminfo entry (retrieved with /usr/5bin/infocmp -C xterm). Both should
  194. contain entries similar to:
  195. >    :te=\E[2J\E[?47l\E8:ti=\E7\E[?47h:
  196.  
  197. PS: If you find any difference, someone (your sysadmin?) should better check
  198.     the complete termcap and terminfo database for consistency.
  199.  
  200. NOTE 1: If you recompile Vim with SAVE_XTERM_SCREEN defined in feature.h, the
  201. builtin xterm will include the mentioned "te" and "ti" entries.
  202.  
  203. NOTE 2: If you want to disable the screen switching, and you don't want to
  204. change your termcap, you can add these lines to your .vimrc:
  205. >    :set t_ti= t_te=
  206.  
  207. ==============================================================================
  208. Scrolling in Insert mode                *scroll-insert*
  209.  
  210. If you are in insert mode and you want to see something that is just off the
  211. screen, you can use CTRL-X CTRL-E and CTRL-X CTRL-Y to scroll the screen.
  212.                         |i_CTRL-X_CTRL-E|
  213.  
  214. To make this easier, you could use these mappings:
  215. >    :inoremap ^E ^X^E
  216. >    :inoremap ^Y ^X^Y
  217. (Type CTRL-V CTRL-E to enter ^E, CTRL-V CTRL-X to enter ^X, etc.)
  218. You then lose the ability to copy text from the line above/below the cursor
  219. |i_CTRL-E|.
  220.  
  221. Also consider setting 'scrolloff' to a larger value, so that you can always see
  222. some context around the cursor.  If 'scrolloff' is bigger than half the window
  223. height, the cursor will always be in the middle and the text is scrolled when
  224. the cursor is moved up/down.
  225.  
  226. ==============================================================================
  227. Smooth scrolling                    *scroll-smooth*
  228.  
  229. If you like the scrolling to go a bit smoother, you can use these mappings:
  230. >    :map ^U ^Y^Y^Y^Y^Y^Y^Y^Y^Y^Y^Y^Y^Y^Y^Y^Y
  231. >    :map ^D ^E^E^E^E^E^E^E^E^E^E^E^E^E^E^E^E
  232.  
  233. (Type CTRL-V CTRL-U to enter ^U, CTRL-V CTRL-Y to enter ^Y, etc.)
  234.  
  235. ==============================================================================
  236. Correcting common typing mistakes            *type-mistakes*
  237.  
  238. When there are a few words that you keep on typing in the wrong way, make
  239. abbreviations that correct them.  For example:
  240. >    :ab teh the
  241. >    :ab fro for
  242.  
  243. ==============================================================================
  244. Counting words, lines, etc.                *count-items*
  245.  
  246. To count how often any pattern occurs in a buffer, set 'report' to 0, and use
  247. the substitute command to replace the pattern with itself.  The reported
  248. number of substitutions is the number of items.  Examples:
  249.  
  250. >    :set report=0
  251. >    :%s/./&/g        characters
  252. >    :%s/\i\+/&/g        words
  253. >    :%s/^//g        lines
  254. >    :%s/the/&/g        "the"
  255.  
  256. ==============================================================================
  257. Renaming files                        *rename-files*
  258.  
  259. Say I have a directory with the following files in them (directory picked at
  260. random :-):
  261.  
  262. buffer.c
  263. charset.c
  264. digraph.c
  265. ...
  266.  
  267. and I want to rename *.c *.bla.  I'd do it like this:
  268.  
  269. >    $ vim
  270. >    :r! ls *.c
  271. >    :%s/\(.*\).c/mv & \1.bla
  272. >    :w !sh
  273. >    :q!
  274.  
  275. ==============================================================================
  276. Speeding up external commands                *speed-up*
  277.  
  278. In some situations, execution of an external command can be very slow.  This
  279. can also slow down wildcard expansion on Unix.  Here are a few suggestions to
  280. increase the speed.
  281.  
  282. If your .cshrc (or other file, depending on the shell used) is very long, you
  283. should separate it into a section for interactive use and a section for
  284. non-interactive use (often called secondary shells).  When you execute a
  285. command from Vim like ":!ls", you do not need the interactive things (for
  286. example, setting the prompt).  Put the stuff that is not needed after these
  287. lines:
  288.  
  289. >    if ($?prompt == 0) then
  290. >        exit 0
  291. >    endif
  292.  
  293. Another way is to include the "-f" flag in the 'shell' option, e.g.:
  294.  
  295. >    :set shell=csh\ -f
  296.  
  297. (the backslash is needed to include the space in the option).
  298. This will make csh completely skip the use of the .cshrc file.  This may cause
  299. some things to stop working though.
  300.  
  301. ==============================================================================
  302. Useful mappings                        *useful-mappings*
  303.  
  304. Here are a few mappings that some people like to use.
  305.  
  306. >    :map ' `
  307. Make the single quote work like a backtick.  Puts the cursor on the column of
  308. a mark, instead of going to the first non-blank character in the line.
  309.  
  310.                             *emacs-keys*
  311. For Emacs-style editing on the command-line:
  312. >    " start of line
  313. >    :cnoremap <C-A>        <Home>
  314. >    " back one character
  315. >    :cnoremap <C-B>        <Left>
  316. >    " delete character under cursor
  317. >    :cnoremap <C-D>        <Del>
  318. >    " end of line
  319. >    :cnoremap <C-E>        <End>
  320. >    " forward one character
  321. >    :cnoremap <C-F>        <Right>
  322. >    " recall newer command-line
  323. >    :cnoremap <C-N>        <Down>
  324. >    " recall previous (older) command-line
  325. >    :cnoremap <C-P>        <Up>
  326. >    " back one word
  327. >    :cnoremap <Esc><C-B>    <S-Left>
  328. >    " forward one word
  329. >    :cnoremap <Esc><C-F>    <S-Right>
  330.  
  331. NOTE: this requires that the '<' flag is excluded from 'cpoptions'.
  332.  
  333.                             *format-bullet-list*
  334. This mapping will format any bullet list.  It requires that there is an empty
  335. line above and below each list entry.  The expression commands are used to
  336. be able to give comments to the parts of the mapping.
  337.  
  338. >    let m =     ":map _f  :set ai<CR>"    " need 'autoindent' set
  339. >    let m = m . "{O<Esc>"              " add empty line above item
  340. >    let m = m . "}{)^W"              " move to text after bullet
  341. >    let m = m . "i     <CR>     <Esc>"    " add space for indent
  342. >    let m = m . "gq}"              " format text after the bullet
  343. >    let m = m . "{dd"              " remove the empty line
  344. >    let m = m . "5lDJ"              " put text after bullet
  345. >    execute m                  |" define the mapping
  346.  
  347. (<> notation |<>|.  Note that this is all typed literally.  ^W is "^" "W", not
  348. CTRL-W.  You can copy/paste this into Vim if '<' is not included in
  349. 'cpoptions')
  350.  
  351. Note that the last comment starts with |", because the ":execute" command
  352. doesn't accept a comment directly.
  353.  
  354. You also need to set 'textwidth' to a non-zero value, e.g.,
  355. >    set tw=70
  356.  
  357. A mapping that does about the same, but takes the indent for the list from the
  358. first line (Note: this mapping is a single long line with a lot of spaces):
  359. >    :map _f :set ai<CR>}{a                                                          <Esc>WWmmkD`mi<CR><Esc>kkddpJgq}'mJO<Esc>j
  360.  
  361. To count the number of words in the current buffer:
  362. >    :map _wc :%s/[^ <Tab>]\+/&/g<CR>``
  363. The number of changes reported is the number of words.  You can do "u" to undo
  364. the substitute (although the text isn't really modified).
  365.  
  366. ==============================================================================
  367. Compressing the help files                *gzip-helpfile*
  368.  
  369. For those of you who are really short on disk space, you can compress the help
  370. files and still be able to view them with Vim.  This makes accessing the help
  371. files a bit slower and requires the "gzip" program.
  372.  
  373. (1) Compress all the help files: "gzip doc/*.txt".
  374.  
  375. (2) Edit "doc/tags" and change the ".txt" to ".txt.gz":
  376. >    :%s=\(\t.*\.txt\)\t=\1.gz\t=
  377.  
  378. (3) Add these lines to your vimrc:
  379. >    augroup helpgzip
  380. >    autocmd!
  381. >    autocmd BufReadPre  *.txt.gz set bin
  382. >    autocmd BufReadPost *.txt.gz let ch_save = &ch|set ch=2
  383. >    autocmd BufReadPost *.txt.gz '[,']!gunzip
  384. >    autocmd BufReadPost *.txt.gz set nobin
  385. >    autocmd BufReadPost *.txt.gz let &ch = ch_save|unlet ch_save
  386. >    autocmd BufReadPost *.txt.gz execute ":doautocmd BufReadPost " . expand("%:r")
  387. >    augroup END
  388. >    set helpfile=<dirname>/help.txt.gz
  389.  
  390. Where <dirname> is the directory where the help files are.  If you have
  391. already included autocommands to edit ".gz" files (from $VIM/vimrc_example),
  392. you should omit the three "autocmd" lines.  Setting 'cmdheight' to two is to
  393. avoid the |hit-return| prompt; it is not mandatory.  You must also make sure
  394. that $VIM is set to where the other Vim files are, when they are not in the
  395. same location as the compressed "doc" directory.  See |$VIM|.
  396.  
  397. ==============================================================================
  398. Executing shell commands in a window            *shell-window*
  399.  
  400. There have been questions for the possibility to execute a shell in a window
  401. inside Vim.  The answer: you can't!  Including this would add a lot of code to
  402. Vim, which is a good reason not to do this.  After all, Vim is an editor, it
  403. is not supposed to do non-editing tasks.  However, to get something like this,
  404. you might try splitting your terminal screen or display window with the
  405. "splitvt" program.  You can probably find it on some ftp server.  The person
  406. that knows more about this is Sam Lantinga (<slouken@cs.ucdavis.edu>).
  407.  
  408. ==============================================================================
  409. Using <> notation in autocommands            *autocmd-<>*
  410.  
  411. The <> notation is not recognized in the argument of an :autocmd.  To avoid
  412. having to use special characters, you could use a self-destroying mapping to
  413. get the <> notation and then call the mapping from the autocmd.  Example:
  414.  
  415.                     *buffer-menu* *map-self-destroy*
  416. > " This is for automatically adding the name of the file to the menu list.
  417. > " It uses a self-destroying mapping!
  418. > " 1. use a line in the buffer to convert the 'dots' in the file name to \.
  419. > " 2. store that in register '"'
  420. > " 3. add that name to the Buffers menu list
  421. > " WARNING: this does have some side effects, like overwriting the
  422. > " current register contents and removing any mapping for the "i" command.
  423. > "
  424. > autocmd BufNewFile,BufReadPre * nmap i :nunmap i<CR>O<C-R>%<Esc>:.g/\./s/\./\\./g<CR>0"9y$u:menu Buffers.<C-R>9 :buffer <C-R>%<C-V><CR><CR>
  425. > autocmd BufNewFile,BufReadPre * normal i
  426.  
  427. Another method, perhaps better, is to use the ":execute" command.  In the
  428. string you can use the <> notation by preceding it with a backslash.  Don't
  429. forget to double the number of existing backslashes and put a backslash before
  430. '"'.
  431.  
  432. > autocmd BufNewFile,BufReadPre * exe "normal O\<C-R>%\<Esc>:.g/\\./s/\\./\\\\./g\<CR>0\"9y$u:menu Buffers.\<C-R>9 :buffer \<C-R>%\<C-V>\<CR>\<CR>"
  433.  
  434. For a real buffer menu, user functions should be used |:function|, but then
  435. the <> notation isn't used, which defeats using it as an example here.
  436.  
  437.  vim:ts=8:sw=8:tw=78:
  438.