-*- mode:org; mode: read-only -*- vim:foldmethod=marker:nomodifiable:tw=2048 This document can be read as "folded document" in Vim or Neovim or in GNU Emacs (org-mode) If this text file is viewed in a browser, encoding should be set to utf-8 Vim: zo opens a fold zc closes The Vim text text editor -- tips and tricks (Volker Kiefel 2023-10-23) * Reopen files at the last cursor position (save cursor position) {{{ Insert a small vim script code into initialization file. The code may be found in the help system with :help restore-cursor or :help last-position-jump }}} * Change end of line codes: DOS/UNIX {{{ :set fileformat=dos :set fileformat=unix }}} * Search and replace with confirmation {{{ :34,45s/old/new/gc }}} * Folds {{{ set foldmethod=manual {Visual}zf creates a fold zd deletes the fold under the cursor zE eliminates all folds in the window zo opens the fold zc closes the fold For use of LaTeX files three curly braces are not optimal, as three curly braces often occur in normal text/code. You may therefore define an alternative (pair of) foldmarker(s). An example for a modeline for a LaTeX file. % vim:foldmethod=marker foldmarker=--->,<--- }}} * Navigate in a text {{{ ** Local variables in C sourcecode jump from use of a variable to its local declaration: gd ** Coordinate movements in two windows in window 1: :set scrollbind in window 2, :set scrollbind This command is further specified: ver Bind vertical scrolling for 'scrollbind' windows hor Bind horizontal scrolling for 'scrollbind' windows jump Applies to the offset between two windows for vertical example: :set scrollopt=ver,hor }}} * Navigate in LaTeX projects with ctags {{{ The following was published by daniel kullmann: https://stackoverflow.com/questions/8119405/create-tags-file-for-latex-for-labels-and-bib-items see `latex-tips.txt' write: --langdef=tex2 --langmap=tex2:.tex --regex-tex2=/\\label[ \t]*\*?\{[ \t]*([^}]*)\}/\1/l,label/ --langdef=bib --langmap=bib:.bib --regex-bib=/^@[A-Za-z]+\{([^,]*)/\1/b,bib/ into $HOME/.ctags ctags *.tex *.bib works now as I want it. Exuberant ctags must be used. Documentation: http://ctags.sourceforge.net/EXTENDING.html }}} * Use diff {{{ Start diff mode from VIM; fist file in actual window, call second file with :diffsplit {filename} Transfer differences for range from the other window to the current window :[range]diffget Transfer differences for range from the current window to the other window :[range]diffput }}} * Using eVim (easy VIM) {{{ gvim -y ** execute one command, return to Insert mode ctrl-o (described in help files i_CTRL-O) ctrl-l (switch to normal mode) }}} * iskeyword {{{ set isk+=58 }}} * Modification of text files by external files {{{ If changes of a text file by an external program are made vim shall read the file again automatically with :set autoread }}} * Processing files with s {{{ :set tabstop=n (Number of spaces that a in the file counts for) to remove tabs: :set expandtab :retab! to replace tabs by approprate counts of spaces: :set noexpandtab :retab! show tab and end of line characters: :set list (see documentation:) :help 'list' }}} * Visual {{{ gv Start Visual mode with the same area as the previous area and the same mode. In Visual mode the current and the previous Visual area are exchanged. g CTRL-G shows the number of characters selected in visual mode (documentation: help g_CTRL-G, :help v_g_CTRL-G) }}} * Menus in console mode (Vim) {{{ Description see console-menus }}} * Line/word wrapping {{{ set wrap wraps lines exactly at end of window set wrap set lbr [linebreak] wraps lines at whitespaces between words, however this only affects display, no s are inserted. {Visual}J Join highlighted lines, remove the indent, insert spaces {Visual}gJ Join the highlighted lines, do not insert or remove spaces }}} * Distance between lines: {{{ set linespace=1 engerer Abstand mit kleineren Ziffern }}} * count words {{{ g Works also in visual mode }}} * Problems with syntax highlighting {{{ e. g. with TeX: force Vim to reanalyse current file from beginning :syntax sync fromstart a pleasant colorscheme for vim :colorscheme manxome }}} * Control maximum size of registers written to viminfo file {{{ some configuration paramaters can be written into the viminfo string, an example: set viminfo='20,s1000,h this means that: - marks are remembered for the last 20 edited files - maximum size of registers saved in Kbyte (1000 Kbyte) - Disables the effect of 'hlsearch' when loading the viminfo file }}} * Vim and the clipboard {{{ set clipboard=unnamed Vim takes all text that should go in the unnamed register and puts it in the clipboard register }}} * Insert text from a register into the ex cmd line {{{ The following command inserts text from the GUI clipboard Ctrl-R + cf :help i_Ctrl-R }}} * set options of gvim GUI {{{ Add scrollbar at the bottom of the screen: guioptions+=b Define GUI font in gvim (Windows, example): set guifont=Liberation_Mono:h10 Define GUI font in gvim (Linux, examples): set guifont=DejaVu\ Sans\ Mono\ 15 set guifont=Liberation\ Mono\ 11 }}} * set language for use of menu translation {{{ if the language for the menus is set to a non-English language put :set langmenu=none before loading menus. }}} * Install German dictionary (Vim v7, win32, Linux) {{{ Get German Dictionary from http://lingucomponent.openoffice.org/spell_dic.html unzip de_DE.zip. Install with :mkspell \vim\vim70\spell\de de_DE Under Linux: Create ~/.vim/spell unzip de_DE.zip in the current directiry start vim and run :mkspell ~/.vim/spell/de de_DE this generates de.latin1.spl. to turn on spelling: :set spell :set spelllang=de ... Experience with Windows 10: Vim Spell-Checker: create German spl-file Uncompress "dict-de_de-igerman98_2011-06-21.oxt" with unzip This creates subdirectory de_DE_igerman98/ with the files "de_DE_igerman98.aff" and "de_DE_igerman98.dic" The file "de.latin1.spl" is created with the command :mkspell c:\vim\vim74\spell\de de_DE_igerman98/de_DE_igerman98 }}} * Problem with automatic installation of German dictionary from the web {{{ Find the server with '/vim/runtime/spell', then enter :let g:spellfile_URL = 'https://ftp.pl.vim.org/pub/vim/runtime/spell' If the server exists, automatic download will begin. Source: https://vi.stackexchange.com/questions/5651/how-can-i-make-vim-download-spell-files A list of mirrors is found at: https://www.vim.org/mirrors.php Then add '/runtime/spell' to the url. So a valid repository for spelling files may be found. (2022-10-23) }}} * Apply an external program or shell command ("filter") to marked text {{{ Mark Text, apply external command with "!" :!fmt -w 77 reformats block of text with 77 columns }}} * External commands: compile LaTeX documents with ltxshell, enter filename at command line {{{ External commands are entered at the command line: :!command In order to enter the filename at the command line enter the contents of register `%': :ctrl-r % The `ltxshell' command can be started with :!ltxshell ctrl-r % Then remove the trailing `.tex'. Explanation: The vim-register `%' contains the name of the current file. Documentation on ctrl-r: :help i_ctrl-r }}} * Print/list lines and line number with specified content {{{ Example: :g/^\\begin{[pr]sli/nu prints all lines and line numbers containing \begin{psli or \begin{rsli }}} * Show editor line numbers {{{ :set number :set nu :set nonumber or even better :set relativenumber :set rnu :set norelativenumber }}} * Force vim on Windows to show encoding with :set fenc {{{ On Windows systems the following line should be entered as one of the last lines to _vimrc: :set enc=utf-8 Probably this forces vim to use utf-8 encoding internally and then :set fenc prints the correct encoding, either latin1 or utf-8 }}} * Edit files with "exotic" encoding {{{ To save a file with cp850 encoding: :set fileencoding=cp850 To open a file with cp850 encoding: :edit ++encoding=cp850 filename :edit ++enc=cp850 filename To open a file with cp850 encoding from the command line vim -c ":edit ++enc=cp850 filename" To get information of encoding of currently opened file: :set fileencoding Help on encoding: :help ++enc :help charset :help encoding-values }}} * Enter characters by number, characters by numeric value {{{ CTRL-V nnn (decimal) CTRL-V Onnn (octal) CTRL-V Xnn (hexadecimal) see :help i_CTRL-V_digit Enter Unicode Characters: CTRL-V u 1F00 (results in small alpha with spritus lenis) }}} * Using the Help system {{{ :helpgrep keyword show the list with items found: :cwindow show the next/previous item found: :cn :cp show the introduction screen/opening screen/about text :intro }}} * Toggle Cursor shape in Vim (Terminal) {{{ togglecursor.vim: https://github.com/jszakmeister/vim-togglecursor/blob/master/plugin/togglecursor.vim https://www.vim.org/scripts/script.php?script_id=4403 }}} * Highlight current line {{{ :set cursorline }}} * Turn off/on highlighting of corresponding parentheses {{{ :NoMatchParen :DoMatchParen }}} * Information on vim (for website) {{{ https://groups.google.com/forum/#!forum/vim_use }}} * Commands for help files {{{ ctags and structure of tags files (tags-file-format) :help tagsrch }}} * Neovim, nvim {{{ nvim "diagnostics" :checkhealth (only nvim, not vim) access to clipboard: install xclip or xsel, details see: :help clipboard (only nvim, not vim) }}} * Build, compile neovim, nvim install in HOME (Linux) {{{ from https://github.com/neovim/neovim/wiki/Building-Neovim and https://github.com/neovim/neovim/wiki/Installing-Neovim#install-from-source For Unix-like systems this installs Neovim to /usr/local, while for Windows to C:\Program Files. Note, however, that this can complicate uninstallation. The following example avoids this by isolating an installation under $HOME/neovim: make CMAKE_EXTRA_FLAGS="-DCMAKE_INSTALL_PREFIX=$HOME/neovim" make install tested on 2023-08-06 }}} * Show whitespace characters in nvim {{{ :set list listchars=tab:>\ ,trail:-,eol:$ or :set list listchars=tab:>\ ,space:·,trail:-,eol:$ (showing also spaces as "·") for documentation in nvim: :help listchars source: https://vi.stackexchange.com/questions/11887/show-newline-characters-in-neovim The middle dot (·) has the decimal numeric value: 183 }}} * Neovim: set indentation depth in C functions {{{ :set cindent shiftwidth=2 This also affects the (undesired) indentation within environments in LaTeX documents. For documentation: :helpgrep :set cindent shiftwidth }}}