various.txt (23770B)
1 *various.txt* Nvim 2 3 4 VIM REFERENCE MANUAL by Bram Moolenaar 5 6 7 Various commands *various* 8 9 Type |gO| to see the table of contents. 10 11 ============================================================================== 12 1. Various commands *various-cmds* 13 14 *CTRL-L* 15 CTRL-L Clears and redraws the screen. The redraw may happen 16 later, after processing typeahead. 17 See also |nvim__redraw()|. 18 *CTRL-L-default* 19 By default, also clears search highlighting 20 |:nohlsearch| and updates diffs |:diffupdate|. 21 |default-mappings| 22 23 *:mod* *:mode* 24 :mod[e] Clears and redraws the screen. 25 See also |nvim__redraw()|. 26 27 *:redr* *:redraw* 28 :redr[aw][!] Redraws pending screen updates now, or the entire 29 screen if "!" is included. To CLEAR the screen use 30 |:mode| or |CTRL-L|. 31 It can be used to redraw the screen in a script 32 or function (or a mapping if 'lazyredraw' is set). 33 See also |nvim__redraw()|. 34 35 *:redraws* *:redrawstatus* 36 :redraws[tatus][!] Redraws the status line and window bar of the current 37 window, or all status lines and window bars if "!" is 38 included. Redraws the commandline instead if it contains 39 the 'ruler'. Useful if 'statusline' or 'winbar' includes 40 an item that doesn't cause automatic updating. 41 See also |nvim__redraw()|. 42 43 *:redrawt* *:redrawtabline* 44 :redrawt[abline] Redraw the tabline. Useful to update the tabline when 45 'tabline' includes an item that doesn't trigger 46 automatic updating. See also |nvim__redraw()|. 47 48 *N<Del>* 49 <Del> When entering a number: Remove the last digit. 50 Note: if you like to use <BS> for this, add this 51 mapping to your vimrc: > 52 :map CTRL-V <BS> CTRL-V <Del> 53 < 54 :as[cii] or *ga* *:as* *:ascii* 55 ga Print the ascii value of the character under the 56 cursor in decimal, hexadecimal and octal. 57 Mnemonic: Get Ascii value. 58 59 For example, when the cursor is on a 'R': 60 <R> 82, Hex 52, Octal 122 ~ 61 When the character is a non-standard ASCII character, 62 but printable according to the 'isprint' option, the 63 non-printable version is also given. 64 65 When the character is larger than 127, the <M-x> form 66 is also printed. For example: 67 <~A> <M-^A> 129, Hex 81, Octal 201 ~ 68 <p> <|~> <M-~> 254, Hex fe, Octal 376 ~ 69 (where <p> is a special character) 70 71 The <Nul> character in a file is stored internally as 72 <NL>, but it will be shown as: 73 <^@> 0, Hex 00, Octal 000 ~ 74 75 If the character has composing characters these are 76 also shown. The value of 'maxcombine' doesn't matter. 77 78 If the character can be inserted as a digraph, also 79 output the two characters that can be used to create 80 the character: 81 <ö> 246, Hex 00f6, Oct 366, Digr o: ~ 82 This shows you can type CTRL-K o : to insert ö. 83 84 *g8* 85 g8 Print the hex values of the bytes used in the 86 character under the cursor, assuming it is in |UTF-8| 87 encoding. This also shows composing characters. The 88 value of 'maxcombine' doesn't matter. 89 Example of a character with two composing characters: 90 e0 b8 81 + e0 b8 b9 + e0 b9 89 ~ 91 92 *8g8* 93 8g8 Find an illegal UTF-8 byte sequence at or after the 94 cursor. 95 Can be used when editing a file that was supposed to 96 be UTF-8 but was read as if it is an 8-bit encoding 97 because it contains illegal bytes. 98 Does not wrap around the end of the file. 99 Note that when the cursor is on an illegal byte or the 100 cursor is halfway through a multibyte character the 101 command won't move the cursor. 102 103 *gx* 104 gx Opens the current filepath or URL (decided by 105 |<cfile>|, 'isfname') at cursor using the system 106 default handler. Mapped to |vim.ui.open()|. 107 108 *v_gx* 109 {Visual}gx Opens the selected text using the system default 110 handler. Mapped to |vim.ui.open()|. 111 112 *:p* *:pr* *:print* *E749* 113 :[range]p[rint] [flags] 114 Print [range] lines (default current line). Can also 115 be spelled `:[range]|` due to Vi compatibility (see 116 |:bar|). Gives an error in an empty buffer. 117 In the GUI you can use the File.Print menu entry. 118 See |ex-flags| for [flags]. 119 The |:filter| command can be used to only show lines 120 matching a pattern. 121 122 :[range]p[rint] {count} [flags] 123 Print {count} lines, starting with [range] (default 124 current line |cmdline-ranges|). 125 See |ex-flags| for [flags]. 126 127 *:l* *:list* 128 :[range]l[ist] [count] [flags] 129 Same as :print, but show tabs as ">", trailing spaces 130 as "-", and non-breakable space characters as "+" by 131 default. Further changed by the 'listchars' option. 132 See |ex-flags| for [flags]. 133 134 *:nu* *:number* 135 :[range]nu[mber] [count] [flags] 136 Same as :print, but precede each line with its line 137 number. (See also |hl-LineNr| and 'numberwidth'). 138 See |ex-flags| for [flags]. 139 140 *:#* 141 :[range]# [count] [flags] 142 synonym for :number. 143 144 *:#!* *vim-shebang* 145 :#!{anything} Ignored, so that you can start a Vim script with: > 146 #!vim -S 147 let mylogbook='$HOME/logbook.md' 148 exe $':e {mylogbook}' 149 $ 150 put ='## ' .. strftime('%d. %b %Y') 151 norm! o 152 < 153 Make that script executable and run it to create a 154 new diary entry. 155 156 *:z* *E144* 157 :[range]z[+-^.=][count] Display several lines of text surrounding the line 158 specified with [range], or around the current line 159 if there is no [range]. 160 161 If there is a [count], that's how many lines you'll 162 see; if there is no [count] and only one window then 163 twice the value of the 'scroll' option is used, 164 otherwise the current window height minus 3 is used. 165 This is the value of "scr" in the table below. 166 167 If there is a [count] the 'window' option is set to 168 its value. 169 170 :z can be used either alone or followed by any of 171 several marks. These have the following effect: 172 173 mark first line last line new cursor line ~ 174 ---- ---------- --------- ------------ 175 + current line 1 scr forward 1 scr forward 176 - 1 scr back current line current line 177 ^ 2 scr back 1 scr back 1 scr back 178 . 1/2 scr back 1/2 scr fwd 1/2 scr fwd 179 = 1/2 scr back 1/2 scr fwd current line 180 181 Specifying no mark at all is the same as "+". 182 If the mark is "=", a line of dashes is printed 183 around the current line. 184 185 *:z!* 186 :[range]z![+-^.=][count] 187 Like ":z", but when [count] is not specified, it 188 defaults to the Vim window height minus one. 189 190 :[range]z[!]#[+-^.=][count] *:z#* 191 Like ":z" or ":z!", but number the lines. 192 193 *:=* 194 := [args] Without [args]: prints the last line number. 195 With [args]: equivalent to `:lua ={expr}`. see |:lua| 196 197 :{range}= Prints the last line number in {range}. For example, 198 this prints the current line number: > 199 :.= 200 201 :norm[al][!] {commands} *:norm* *:normal* 202 Execute Normal mode commands {commands}. This makes 203 it possible to execute Normal mode commands typed on 204 the command-line. {commands} are executed like they 205 are typed. For undo all commands are undone together. 206 Execution stops when an error is encountered. 207 208 If the [!] is given, mappings will not be used. 209 Without it, when this command is called from a 210 non-remappable mapping (|:noremap|), the argument can 211 be mapped anyway. 212 213 {commands} should be a complete command. If 214 {commands} does not finish a command, the last one 215 will be aborted as if <Esc> or <C-C> was typed. 216 This implies that an insert command must be completed 217 (to start Insert mode, see |:startinsert|). A ":" 218 command must be completed as well. And you can't use 219 "gQ" to start Ex mode. 220 221 The display is not updated while ":normal" is busy. 222 223 {commands} cannot start with a space. Put a count of 224 1 (one) before it, "1 " is one space. 225 226 This command cannot be followed by another command, 227 since any '|' is considered part of the command. 228 229 This command can be used recursively, but the depth is 230 limited by 'maxmapdepth'. 231 232 An alternative is to use |:execute|, which uses an 233 expression as argument. This allows the use of 234 printable characters to represent special characters. 235 236 Example: > 237 :exe "normal \<c-w>\<c-w>" 238 < 239 240 :{range}norm[al][!] {commands} *:normal-range* 241 Execute Normal mode commands {commands} for each line 242 in the {range}. Before executing the {commands}, the 243 cursor is positioned in the first column of the range, 244 for each line. Otherwise it's the same as the 245 ":normal" command without a range. 246 247 *:sh* *:shell* *E371* *E360* 248 :sh[ell] Removed. |vim-differences| 249 250 *:terminal* *:te* 251 :te[rminal][!] [cmd] Run [cmd] in a non-interactive 'shell' in a new 252 |terminal-emulator| buffer. Without [cmd], start an 253 interactive 'shell'. 254 255 By default the current window is used. To open in 256 a split window, use |:horizontal| or |:vertical|: > 257 :hor te 258 :vert te 259 < 260 Type |i| to enter |Terminal-mode|, then keys are sent to 261 the job running in the terminal. Type <C-\><C-N> to 262 leave Terminal-mode. |CTRL-\_CTRL-N|. Type <C-\><C-O> 263 to execute a single normal mode command |t_CTRL-\_CTRL-O| 264 265 Fails if changes have been made to the current buffer, 266 unless 'hidden' is set. 267 268 If [cmd] is omitted, and the 'shell' job exits with no 269 error, the buffer is closed automatically 270 |default-autocmds|. 271 272 To enter |Terminal-mode| automatically: > 273 autocmd TermOpen * startinsert 274 < 275 *:!cmd* *:!* 276 :!{cmd} Execute {cmd} with 'shell'. See also |:terminal|. 277 For the filter command, see |:range!|. 278 279 The command runs in a non-interactive shell connected 280 to a pipe (not a terminal). Use |:terminal| to run an 281 interactive shell connected to a terminal. 282 283 Backgrounded ("&") commands must not write to stdout 284 or stderr, the streams are closed immediately. |E5677| 285 Use |jobstart()| instead. > 286 :call jobstart('foo', {'detach':1}) 287 < 288 For powershell, chaining a stringed executable path 289 requires using the call operator (&). > 290 :!Write-Output "1`n2" | & "C:\Windows\System32\sort.exe" /r 291 < 292 Vim builds command line using options 'shell', 'shcf', 293 'sxq' and 'shq' in the following order: 294 `&sh &shcf &sxq &shq {cmd} &shq &sxq` 295 So setting both 'sxq' and 'shq' is possible but rarely 296 useful. Additional escaping inside `{cmd}` may also 297 be due to 'sxe' option. 298 299 Also, all |cmdline-special| characters in {cmd} are 300 replaced by Vim before passing them to shell. 301 302 *E34* 303 Any "!" in {cmd} is replaced with the previous 304 external command (see also 'cpoptions'), unless 305 escaped by a backslash. Example: ":!ls" followed by 306 ":!echo ! \! \\!" executes "echo ls ! \!". 307 308 Any "|" in {cmd} is passed to the shell, you cannot 309 use it to append a Vim command. See |:bar|. 310 311 Any "%" in {cmd} is expanded to the current file name. 312 Any "#" in {cmd} is expanded to the alternate file name. 313 Special characters are not escaped, use quotes or 314 |shellescape()|: > 315 :!ls "%" 316 :exe "!ls " .. shellescape(expand("%")) 317 < 318 Newline character ends {cmd} unless a backslash 319 precedes the newline. What follows is interpreted as 320 another |:| command. 321 322 After the command has been executed, the timestamp and 323 size of the current file is checked |timestamp|. 324 325 If the command produces too much output some lines may 326 be skipped so the command can execute quickly. No 327 data is lost, this only affects the display. The last 328 few lines are always displayed (never skipped). 329 330 To avoid the hit-enter prompt use: > 331 :silent !{cmd} 332 < 333 *:!!* 334 :!! Repeat last ":!{cmd}". 335 336 *:ve* *:ver* *:version* 337 :ve[rsion] Print editor version and build information. 338 See also |feature-compile|. 339 340 *:redi* *:redir* 341 :redi[r][!] > {file} Redirect messages to file {file}. The messages which 342 are the output of commands are written to that file, 343 until redirection ends. The messages are also still 344 shown on the screen. When [!] is included, an 345 existing file is overwritten. When [!] is omitted, 346 and {file} exists, this command fails. 347 348 Only one ":redir" can be active at a time. Calls to 349 ":redir" will close any active redirection before 350 starting redirection to the new target. For recursive 351 use check out |execute()|. 352 353 To stop the messages and commands from being echoed to 354 the screen, put the commands in a function and call it 355 with ":silent call Function()". 356 Alternatives are the 'verbosefile' option or 357 |execute()| function, these can be used in combination 358 with ":redir". 359 360 :redi[r] >> {file} Redirect messages to file {file}. Append if {file} 361 already exists. 362 363 :redi[r] @{a-zA-Z} 364 :redi[r] @{a-zA-Z}> Redirect messages to register {a-z}. Append to the 365 contents of the register if its name is given 366 uppercase {A-Z}. The ">" after the register name is 367 optional. 368 :redi[r] @{a-z}>> Append messages to register {a-z}. 369 370 :redi[r] @*> 371 :redi[r] @+> Redirect messages to the selection or clipboard. For 372 backward compatibility, the ">" after the register 373 name can be omitted. See |quotestar| and |quoteplus|. 374 :redi[r] @*>> 375 :redi[r] @+>> Append messages to the selection or clipboard. 376 377 :redi[r] @"> Redirect messages to the unnamed register. For 378 backward compatibility, the ">" after the register 379 name can be omitted. 380 :redi[r] @">> Append messages to the unnamed register. 381 382 :redi[r] => {var} Redirect messages to a variable. If the variable 383 doesn't exist, then it is created. If the variable 384 exists, then it is initialized to an empty string. 385 The variable will remain empty until redirection ends. 386 Only string variables can be used. After the 387 redirection starts, if the variable is removed or 388 locked or the variable type is changed, then further 389 command output messages will cause errors. When using 390 a local variable (l:var in a function or s:var in a 391 script) and another `:redir` causes the current one to 392 end, the scope might be different and the assignment 393 fails. 394 To get the output of one command the |execute()| 395 function can be used instead of redirection. 396 397 :redi[r] =>> {var} Append messages to an existing variable. Only string 398 variables can be used. 399 400 :redi[r] END End redirecting messages. 401 402 *:filt* *:filter* 403 :filt[er][!] {pattern} {command} 404 :filt[er][!] /{pattern}/ {command} 405 Restrict the output of {command} to lines matching 406 with {pattern}. For example, to list only xml files: > 407 :filter /\.xml$/ oldfiles 408 < If the [!] is given, restrict the output of {command} 409 to lines that do NOT match {pattern}. 410 411 {pattern} is a Vim search pattern. Instead of 412 enclosing it in / any non-ID character (see 'isident') 413 can be used, so long as it does not appear in 414 {pattern}. 415 Without the enclosing character the pattern cannot 416 include the bar character. 'ignorecase' is not used. 417 418 The pattern is matched against the relevant part of 419 the output, not necessarily the whole line. Only some 420 commands support filtering, try it out to check if it 421 works. Some of the commands that support filtering: 422 |:#| - filter whole line 423 |:clist| - filter by file name or module name 424 |:command| - filter by command name 425 |:files| - filter by file name 426 |:highlight| - filter by highlight group 427 |:history| - filter by history commands 428 |:jumps| - filter by file name 429 |:let| - filter by variable name 430 |:list| - filter whole line 431 |:llist| - filter by file name or module name 432 |:marks| - filter by text in the current file, 433 or file name for other files 434 |:oldfiles| - filter by file name 435 |:registers| - filter by register contents 436 (does not work multi-line) 437 |:set| - filter by option name 438 439 Only normal messages are filtered, error messages are 440 not. 441 442 *:sil* *:silent* *:silent!* 443 :sil[ent][!] {command} Execute {command} silently. Normal messages will not 444 be given or added to the message history. 445 When [!] is added, error messages will also be 446 skipped, and commands and mappings will not be aborted 447 when an error is detected. |v:errmsg| is still set. 448 When [!] is not used, an error message will cause 449 further messages to be displayed normally. 450 Redirection, started with |:redir|, will continue as 451 usual, although there might be small differences. 452 This will allow redirecting the output of a command 453 without seeing it on the screen. Example: > 454 :redir >/tmp/foobar 455 :silent g/Aap/p 456 :redir END 457 < To execute a Normal mode command silently, use the 458 |:normal| command. For example, to search for a 459 string without messages: > 460 :silent exe "normal /path\<CR>" 461 < ":silent!" is useful to execute a command that may 462 fail, but the failure is to be ignored. Example: > 463 :let v:errmsg = "" 464 :silent! /^begin 465 :if v:errmsg != "" 466 : ... pattern was not found 467 < ":silent" also skips the hit-enter prompt. 468 Dialogs that prompt for user input (|confirm()|, 469 'swapfile', …) are never silent. 470 471 *:uns* *:unsilent* 472 :uns[ilent] {command} Execute {command} not silently. Only makes a 473 difference when |:silent| was used to get to this 474 command. 475 Use this for giving a message even when |:silent| was 476 used. In this example |:silent| is used to avoid the 477 message about reading the file and |:unsilent| to be 478 able to list the first line of each file. > 479 :silent argdo unsilent echo expand('%') .. ": " .. getline(1) 480 < 481 482 *:verb* *:verbose* 483 :[count]verb[ose] {command} 484 Execute {command} with 'verbose' set to [count]. If 485 [count] is omitted one is used. ":0verbose" can be 486 used to set 'verbose' to zero. 487 The additional use of ":silent" makes messages 488 generated but not displayed. 489 The combination of ":silent" and ":verbose" can be 490 used to generate messages and check them with 491 |v:statusmsg| and friends. For example: > 492 :let v:statusmsg = "" 493 :silent verbose runtime foobar.vim 494 :if v:statusmsg != "" 495 : " foobar.vim could not be found 496 :endif 497 < When concatenating another command, the ":verbose" 498 only applies to the first one: > 499 :4verbose set verbose | set verbose 500 < verbose=4 ~ 501 verbose=0 ~ 502 For logging verbose messages in a file use the 503 'verbosefile' option. 504 505 *:verbose-cmd* 506 When 'verbose' is non-zero, listing the value of a Vim option or a key map or 507 an abbreviation or a user-defined function or a command or a highlight group 508 or an autocommand will also display where it was last defined. If they were 509 defined in Lua they will only be located if 'verbose' is set. So Start 510 nvim with -V1 arg to see them. If it was defined manually then there 511 will be no "Last set" message. When it was defined while executing a function, 512 user command or autocommand, the script in which it was defined is reported. 513 514 *K* 515 [count]K Runs the program given by 'keywordprg' to lookup the 516 |word| (defined by 'iskeyword') under or right of the 517 cursor. Default is "man". Works like this: > 518 :tabnew | terminal {program} {keyword} 519 < Special cases: 520 - If 'keywordprg' begins with ":" it is invoked as 521 a Vim command with [count]. 522 - If 'keywordprg' is empty, |:help| is used. 523 - When 'keywordprg' is equal to "man", a [count] 524 before "K" is inserted after the "man" command and 525 before the keyword. For example, using "2K" while 526 the cursor is on "mkdir", results in: > 527 !man 2 mkdir 528 < - When 'keywordprg' is equal to "man -s", a [count] 529 before "K" is inserted after the "-s". If there is 530 no count, the "-s" is removed. 531 *K-lsp-default* 532 - The Nvim |LSP| client sets K to show LSP "hover" 533 feature. |lsp-defaults| 534 535 *v_K* 536 {Visual}K Like "K", but use the visually highlighted text for 537 the keyword. Only works when the highlighted text is 538 not more than one line. 539 540 *gO* 541 gO Show a filetype-specific, navigable "outline" of the 542 current buffer. For example, in a |help| buffer this 543 shows the table of contents. 544 545 Works in |help| and |:Man| buffers, or any buffer with 546 an active |LSP| client (|lsp-defaults|). 547 548 [N]gs *gs* *:sl* *:sleep* 549 :[N]sl[eep] [N][m] Do nothing for [N] seconds, or [N] milliseconds if [m] 550 was given. "gs" always uses seconds. 551 Default is one second. > 552 :sleep "sleep for one second 553 :5sleep "sleep for five seconds 554 :sleep 100m "sleep for 100 milliseconds 555 10gs "sleep for ten seconds 556 < Can be interrupted with CTRL-C. 557 "gs" stands for "goto sleep". 558 While sleeping the cursor is positioned in the text, 559 if at a visible position. 560 Queued messages are processed during the sleep. 561 562 *:sl!* *:sleep!* 563 :[N]sl[eep]! [N][m] Same as above, but hide the cursor. 564 565 *g==* 566 g== Executes the current code block. 567 568 Works in |help| buffers. 569 570 ============================================================================== 571 2. Using Vim like less or more *less* 572 573 If you use the less or more program to view a file, you don't get syntax 574 highlighting. Thus you would like to use Vim instead. You can do this by 575 using the shell script "$VIMRUNTIME/scripts/less.sh". 576 577 This shell script uses the Vim script "$VIMRUNTIME/scripts/less.vim". It sets 578 up mappings to simulate the commands that less supports. Otherwise, you can 579 still use the Vim commands. 580 581 This isn't perfect. For example, when viewing a short file Vim will still use 582 the whole screen. But it works well enough for most uses, and you get syntax 583 highlighting. 584 585 The "h" key will give you a short overview of the available commands. 586 587 If you want to set options differently when using less, define the 588 LessInitFunc in your vimrc, for example: > 589 590 func LessInitFunc() 591 set nocursorcolumn nocursorline 592 endfunc 593 < 594 ============================================================================== 595 3. Commenting *commenting* 596 597 Nvim supports commenting and uncommenting of lines based on 'commentstring'. 598 599 Acting on a single line behaves as follows: 600 - If the line matches 'commentstring', the comment markers are removed (e.g. 601 `/*foo*/` is transformed to `foo`). 602 - Otherwise the comment markers are added to the current line (e.g. `foo` is 603 transformed to `/*foo*/`). Blank lines are ignored. 604 605 Acting on multiple lines behaves as follows: 606 - If each affected non-blank line matches 'commentstring', then all comment 607 markers are removed. 608 - Otherwise all affected lines are converted to comments; blank lines are 609 transformed to empty comments (e.g. `/**/`). Comment markers are aligned to 610 the least indented line. 611 612 Matching 'commentstring' does not account for whitespace in comment markers. 613 Removing comment markers is first attempted exactly, with fallback to using 614 markers trimmed from whitespace. 615 616 If the filetype of the buffer is associated with a language for which a 617 |treesitter| parser is installed, then |vim.filetype.get_option()| is called 618 to look up the value of 'commentstring' corresponding to the cursor position. 619 (This can be different from the buffer's 'commentstring' in case of 620 |treesitter-language-injections|.) 621 622 The following |default-mappings| are defined: 623 624 *gc* *gc-default* 625 gc{motion} Comment or uncomment lines covered by {motion}. 626 627 *gcc* *gcc-default* 628 gcc Comment or uncomment [count] lines starting at cursor. 629 630 *v_gc* *v_gc-default* 631 {Visual}gc Comment or uncomment the selected line(s). 632 633 *o_gc* *o_gc-default* 634 gc Text object for the largest contiguous block of 635 non-blank commented lines around the cursor (e.g. 636 `gcgc` uncomments a comment block; `dgc` deletes it). 637 Works only in Operator-pending mode. 638 639 vim:noet:tw=78:ts=8:ft=help:norl: