visually selected text and searches for it in C files.
This is a complicated mapping. You can see that mappings can be used to do
quite complicated things. Still, it is just a sequence of commands that are
executed like you typed them.
*vimrc-filetype*
>
filetype plugin indent on
This switches on three very clever mechanisms:
1. Filetype detection.
Whenever you start editing a file, Vim will try to figure out what kind of
file this is. When you edit "main.c", Vim will see the ".c" extension and
recognize this as a "c" filetype. When you edit a file that starts with
"#!/bin/sh", Vim will recognize it as a "sh" filetype.
The filetype detection is used for syntax highlighting and the other two
items below.
See |filetypes|.
2. Using filetype plugin files
Many different filetypes are edited with different options. For example,
when you edit a "c" file, it's very useful to set the 'cindent' option to
automatically indent the lines. These commonly useful option settings are
included with Vim in filetype plugins. You can also add your own, see
|write-filetype-plugin|.
3. Using indent files
When editing programs, the indent of a line can often be computed
automatically. Vim comes with these indent rules for a number of
filetypes. See |:filetype-indent-on| and 'indentexpr'.
*restore-cursor* *last-position-jump* >vim
augroup RestoreCursor
autocmd!
autocmd BufReadPre * autocmd FileType <buffer> ++once
\ let s:line = line("'\"")
\ | if s:line >= 1 && s:line <= line("$") && &filetype !~# 'commit'
\ && index(['xxd', 'gitrebase'], &filetype) == -1
\ && !&diff
\ | execute "normal! g`\""
\ | endif
augroup END
Another autocommand. This time it is used after reading any file. The
complicated stuff after it checks if the '" mark is defined, and jumps to it
if so. It doesn't do that when:
- editing a commit or rebase message, which are likely a different one than
last time,
- using xxd(1) to filter and edit binary files, which transforms input files
back and forth, causing them to have dual nature, so to speak (see also
|using-xxd|) and
- Vim is in diff mode
The backslash at the start of a line is used to continue the command from the
previous line. That avoids a line getting very long. See |line-continuation|.
This only works in a Vim script file, not when typing commands at the
command line.
>
command DiffOrig vert new | set bt=nofile | r ++edit # | 0d_ | diffthis
\ | wincmd p | diffthis
This adds the ":DiffOrig" command. Use this in a modified buffer to see the
differences with the file it was loaded from. See |diff| and |:DiffOrig|.
>
set nolangremap
Prevent that the langmap option applies to characters that result from a
mapping. If set (default), this may break plugins (but it's backward
compatible). See 'langremap'.
==============================================================================
*05.3* Simple mappings
A mapping enables you to bind a set of Vim commands to a single key. Suppose,
for example, that you need to surround certain words with curly braces. In
other words, you need to change a word such as "amount" into "{amount}". With
the :map command, you can tell Vim that the F5 key does this job. The command
is as follows: >
:map <F5> i{<Esc>ea}<Esc>
<
Note:
When entering this command, you must enter <F5> by typing four
characters. Similarly, <Esc> is not entered by pressing the <Esc>
key, but by typing five characters. Watch out for this difference
when reading the manual!
Let's break this down:
<F5> The F5 function key. This is the trigger key that causes the
command to be executed as the key is pressed.
i{<Esc> Insert the { character. The <Esc> key ends Insert mode.
e Move to the end of the word.
a}<Esc> Append the } to the word.
After you execute the ":map" command, all you have to do to put {} around a