Christian Brabandt | b4ddc6c | 2024-01-02 16:51:11 +0100 | [diff] [blame] | 1 | *tips.txt* For Vim version 9.1. Last change: 2023 Aug 10 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2 | |
| 3 | |
| 4 | VIM REFERENCE MANUAL by Bram Moolenaar |
| 5 | |
| 6 | |
| 7 | Tips and ideas for using Vim *tips* |
| 8 | |
Bram Moolenaar | 8f3f58f | 2010-01-06 20:52:26 +0100 | [diff] [blame] | 9 | These are just a few that we thought would be helpful for many users. |
| 10 | You can find many more tips on the wiki. The URL can be found on |
| 11 | http://www.vim.org |
| 12 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 13 | Don't forget to browse the user manual, it also contains lots of useful tips |
| 14 | |usr_toc.txt|. |
| 15 | |
| 16 | Editing C programs |C-editing| |
| 17 | Finding where identifiers are used |ident-search| |
| 18 | Switching screens in an xterm |xterm-screens| |
| 19 | Scrolling in Insert mode |scroll-insert| |
| 20 | Smooth scrolling |scroll-smooth| |
| 21 | Correcting common typing mistakes |type-mistakes| |
| 22 | Counting words, lines, etc. |count-items| |
| 23 | Restoring the cursor position |restore-position| |
| 24 | Renaming files |rename-files| |
Bram Moolenaar | 8f3f58f | 2010-01-06 20:52:26 +0100 | [diff] [blame] | 25 | Change a name in multiple files |change-name| |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 26 | Speeding up external commands |speed-up| |
| 27 | Useful mappings |useful-mappings| |
| 28 | Compressing the help files |gzip-helpfile| |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 29 | Executing shell commands in a window |shell-window| |
Bram Moolenaar | 8f3f58f | 2010-01-06 20:52:26 +0100 | [diff] [blame] | 30 | Hex editing |hex-editing| |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 31 | Using <> notation in autocommands |autocmd-<>| |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 32 | Highlighting matching parens |match-parens| |
Bram Moolenaar | 7e6a515 | 2021-01-02 16:39:53 +0100 | [diff] [blame] | 33 | Opening help in the current window |help-curwin| |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 34 | |
| 35 | ============================================================================== |
| 36 | Editing C programs *C-editing* |
| 37 | |
| 38 | There are quite a few features in Vim to help you edit C program files. Here |
| 39 | is an overview with tags to jump to: |
| 40 | |
| 41 | |usr_29.txt| Moving through programs chapter in the user manual. |
| 42 | |usr_30.txt| Editing programs chapter in the user manual. |
| 43 | |C-indenting| Automatically set the indent of a line while typing |
| 44 | text. |
| 45 | |=| Re-indent a few lines. |
| 46 | |format-comments| Format comments. |
| 47 | |
| 48 | |:checkpath| Show all recursively included files. |
| 49 | |[i| Search for identifier under cursor in current and |
| 50 | included files. |
| 51 | |[_CTRL-I| Jump to match for "[i" |
| 52 | |[I| List all lines in current and included files where |
| 53 | identifier under the cursor matches. |
| 54 | |[d| Search for define under cursor in current and included |
| 55 | files. |
| 56 | |
| 57 | |CTRL-]| Jump to tag under cursor (e.g., definition of a |
| 58 | function). |
| 59 | |CTRL-T| Jump back to before a CTRL-] command. |
| 60 | |:tselect| Select one tag out of a list of matching tags. |
| 61 | |
| 62 | |gd| Go to Declaration of local variable under cursor. |
| 63 | |gD| Go to Declaration of global variable under cursor. |
| 64 | |
| 65 | |gf| Go to file name under the cursor. |
| 66 | |
| 67 | |%| Go to matching (), {}, [], /* */, #if, #else, #endif. |
| 68 | |[/| Go to previous start of comment. |
| 69 | |]/| Go to next end of comment. |
| 70 | |[#| Go back to unclosed #if, #ifdef, or #else. |
| 71 | |]#| Go forward to unclosed #else or #endif. |
| 72 | |[(| Go back to unclosed '(' |
| 73 | |])| Go forward to unclosed ')' |
| 74 | |[{| Go back to unclosed '{' |
| 75 | |]}| Go forward to unclosed '}' |
| 76 | |
| 77 | |v_ab| Select "a block" from "[(" to "])", including braces |
| 78 | |v_ib| Select "inner block" from "[(" to "])" |
| 79 | |v_aB| Select "a block" from "[{" to "]}", including brackets |
| 80 | |v_iB| Select "inner block" from "[{" to "]}" |
| 81 | |
| 82 | ============================================================================== |
| 83 | Finding where identifiers are used *ident-search* |
| 84 | |
| 85 | You probably already know that |tags| can be used to jump to the place where a |
| 86 | function or variable is defined. But sometimes you wish you could jump to all |
| 87 | the places where a function or variable is being used. This is possible in |
| 88 | two ways: |
| 89 | 1. Using the |:grep| command. This should work on most Unix systems, |
| 90 | but can be slow (it reads all files) and only searches in one directory. |
| 91 | 2. Using ID utils. This is fast and works in multiple directories. It uses a |
| 92 | database to store locations. You will need some additional programs for |
| 93 | this to work. And you need to keep the database up to date. |
| 94 | |
| 95 | Using the GNU id-tools: |
| 96 | |
| 97 | What you need: |
| 98 | - The GNU id-tools installed (mkid is needed to create ID and lid is needed to |
| 99 | use the macros). |
| 100 | - An identifier database file called "ID" in the current directory. You can |
| 101 | create it with the shell command "mkid file1 file2 ..". |
| 102 | |
| 103 | Put this in your .vimrc: > |
Bram Moolenaar | c51cf03 | 2022-02-26 12:25:45 +0000 | [diff] [blame] | 104 | map _u :call ID_search()<Bar>execute "/\\<" .. g:word .. "\\>"<CR> |
| 105 | map _n :n<Bar>execute "/\\<" .. g:word .. "\\>"<CR> |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 106 | |
| 107 | function! ID_search() |
| 108 | let g:word = expand("<cword>") |
Bram Moolenaar | c51cf03 | 2022-02-26 12:25:45 +0000 | [diff] [blame] | 109 | let x = system("lid --key=none " .. g:word) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 110 | let x = substitute(x, "\n", " ", "g") |
Bram Moolenaar | c51cf03 | 2022-02-26 12:25:45 +0000 | [diff] [blame] | 111 | execute "next " .. x |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 112 | endfun |
| 113 | |
| 114 | To use it, place the cursor on a word, type "_u" and vim will load the file |
| 115 | that contains the word. Search for the next occurrence of the word in the |
| 116 | same file with "n". Go to the next file with "_n". |
| 117 | |
| 118 | This has been tested with id-utils-3.2 (which is the name of the id-tools |
| 119 | archive file on your closest gnu-ftp-mirror). |
| 120 | |
| 121 | [the idea for this comes from Andreas Kutschera] |
| 122 | |
| 123 | ============================================================================== |
| 124 | Switching screens in an xterm *xterm-screens* *xterm-save-screen* |
| 125 | |
| 126 | (From comp.editors, by Juergen Weigert, in reply to a question) |
| 127 | |
| 128 | :> Another question is that after exiting vim, the screen is left as it |
| 129 | :> was, i.e. the contents of the file I was viewing (editing) was left on |
| 130 | :> the screen. The output from my previous like "ls" were lost, |
| 131 | :> ie. no longer in the scrolling buffer. I know that there is a way to |
| 132 | :> restore the screen after exiting vim or other vi like editors, |
| 133 | :> I just don't know how. Helps are appreciated. Thanks. |
| 134 | : |
| 135 | :I imagine someone else can answer this. I assume though that vim and vi do |
| 136 | :the same thing as each other for a given xterm setup. |
| 137 | |
| 138 | They not necessarily do the same thing, as this may be a termcap vs. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 139 | terminfo problem. You should be aware that there are two databases for |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 140 | describing attributes of a particular type of terminal: termcap and |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 141 | terminfo. This can cause differences when the entries differ AND when of |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 142 | the programs in question one uses terminfo and the other uses termcap |
| 143 | (also see |+terminfo|). |
| 144 | |
| 145 | In your particular problem, you are looking for the control sequences |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 146 | ^[[?47h and ^[[?47l. These switch between xterms alternate and main screen |
| 147 | buffer. As a quick workaround a command sequence like > |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 148 | echo -n "^[[?47h"; vim ... ; echo -n "^[[?47l" |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 149 | may do what you want. (My notation ^[ means the ESC character, further down |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 150 | you'll see that the databases use \E instead). |
| 151 | |
| 152 | On startup, vim echoes the value of the termcap variable ti (terminfo: |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 153 | smcup) to the terminal. When exiting, it echoes te (terminfo: rmcup). Thus |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 154 | these two variables are the correct place where the above mentioned control |
| 155 | sequences should go. |
| 156 | |
| 157 | Compare your xterm termcap entry (found in /etc/termcap) with your xterm |
Bram Moolenaar | 9964e46 | 2007-05-05 17:54:07 +0000 | [diff] [blame] | 158 | terminfo entry (retrieved with "infocmp -C xterm"). Both should contain |
| 159 | entries similar to: > |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 160 | :te=\E[2J\E[?47l\E8:ti=\E7\E[?47h: |
| 161 | |
| 162 | PS: If you find any difference, someone (your sysadmin?) should better check |
| 163 | the complete termcap and terminfo database for consistency. |
| 164 | |
| 165 | NOTE 1: If you recompile Vim with FEAT_XTERM_SAVE defined in feature.h, the |
| 166 | builtin xterm will include the mentioned "te" and "ti" entries. |
| 167 | |
| 168 | NOTE 2: If you want to disable the screen switching, and you don't want to |
| 169 | change your termcap, you can add these lines to your .vimrc: > |
| 170 | :set t_ti= t_te= |
| 171 | |
| 172 | ============================================================================== |
| 173 | Scrolling in Insert mode *scroll-insert* |
| 174 | |
| 175 | If you are in insert mode and you want to see something that is just off the |
| 176 | screen, you can use CTRL-X CTRL-E and CTRL-X CTRL-Y to scroll the screen. |
| 177 | |i_CTRL-X_CTRL-E| |
| 178 | |
| 179 | To make this easier, you could use these mappings: > |
| 180 | :inoremap <C-E> <C-X><C-E> |
| 181 | :inoremap <C-Y> <C-X><C-Y> |
| 182 | (Type this literally, make sure the '<' flag is not in 'cpoptions'). |
| 183 | You then lose the ability to copy text from the line above/below the cursor |
| 184 | |i_CTRL-E|. |
| 185 | |
| 186 | Also consider setting 'scrolloff' to a larger value, so that you can always see |
| 187 | some context around the cursor. If 'scrolloff' is bigger than half the window |
| 188 | height, the cursor will always be in the middle and the text is scrolled when |
| 189 | the cursor is moved up/down. |
| 190 | |
| 191 | ============================================================================== |
| 192 | Smooth scrolling *scroll-smooth* |
| 193 | |
| 194 | If you like the scrolling to go a bit smoother, you can use these mappings: > |
| 195 | :map <C-U> <C-Y><C-Y><C-Y><C-Y><C-Y><C-Y><C-Y><C-Y><C-Y><C-Y><C-Y><C-Y><C-Y><C-Y><C-Y><C-Y> |
| 196 | :map <C-D> <C-E><C-E><C-E><C-E><C-E><C-E><C-E><C-E><C-E><C-E><C-E><C-E><C-E><C-E><C-E><C-E> |
| 197 | |
| 198 | (Type this literally, make sure the '<' flag is not in 'cpoptions'). |
| 199 | |
| 200 | ============================================================================== |
| 201 | Correcting common typing mistakes *type-mistakes* |
| 202 | |
| 203 | When there are a few words that you keep on typing in the wrong way, make |
| 204 | abbreviations that correct them. For example: > |
| 205 | :ab teh the |
| 206 | :ab fro for |
| 207 | |
| 208 | ============================================================================== |
| 209 | Counting words, lines, etc. *count-items* |
| 210 | |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 211 | To count how often any pattern occurs in the current buffer use the substitute |
| 212 | command and add the 'n' flag to avoid the substitution. The reported number |
| 213 | of substitutions is the number of items. Examples: > |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 214 | |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 215 | :%s/./&/gn characters |
| 216 | :%s/\i\+/&/gn words |
| 217 | :%s/^//n lines |
| 218 | :%s/the/&/gn "the" anywhere |
| 219 | :%s/\<the\>/&/gn "the" as a word |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 220 | |
| 221 | You might want to reset 'hlsearch' or do ":nohlsearch". |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 222 | Add the 'e' flag if you don't want an error when there are no matches. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 223 | |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 224 | An alternative is using |v_g_CTRL-G| in Visual mode. |
| 225 | |
| 226 | If you want to find matches in multiple files use |:vimgrep|. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 227 | |
| 228 | *count-bytes* |
| 229 | If you want to count bytes, you can use this: |
| 230 | |
| 231 | Visually select the characters (block is also possible) |
| 232 | Use "y" to yank the characters |
| 233 | Use the strlen() function: > |
| 234 | :echo strlen(@") |
| 235 | A line break is counted for one byte. |
| 236 | |
| 237 | ============================================================================== |
| 238 | Restoring the cursor position *restore-position* |
| 239 | |
| 240 | Sometimes you want to write a mapping that makes a change somewhere in the |
| 241 | file and restores the cursor position, without scrolling the text. For |
| 242 | example, to change the date mark in a file: > |
| 243 | :map <F2> msHmtgg/Last [cC]hange:\s*/e+1<CR>"_D"=strftime("%Y %b %d")<CR>p'tzt`s |
| 244 | |
| 245 | Breaking up saving the position: |
| 246 | ms store cursor position in the 's' mark |
| 247 | H go to the first line in the window |
| 248 | mt store this position in the 't' mark |
| 249 | |
| 250 | Breaking up restoring the position: |
| 251 | 't go to the line previously at the top of the window |
| 252 | zt scroll to move this line to the top of the window |
| 253 | `s jump to the original position of the cursor |
| 254 | |
Bram Moolenaar | 8f3f58f | 2010-01-06 20:52:26 +0100 | [diff] [blame] | 255 | For something more advanced see |winsaveview()| and |winrestview()|. |
| 256 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 257 | ============================================================================== |
| 258 | Renaming files *rename-files* |
| 259 | |
| 260 | Say I have a directory with the following files in them (directory picked at |
| 261 | random :-): |
| 262 | |
| 263 | buffer.c |
| 264 | charset.c |
| 265 | digraph.c |
| 266 | ... |
| 267 | |
| 268 | and I want to rename *.c *.bla. I'd do it like this: > |
| 269 | |
| 270 | $ vim |
Bram Moolenaar | fe5cafe | 2005-04-19 21:37:59 +0000 | [diff] [blame] | 271 | :r !ls *.c |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 272 | :%s/\(.*\).c/mv & \1.bla |
| 273 | :w !sh |
| 274 | :q! |
| 275 | |
| 276 | ============================================================================== |
Bram Moolenaar | 8f3f58f | 2010-01-06 20:52:26 +0100 | [diff] [blame] | 277 | Change a name in multiple files *change-name* |
| 278 | |
| 279 | Example for using a script file to change a name in several files: |
| 280 | |
| 281 | Create a file "subs.vim" containing substitute commands and a :update |
| 282 | command: > |
| 283 | :%s/Jones/Smith/g |
| 284 | :%s/Allen/Peter/g |
| 285 | :update |
| 286 | < |
| 287 | Execute Vim on all files you want to change, and source the script for |
| 288 | each argument: > |
| 289 | |
| 290 | vim *.let |
| 291 | argdo source subs.vim |
| 292 | |
| 293 | See |:argdo|. |
| 294 | |
| 295 | ============================================================================== |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 296 | Speeding up external commands *speed-up* |
| 297 | |
| 298 | In some situations, execution of an external command can be very slow. This |
| 299 | can also slow down wildcard expansion on Unix. Here are a few suggestions to |
| 300 | increase the speed. |
| 301 | |
| 302 | If your .cshrc (or other file, depending on the shell used) is very long, you |
| 303 | should separate it into a section for interactive use and a section for |
| 304 | non-interactive use (often called secondary shells). When you execute a |
| 305 | command from Vim like ":!ls", you do not need the interactive things (for |
| 306 | example, setting the prompt). Put the stuff that is not needed after these |
| 307 | lines: > |
| 308 | |
| 309 | if ($?prompt == 0) then |
| 310 | exit 0 |
| 311 | endif |
| 312 | |
| 313 | Another way is to include the "-f" flag in the 'shell' option, e.g.: > |
| 314 | |
| 315 | :set shell=csh\ -f |
| 316 | |
| 317 | (the backslash is needed to include the space in the option). |
| 318 | This will make csh completely skip the use of the .cshrc file. This may cause |
| 319 | some things to stop working though. |
| 320 | |
| 321 | ============================================================================== |
| 322 | Useful mappings *useful-mappings* |
| 323 | |
| 324 | Here are a few mappings that some people like to use. |
| 325 | |
| 326 | *map-backtick* > |
| 327 | :map ' ` |
| 328 | Make the single quote work like a backtick. Puts the cursor on the column of |
| 329 | a mark, instead of going to the first non-blank character in the line. |
| 330 | |
| 331 | *emacs-keys* |
| 332 | For Emacs-style editing on the command-line: > |
| 333 | " start of line |
| 334 | :cnoremap <C-A> <Home> |
| 335 | " back one character |
| 336 | :cnoremap <C-B> <Left> |
| 337 | " delete character under cursor |
| 338 | :cnoremap <C-D> <Del> |
| 339 | " end of line |
| 340 | :cnoremap <C-E> <End> |
| 341 | " forward one character |
| 342 | :cnoremap <C-F> <Right> |
| 343 | " recall newer command-line |
| 344 | :cnoremap <C-N> <Down> |
| 345 | " recall previous (older) command-line |
| 346 | :cnoremap <C-P> <Up> |
| 347 | " back one word |
| 348 | :cnoremap <Esc><C-B> <S-Left> |
| 349 | " forward one word |
| 350 | :cnoremap <Esc><C-F> <S-Right> |
| 351 | |
| 352 | NOTE: This requires that the '<' flag is excluded from 'cpoptions'. |<>| |
| 353 | |
| 354 | *format-bullet-list* |
| 355 | This mapping will format any bullet list. It requires that there is an empty |
| 356 | line above and below each list entry. The expression commands are used to |
| 357 | be able to give comments to the parts of the mapping. > |
| 358 | |
Bram Moolenaar | c51cf03 | 2022-02-26 12:25:45 +0000 | [diff] [blame] | 359 | :let m = ":map _f :set ai<CR>" " need 'autoindent' set |
| 360 | :let m ..= "{O<Esc>" " add empty line above item |
| 361 | :let m ..= "}{)^W" " move to text after bullet |
| 362 | :let m ..= "i <CR> <Esc>" " add space for indent |
| 363 | :let m ..= "gq}" " format text after the bullet |
| 364 | :let m ..= "{dd" " remove the empty line |
| 365 | :let m ..= "5lDJ" " put text after bullet |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 366 | :execute m |" define the mapping |
| 367 | |
| 368 | (<> notation |<>|. Note that this is all typed literally. ^W is "^" "W", not |
| 369 | CTRL-W. You can copy/paste this into Vim if '<' is not included in |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 370 | 'cpoptions'.) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 371 | |
| 372 | Note that the last comment starts with |", because the ":execute" command |
| 373 | doesn't accept a comment directly. |
| 374 | |
| 375 | You also need to set 'textwidth' to a non-zero value, e.g., > |
| 376 | :set tw=70 |
| 377 | |
| 378 | A mapping that does about the same, but takes the indent for the list from the |
| 379 | first line (Note: this mapping is a single long line with a lot of spaces): > |
| 380 | :map _f :set ai<CR>}{a <Esc>WWmmkD`mi<CR><Esc>kkddpJgq}'mJO<Esc>j |
| 381 | < |
| 382 | *collapse* |
| 383 | These two mappings reduce a sequence of empty (;b) or blank (;n) lines into a |
| 384 | single line > |
| 385 | :map ;b GoZ<Esc>:g/^$/.,/./-j<CR>Gdd |
| 386 | :map ;n GoZ<Esc>:g/^[ <Tab>]*$/.,/[^ <Tab>]/-j<CR>Gdd |
| 387 | |
| 388 | ============================================================================== |
| 389 | Compressing the help files *gzip-helpfile* |
| 390 | |
| 391 | For those of you who are really short on disk space, you can compress the help |
| 392 | files and still be able to view them with Vim. This makes accessing the help |
| 393 | files a bit slower and requires the "gzip" program. |
| 394 | |
| 395 | (1) Compress all the help files: "gzip doc/*.txt". |
| 396 | |
| 397 | (2) Edit "doc/tags" and change the ".txt" to ".txt.gz": > |
| 398 | :%s=\(\t.*\.txt\)\t=\1.gz\t= |
| 399 | |
| 400 | (3) Add this line to your vimrc: > |
| 401 | set helpfile={dirname}/help.txt.gz |
| 402 | |
| 403 | Where {dirname} is the directory where the help files are. The |gzip| plugin |
| 404 | will take care of decompressing the files. |
| 405 | You must make sure that $VIMRUNTIME is set to where the other Vim files are, |
| 406 | when they are not in the same location as the compressed "doc" directory. See |
| 407 | |$VIMRUNTIME|. |
| 408 | |
| 409 | ============================================================================== |
| 410 | Executing shell commands in a window *shell-window* |
| 411 | |
Bram Moolenaar | 6dc819b | 2018-07-03 16:42:19 +0200 | [diff] [blame] | 412 | See |terminal|. |
| 413 | |
| 414 | Another solution is splitting your terminal screen or display window with the |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 415 | "splitvt" program. You can probably find it on some ftp server. The person |
| 416 | that knows more about this is Sam Lantinga <slouken@cs.ucdavis.edu>. |
Bram Moolenaar | 6dc819b | 2018-07-03 16:42:19 +0200 | [diff] [blame] | 417 | |
| 418 | Another alternative is the "window" command, found on BSD Unix systems, which |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 419 | supports multiple overlapped windows. Or the "screen" program, found at |
| 420 | www.uni-erlangen.de, which supports a stack of windows. |
| 421 | |
| 422 | ============================================================================== |
| 423 | Hex editing *hex-editing* *using-xxd* |
| 424 | |
| 425 | See section |23.4| of the user manual. |
| 426 | |
| 427 | If one has a particular extension that one uses for binary files (such as exe, |
| 428 | bin, etc), you may find it helpful to automate the process with the following |
| 429 | bit of autocmds for your <.vimrc>. Change that "*.bin" to whatever |
| 430 | comma-separated list of extension(s) you find yourself wanting to edit: > |
| 431 | |
| 432 | " vim -b : edit binary using xxd-format! |
| 433 | augroup Binary |
Dragan Simic' via vim_dev | 6a50066 | 2023-08-09 17:23:57 +0200 | [diff] [blame] | 434 | autocmd! |
| 435 | autocmd BufReadPre *.bin set binary |
| 436 | autocmd BufReadPost *.bin |
| 437 | \ if &binary |
| 438 | \ | execute "silent %!xxd -c 32" |
| 439 | \ | set filetype=xxd |
| 440 | \ | redraw |
| 441 | \ | endif |
| 442 | autocmd BufWritePre *.bin |
| 443 | \ if &binary |
| 444 | \ | let s:view = winsaveview() |
| 445 | \ | execute "silent %!xxd -r -c 32" |
| 446 | \ | endif |
| 447 | autocmd BufWritePost *.bin |
| 448 | \ if &binary |
| 449 | \ | execute "silent %!xxd -c 32" |
| 450 | \ | set nomodified |
| 451 | \ | call winrestview(s:view) |
| 452 | \ | redraw |
| 453 | \ | endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 454 | augroup END |
| 455 | |
| 456 | ============================================================================== |
| 457 | Using <> notation in autocommands *autocmd-<>* |
| 458 | |
| 459 | The <> notation is not recognized in the argument of an :autocmd. To avoid |
| 460 | having to use special characters, you could use a self-destroying mapping to |
| 461 | get the <> notation and then call the mapping from the autocmd. Example: |
| 462 | |
| 463 | *map-self-destroy* > |
| 464 | " This is for automatically adding the name of the file to the menu list. |
| 465 | " It uses a self-destroying mapping! |
| 466 | " 1. use a line in the buffer to convert the 'dots' in the file name to \. |
| 467 | " 2. store that in register '"' |
| 468 | " 3. add that name to the Buffers menu list |
| 469 | " WARNING: this does have some side effects, like overwriting the |
| 470 | " current register contents and removing any mapping for the "i" command. |
| 471 | " |
| 472 | 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> |
| 473 | autocmd BufNewFile,BufReadPre * normal i |
| 474 | |
| 475 | Another method, perhaps better, is to use the ":execute" command. In the |
| 476 | string you can use the <> notation by preceding it with a backslash. Don't |
| 477 | forget to double the number of existing backslashes and put a backslash before |
| 478 | '"'. |
| 479 | > |
| 480 | 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>" |
| 481 | |
| 482 | For a real buffer menu, user functions should be used (see |:function|), but |
| 483 | then the <> notation isn't used, which defeats using it as an example here. |
| 484 | |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 485 | ============================================================================== |
| 486 | Highlighting matching parens *match-parens* |
| 487 | |
| 488 | This example shows the use of a few advanced tricks: |
| 489 | - using the |CursorMoved| autocommand event |
| 490 | - using |searchpairpos()| to find a matching paren |
Bram Moolenaar | 7e8fd63 | 2006-02-18 22:14:51 +0000 | [diff] [blame] | 491 | - using |synID()| to detect whether the cursor is in a string or comment |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 492 | - using |:match| to highlight something |
| 493 | - using a |pattern| to match a specific position in the file. |
| 494 | |
| 495 | This should be put in a Vim script file, since it uses script-local variables. |
Bram Moolenaar | 7e8fd63 | 2006-02-18 22:14:51 +0000 | [diff] [blame] | 496 | It skips matches in strings or comments, unless the cursor started in string |
| 497 | or comment. This requires syntax highlighting. |
Bram Moolenaar | fd2ac76 | 2006-03-01 22:09:21 +0000 | [diff] [blame] | 498 | |
| 499 | A slightly more advanced version is used in the |matchparen| plugin. |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 500 | > |
| 501 | let s:paren_hl_on = 0 |
| 502 | function s:Highlight_Matching_Paren() |
| 503 | if s:paren_hl_on |
| 504 | match none |
| 505 | let s:paren_hl_on = 0 |
| 506 | endif |
| 507 | |
| 508 | let c_lnum = line('.') |
| 509 | let c_col = col('.') |
| 510 | |
| 511 | let c = getline(c_lnum)[c_col - 1] |
| 512 | let plist = split(&matchpairs, ':\|,') |
| 513 | let i = index(plist, c) |
| 514 | if i < 0 |
| 515 | return |
| 516 | endif |
| 517 | if i % 2 == 0 |
| 518 | let s_flags = 'nW' |
| 519 | let c2 = plist[i + 1] |
| 520 | else |
| 521 | let s_flags = 'nbW' |
| 522 | let c2 = c |
| 523 | let c = plist[i - 1] |
| 524 | endif |
| 525 | if c == '[' |
| 526 | let c = '\[' |
| 527 | let c2 = '\]' |
| 528 | endif |
Bram Moolenaar | c51cf03 | 2022-02-26 12:25:45 +0000 | [diff] [blame] | 529 | let s_skip ='synIDattr(synID(line("."), col("."), 0), "name") ' .. |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 530 | \ '=~? "string\\|comment"' |
| 531 | execute 'if' s_skip '| let s_skip = 0 | endif' |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 532 | |
Bram Moolenaar | 7e8fd63 | 2006-02-18 22:14:51 +0000 | [diff] [blame] | 533 | let [m_lnum, m_col] = searchpairpos(c, '', c2, s_flags, s_skip) |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 534 | |
| 535 | if m_lnum > 0 && m_lnum >= line('w0') && m_lnum <= line('w$') |
Bram Moolenaar | c51cf03 | 2022-02-26 12:25:45 +0000 | [diff] [blame] | 536 | exe 'match Search /\(\%' .. c_lnum .. 'l\%' .. c_col .. |
| 537 | \ 'c\)\|\(\%' .. m_lnum .. 'l\%' .. m_col .. 'c\)/' |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 538 | let s:paren_hl_on = 1 |
| 539 | endif |
| 540 | endfunction |
| 541 | |
Bram Moolenaar | f740b29 | 2006-02-16 22:11:02 +0000 | [diff] [blame] | 542 | autocmd CursorMoved,CursorMovedI * call s:Highlight_Matching_Paren() |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 543 | autocmd InsertEnter * match none |
| 544 | < |
| 545 | |
Bram Moolenaar | 7e6a515 | 2021-01-02 16:39:53 +0100 | [diff] [blame] | 546 | ============================================================================== |
| 547 | Opening help in the current window *help-curwin* |
| 548 | |
| 549 | By default, help is displayed in a split window. If you prefer it opens in |
| 550 | the current window, try this custom `:HelpCurwin` command: |
| 551 | > |
| 552 | command -bar -nargs=? -complete=help HelpCurwin execute s:HelpCurwin(<q-args>) |
| 553 | let s:did_open_help = v:false |
Bram Moolenaar | b59ae59 | 2022-11-23 23:46:31 +0000 | [diff] [blame] | 554 | |
Bram Moolenaar | 7e6a515 | 2021-01-02 16:39:53 +0100 | [diff] [blame] | 555 | function s:HelpCurwin(subject) abort |
| 556 | let mods = 'silent noautocmd keepalt' |
| 557 | if !s:did_open_help |
| 558 | execute mods .. ' help' |
| 559 | execute mods .. ' helpclose' |
| 560 | let s:did_open_help = v:true |
| 561 | endif |
| 562 | if !getcompletion(a:subject, 'help')->empty() |
| 563 | execute mods .. ' edit ' .. &helpfile |
Bram Moolenaar | 113cb51 | 2021-11-07 20:27:04 +0000 | [diff] [blame] | 564 | set buftype=help |
Bram Moolenaar | 7e6a515 | 2021-01-02 16:39:53 +0100 | [diff] [blame] | 565 | endif |
| 566 | return 'help ' .. a:subject |
| 567 | endfunction |
| 568 | < |
| 569 | |
| 570 | |
Bram Moolenaar | 91f84f6 | 2018-07-29 15:07:52 +0200 | [diff] [blame] | 571 | vim:tw=78:ts=8:noet:ft=help:norl: |