Vim


Short hints

Show diff to current buffer

:vertical diffsplit 
opens a file and shows differences to the current opened buffer

Vim together with Cscope

Go into source folder with C files.
Cscope -R -b
Script for vim is
cscope_maps.vim
Open vim with main function as target:
vim -t main
Set cursor on a keyword:
CTRL-\ s
-> searches for the keyword under cursor
CTRL-\ c
-> lists the calls of the underlying function
CTRL-\ f
-> opens the file under the cursor.
CTRL-spacebar *
-> choose instead of the backslash spacebar then the functionality is the same but opening the target file opens it in a new window instead of opening the file in the same buffer. Open this site for more details.

Vim as hex editor

:%!xxd 
to switch editor view to hex editor, and
:%!xxd -r 
to switch back.

Lower/Upper Case Conversion

You can use the "gu" command with a motion such as

   ggguG

(gg = go to the top of the file, gu = make lowercase, G = until the end of the file)

Or you can visually select the block and press "u".

Or if there are no paragraph breaks in the desired text, you can use a text object:

   guip

Splitting the window into several parts is one of my favorite features of vim

 ctrl-w +, ctrl-w - : horicontal change of size
 ctrl-w <, ctrl-w > : vertical change of size
 ctrl-w = : all windows get the same size
 ctrl-w _ : current window is maximized (in horicontal view)
 ctrl-w | : current window is maximized (in vertical view)

Colorscheme for programming

The default colorsceme was not the best as preprocessor statments were shown in purple and this hurts quite much. Better results can be obtained with: :colorscheme murphy

regular expression replacements in vim

For example you want to replace all

   fds[fds_c]=name; fds_c=fds_c+1;

with

   PUSH(name);

then use this:

   s#fds\[fds_c\]=\(.*\); fds_c=fds_c+1;#PUSH(\1);#g

folding

 set foldenable
 set foldmethod=marker
 set foldmarker={,}

custom title text for tabs

 :set tabline=text title for tab

spell checking

 :set spell
 :set spelling=de,en,fr

You can find language packages for a lot of languages at the Vim FTP site.

Ask vim to suggest better ways to spell the word:

 :set spellsuggest=23
defines that the suggestion list is at most 23 entries long.
 z= 
Suggestions
 ]s
Move to next misspelled word after the cursor. A count before the command can be used to repeat. 'wrapscan' applies.
 [s			
Like "]s" but search backwards, find the misspelled word before the cursor. Doesn't recognize words split over two lines, thus may stop at words that are not highlighted as bad. Does not stop at word with missing capital at the start of a line.
 ]S
Like "]s" but only stop at bad words, not at rare words or words for another region.
 [S
Like "]S" but search backwards.

To add words to your own word list:

 zg
Add word under the cursor as a good word to the first name in 'spellfile'. A count may precede the command to indicate the entry in 'spellfile' to be used. A count of two uses the second entry. In Visual mode the selected characters are added as a word (including white space!). When the cursor is on text that is marked as badly spelled then the marked text is used. Otherwise the word under the cursor, separated by non-word characters, is used. If the word is explicitly marked as bad word in another spell file the result is unpredictable.
 zG
Like "zg" but add the word to the internal word list |internal-wordlist|.
 zw
Like "zg" but mark the word as a wrong (bad) word. If the word already appears in 'spellfile' it is turned into a comment line. See |spellfile-cleanup| for getting rid of those.
 zW
Like "zw" but add the word to the internal word list |internal-wordlist|.

bufdo, tabdo, windo

The commands bufdo, windo and tabdo are great for operating on all buffers or windows or tabs. However, the commands finish in a different place from where you started.

ctrl s for search after register content

Mark a word or text part you want to search after. Copy it into register s. Then map the following and you can search the word with ctrl-s.

 map <C-S> let @/=@s<CR>/<CR>

wrapmargin

 :set wrapmargin=20
wraps the input at 20 characters before the right end

Sessions / Views

 :mksession <file>
 :mkview <file>
 :set viewdir=$HOME/.vim/views

use session:

 vim -S Session.vim

or

 :source Session.vim

use view:

 :loadview View.vim

Make view if you leave Buffer:

 autocmd BufWinLeave * mkview
 autocmd BufWinEnter * silent loadview

With sessionoptions you can adjust the settings which are going to store into the session file:

 :set sessionoption=OPTIONS
 :set sessionoption=blank, buffers, curdir, folds, globals, help, localoptions, options, resize, sesdir, slash, tabpages, unix, winpos, winsize
 :set sessionoptions+=winpos

Options which are bold are default values. To show the options use:

 :echo &sessionoptions

Sessions as a Project Manager

 :mksession!

and add in your .vimrc:

 silent source! Session.vim

mark again the previous marked part

Hint was found here.

unicode

 set encoding=utf-8

Then you can use <crtl>+k and afterward [u] [:] to get a on a keyboard without this pad.

content overview:

vim syntax of commands

 * searchs the word under cursor forward
 # same backwards
after the search and leaving the word you cannot continue the search with * or # but with n or N.

Mark a section in visual mode and press "=" to format this section.

The graphical pendant to vim is gvim. Config file is .gvimrc where you can place properties like "set guifont=...".

command mode:

dw deletes a word
dd deletes line
u undo
U undo for actual line
ctrl/R redo
. repetition of the last change
~ converts letters from lower to capital and capital to lower characters
gqap formats paragraph
qa starts recording mode for macro (ends with input of q). To run marco press @a
mb sets mark b. To reach this position again just press `b

ex commands: (these commands can be placed into .vimrc (_vimrc under windows) file in your home directory

":sysntax on" enables syntax highlighting
":map <F3> gqap" defines command gqap on key <F3>
":imap <S-F3> asdf<SPACE>" defines insert operation if you press shift <F3>
":imap <S-F4> asdf<CR>"
":marks" lists all defined marks
":n" next buffer
":prev" previous buffer
":first" jump to first buffer
":buffers" views all buffers
":args" shows file name from command line
"[number]Ctrl/^" jumps to buffer with the given number
"ZZ" saves changes and quit vim
":w" writes changes
":w!" writes even in read only mode
":w >> filename" appends buffer to file filename
":x" quits and saves if there were made changes
":q" quit
":q!" quit even with changes