Nvim :help
pages, generated
from source
using the tree-sitter-vimdoc parser.
CTRL-Z
. This stops
Vim and takes you back to the shell it was started in. You can then do any
other commands until you are bored with them. Then bring back Vim with the
"fg" command.CTRL-Z {any sequence of shell commands} fgYou are right back where you left Vim, nothing has changed. In case pressing
CTRL-Z
doesn't work, you can also use ":suspend".
Don't forget to bring Vim back to the foreground, you would lose any changes
that you made!CTRL-Z
will minimize the Vim window instead.21.2
Executing shell commands:!ls :!dirThe first one is for Unix, the second one for MS-Windows. Vim will execute the program. When it ends you will get a prompt to hit
<Enter>
. This allows you to have a look at the output from the command before
returning to the text you were editing.
The "!" is also used in other places where a program is run. Let's take
a look at an overview:{program}
:r !{program} execute {program}
and read its output
:w !{program} execute {program}
and send text to its input
:[range]!{program} filter text through {program}
CTRL-Z
to suspend Vim. The difference is that a new
shell is started.21.3
Remembering information; ShaDa:set shada=stringThe string specifies what to save. The syntax of this string is an option character followed by an argument. The option/argument pairs are separated by commas. Take a look at how you can build up your own shada string. First, the ' option is used to specify how many files for which you save marks (a-z). Pick a nice even number for this option (1000, for instance). Your command now looks like this:
:set shada='1000The f option controls whether global marks (A-Z and 0-9) are stored. If this option is 0, none are stored. If it is 1 or you do not specify an f option, the marks are stored. You want this feature, so now you have this:
:set shada='1000,f1The < option controls how many lines are saved for each of the registers. By default, all the lines are saved. If 0, nothing is saved. To avoid adding thousands of lines to your ShaDa file (which might never get used and makes starting Vim slower) you use a maximum of 500 lines:
:set shada='1000,f1,<500
'0And you are right back where you left Vim. So you can get on with your work. Vim creates a mark each time you exit Vim. The last one is '0. The position that '0 pointed to is made '1. And '1 is made to '2, and so forth. Mark '9 is lost. The :marks command is useful to find out where '0 to '9 will take you.
:oldfiles
:e #<2Instead of ":e" you can use any command that has a file name argument, the "#<2" item works in the same place as "%" (current file name) and "#" (alternate file name). So you can also split the window to edit the third file:
:split #<3That #<123 thing is a bit complicated when you just want to edit a file. Fortunately there is a simpler way:
:browse oldfiles
<Enter>
(empty cancels):<Enter>
to edit the second file.:wshada! ~/tmp/shadaAnd in the second Vim do:
:rshada! ~/tmp/shadaObviously, the "w" stands for "write" and the "r" for "read". The ! character is used by ":wshada" to forcefully overwrite an existing file. When it is omitted, and the file exists, the information is merged into the file. The ! character used for ":rshada" means that all the information in ShaDa file has priority over existing information, this may overwrite it. Without the ! only information that wasn't set is used. These commands can also be used to store info and use it again later. You could make a directory full of ShaDa files, each containing info for a different purpose.
21.4
Sessions:mksession vimbook.vimLater if you want to restore this session, you can use this command:
:source vimbook.vimIf you want to start Vim and restore a specific session, you can use the following command:
vim -S vimbook.vimThis tells Vim to read a specific file on startup. The 'S' stands for session (actually, you can source any Vim script with -S, thus it might as well stand for "source").
:set sessionoptions+=resizeSESSION HERE, SESSION THERE
:wall :mksession! ~/.config/nvim/secret.vim :source ~/.config/nvim/boring.vimThis first uses ":wall" to write all modified files. Then the current session is saved, using ":mksession!". This overwrites the previous session. The next time you load the secret session you can continue where you were at this point. And finally you load the new "boring" session.
:source ~/.config/nvim/boring.vimThus you have complete control over whether you want to continue next time where you are now, by saving the current setup in a session, or keep the session file as a starting point. Another way of using sessions is to create a window layout that you like to use, and save this in a session. Then you can go back to this layout whenever you want. For example, this is a nice layout to use:
+----------------------------------------+ | VIM - main help file | | | |Move around: Use the cursor keys, or "h| |help.txt================================| |explorer | | |dir |~ | |dir |~ | |file |~ | |file |~ | |file |~ | |file |~ | |~/=========|[No File]===================| | | +----------------------------------------+
:help CTRL-W w :vertical split ~/You can resize the windows a bit to your liking. Then save the session with:
:mksession ~/.config/nvim/mine.vimNow you can start Vim with this layout:
vim -S ~/.config/nvim/mine.vimHint: To open a file you see listed in the explorer window in the empty window, move the cursor to the filename and press "O". Double clicking with the mouse will also do this.
:mksession! ~/.config/nvim/secret.vim :wshada! ~/.local/state/nvim/shada/secret.shadaAnd to restore this again:
:source ~/.config/nvim/secret.vim :rshada! ~/.local/state/nvim/shada/secret.shada
21.5
Views:mkviewVim will decide where to store the view. When you later edit the same file you get the view back with this command:
:loadviewThat's easy, isn't it? Now you want to view the file without the 'number' option on, or with all folds open, you can set the options to make the window look that way. Then store this view with:
:mkview 1Obviously, you can get this back with:
:loadview 1Now you can switch between the two views on the file by using ":loadview" with and without the "1" argument. You can store up to ten views for the same file this way, one unnumbered and nine numbered 1 to 9.
:mkview ~/.config/nvim/main.vimYou can restore it with:
:source ~/.config/nvim/main.vim
21.6
Modelines/* vim:set shiftwidth=4: */ ~Put this line as one of the first or last five lines in the file. When editing the file, you will notice that 'shiftwidth' will have been set to four. When editing another file, it's set back to the default value of eight. For some files the modeline fits well in the header, thus it can be put at the top of the file. For text files and other files where the modeline gets in the way of the normal contents, put it at the end of the file.
:set modelines=10The 'modeline' option can be used to switch this off. Do this when you are working as root on Unix or Administrator on MS-Windows, or when you don't trust the files you are editing:
:set nomodelineUse this format for the modeline:
any-text vim:set {option}={value} ... : any-textThe "any-text" indicates that you can put any text before and after the part that Vim will use. This allows making it look like a comment, like what was done above with "/*" and "*/". The " vim:" part is what makes Vim recognize this line. There must be white space before "vim", or "vim" must be at the start of the line. Thus using something like "gvim:" will not work. The part between the colons is a ":set" command. It works the same way as typing the ":set" command, except that you need to insert a backslash before a colon (otherwise it would be seen as the end of the modeline).
// vim:set textwidth=72 dir=c\:\tmp: use c:\tmp hereThere is an extra backslash before the first colon, so that it's included in the ":set" command. The text after the second colon is ignored, thus a remark can be placed there.