*vim_tips.txt* For Vim version 4.5. Last modification: 1996 Aug 30 Contents: Editing C programs |C-editing| Editing local HTML files (WWW) |html-editing| Editing paragraphs without a line break |edit-no-break| Switching screens in an xterm |xterm-screens| Scrolling in Insert mode |scroll-insert| Smooth scrolling |scroll-smooth| Correcting common typing mistakes |type-mistakes| Renaming files |rename-files| Speeding up external commands |speed-up| Useful mappings |useful-mappings| Compressing the help files |gzip-helpfile| Executing shell commands in a window |shell_window| Pseudo-Ex mode |pseudo-Q| Using <> notation in autocommands |autocmd-<>| Editing C programs *C-editing* ================== There are quite a few features in Vim to help you edit C program files. Here is an overview with tags to jump to: |C_indenting| Automatically set the indent of line while typing text. |=| Re-indent a few lines. |format_comments| Format comments. |:checkpath| Show all recursively included files. |[i| Search for identifier under cursor in current and included files. |[_CTRL-I| Jump to match for "[i" |[I| List all lines in current and included files where identifier under the cursor matches. |[d| Search for define under cursor in current and included files. |CTRL-]| Jump to tag under cursor. |CTRL-T| Jump back from tag. |gd| Go to Declaration of local variable under cursor. |gD| Go to Declaration of global variable under cursor. |gf| Go to file name under the cursor. |%| Go to matching (), {}, [], /* */, #if, #else, #endif. |[/| Go to previous start of comment. |]/| Go to next end of comment. |[#| Go back to unclosed #if, #ifdef, or #else. |]#| Go forward to unclosed #else or #endif. |[(| Go back to unclosed '(' |])| Go forward to unclosed ')' |[{| Go back to unclosed '{' |]}| Go forward to unclosed '}' |v_S| Select current block from "[(" to "])" |v_P| Select current block from "[{" to "]}" Editing local HTML files (WWW) *html-editing* ============================== Vim has some features which can help simplify the creation and maintenance of HTML files, provided that the files you are editing are available on the local file system. The "]f", "gf" and "CTRL-W f" commands can be used to jump to the file whose name appears under the cursor, thus not only checking that the link is valid (at least the file name part of the URL) but also providing a quick and easy way to edit many related HTML pages at once. |gf| A set of macros to help with editing html can be found on the Vim pages. |www| Editing paragraphs without a line break *edit-no-break* ======================================= If you are typing text in Vim that will later go to a word processor, it is useful to keep a paragraph as a single line. To make this more easy: - set 'wrap' on, to make lines wrap |'wrap'| - set 'linebreak' on, to make wrapping happen at a blank |'linebreak'| - use "gk" and "gj" to move one screen line up or down |gj| Switching screens in an xterm *xterm-screens* ============================= (From comp.editors, by Juergen Weigert, in reply to a question) >> Another question is that after exiting vim, the screen is left as it >> was, i.e. the contents of the file I was viewing (editting) was left on >> the screen. The output from my previous like "ls" were lost, >> ie. no longer in the scrolling buffer. I know that there is a way to >> restore the screen after exiting vim or other vi like editors, >> I just don't know how. Helps are appreciated. Thanks. >I imagine someone else can answer this. I assume though that vim and vi do >the same thing as each other for a given xterm setup. They not necessarily do the same thing, as this may be a termcap vs. terminfo problem. You should be aware that there are two databases for describing attributes of a particular type of terminal: termcap and terminfo. This can cause differences when the entries differ *and* when of the programs in question one uses terminfo and the other uses termcap. In your particular problem, you are looking for the control sequences ^[[?47h and ^[[?47l. These switch between xterms alternate and main screen buffer. As a quick workaround a command sequence like echo -n "^[[?47h"; vim ... ; echo -n "^[[?47l" may do what you want. (My notation ^[ means the ESC character, further down you'll see that the databases use \E instead). On startup, vim echoes the value of the termcap variable ti (terminfo: smcup) to the terminal. When exiting, it echoes te (terminfo: rmcup). Thus these two variables are the correct place where the above mentioned control sequences should go. Compare your xterm termcap entry (found in /etc/termcap) with your xterm terminfo entry (retrieved with /usr/5bin/infocmp -C xterm). Both should contain entries similar to: :te=\E[2J\E[?47l\E8:ti=\E7\E[?47h: PS: If you find any difference, someone (your sysadmin?) should better check the complete termcap and terminfo database for consistency. NOTE: If you recompile Vim with SAVE_XTERM_SCREEN defined in feature.h, the builtin xterm will include the mentioned "te" and "ti" entries. Scrolling in Insert mode *scroll-insert* ======================== If you are in insert mode and you want to see something that is just off the screen, you can use CTRL-X CTRL-E and CTRL-X CTRL-Y to scroll the screen. |i_CTRL-X_CTRL-E| To make this easier, you could use these mappings: :inoremap ^E ^X^E :inoremap ^Y ^X^Y (Type CTRL-V CTRL-E to enter ^E, CTRL-V CTRL-X to enter ^X, etc.) Also consider setting 'scrolloff' to a larger value, so that you can always see some context around the cursor. If 'scrolloff' is bigger than half the window height, the cursor will always be in the middle and the text is scrolled when the cursor is moved up/down. Smooth scrolling *scroll-smooth* ================ If you like the scrolling to go a bit smoother, you can use these mappings: :map ^U ^Y^Y^Y^Y^Y^Y^Y^Y^Y^Y^Y^Y^Y^Y^Y^Y :map ^D ^E^E^E^E^E^E^E^E^E^E^E^E^E^E^E^E (Type CTRL-V CTRL-U to enter ^U, CTRL-V CTRL-Y to enter ^Y, etc.) Correcting common typing mistakes *type-mistakes* ================================= When there are a few words that you keep on typing in the wrong way, make abbreviations that correct them. For example: :ab teh the :ab fro for Renaming files *rename-files* ============== Say I have a directory with the following files in them (directory picked at random :-): addcr.c alloc.c amiga.c ... and I want to rename *.c *.bla. I'd do it like this: $ vim :r! ls *.c :%s/\(.*\).c/mv & \1.bla :w !sh :q! Speeding up external commands *speed-up* ============================= In some situations, execution of an external command can be very slow. This can also slow down wildcard expansion on Unix. Here are a few suggestions to increase the speed. If your .cshrc (or other file, depending on the shell used) is very long, you should separate it into a section for interactive use and a section for non-interactive use (often called secondary shells). When you execute a command from Vim like ":!ls", you do not need the interactive things (for example, setting the prompt). Put the stuff that is not needed after these lines: if ($?prompt == 0) then exit 0 endif Another way is to include the "-f" flag in the 'shell' option, e.g.: :set shell=csh\ -f (the backslash is needed to include the space in the option). This will make csh completely skip the use of the .cshrc file. This may cause some things to stop working though. Useful mappings *useful-mappings* =============== Here are a few mappings that some people like to use. :map ' ` Make the single quote work like a backtick. Puts the cursor on the column of a mark, instead of going to the first non-blank character in the line. *emacs_keys* For Emacs-style editing on the command-line: :cnoremap ^A start of line :cnoremap ^B back one character :cnoremap ^D delete character under cursor :cnoremap ^E end of line :cnoremap ^F forward one character :cnoremap ^N recall newer command-line :cnoremap ^P recall previous (older) command-line :cnoremap ^B back one word :cnoremap ^F forward one word *format_bullet_list* This mapping will format any bullet list. It requires that there is an empty line above and below each list entry: :map _f :set ai{O}{)^Wi gq}{dd5lDJ (<> notation |<>|. Note that ^W is "^" "W", not CTRL-W. You can copy/paste this into Vim if '<' is not included in 'cpoptions') You also need to set 'textwidth' to a non-zero value, e.g., set tw=70 A mapping that does about the same, but takes the indent for the list from the first line (Note: this mapping is a single long line): :map _f :set ai}{a WWmmkD`mikkddpJgq}'mJOj Compressing the help files *gzip-helpfile* ========================== For those of you who are really short on disk space, you can compress the help files and still be able to view them with Vim. This makes accessing the help files a bit slower. (1) Compress all the help files: "gzip doc/*.txt". (2) Edit "doc/vim_tags" and do ":%s=\\.txt/=.txt.gz/=" (<> notation |<>|. You have to type a single backslash for \\ and a real Tab for ) (3) Add these lines to your vimrc: set helpfile=/vim_help.txt.gz autocmd BufReadPre *.txt.gz set bin autocmd BufReadPost *.txt.gz '[,']!gunzip autocmd BufReadPost *.txt.gz set nobin set cmdheight=2 Where is the directory where the help files are. If you have already included autocommands to edit ".gz" files, you should omit the three "autocmd" lines. Setting the command height to two is to avoid having to hit return; it is not mandatory. Executing shell commands in a window *shell_window* ==================================== There have been questions for the possibility to execute a shell in a window inside Vim. The answer: you can't! Including this would add a lot of code to Vim, which is a good reason not to do this. After all, Vim is an editor, it is not supposed to do non-editing tasks. However, to get something like this, you might try splitting your terminal screen or display window with the "splitvt" program. You can probably find it on some ftp server. The person that knows more about this is Sam Lantinga (slouken@cs.ucdavis.edu). Pseudo-Ex mode *pseudo-Q* ============== If you really miss the "Q" command from Vi a lot, you might want to try this mapping. It is far from a real Ex mode, but it looks like it. The "Q" command can still be used for formatting if you use Visual mode. You need to type "visual" to get out of Ex mode. Unfortunately, any error message does that too. nnoremap Q :cnoremap :: cabbrev visual cunmap (<> notation |<>|. You should enter this literally.) In a following version of Vim the "Q" command will be made Vi compatible. Use "gq" for formatting text. Using <> notation in autocommands *autocmd-<>* ================================= The <> notation is not recognized in the argument for :autocmd. To avoid having to use special characters, you could use a mapping to get the <> notation and then call the mapping from the autocmd. Example: *buffer-menu* " " This is for automatically adding the name of the file to the menu list " first we use the normal mode to convert the 'dots' in the filename to " \., store that in register '"' and then add that name to the Buffers menu " list. WARNING: this does have some side effects, like overwriting the " current register contents. " nmap _xaddbufmenu O%:.g/\./s/\./\\./g0"9y$u:menu Buffers.9 :buffer % autocmd BufNewFile,BufReadPre * normal _xaddbufmenu vim:ts=8:sw=8:js:tw=78: