-*- mode:org; mode: read-only -*- vim:foldmethod=marker:foldmarker={{{,}}}: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 2024-08-04) * 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 zR opens all folds zM closes all folds 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 }}} * 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 }}} * 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 " only in gvim }}} * count words {{{ g Works also in visual mode: select area to be analyzed }}} * 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 }}} * 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 }}} * Start Neovim/Vim with latin1 default encoding for saving text files {{{ file nvim-l1.bat (Windows): @echo off c:\nvim-win64\bin\nvim.exe -c "so c:\util\l1-vim.vim" %1 %2 %3 %4 %5 %6 %7 %8 %9 file nvim-l1.sh (Linux): #!/bin/sh nvim -c "so /home/username/vim_scr/l1-vim.vim" "$@" file l1-vim.vim: au BufWritePre * :set fenc=latin1 }}} * 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) }}} * :substitute command -- insert a carriage return, split lines {{{ In a :substitute command, enter "\r" as the replace-with string Example: :%s/,\ /\r/g This command replaces all occurrences of ", " by a carriage return, more details are found with :help s }}} * :substitute command -- remove empty lines in a text file {{{ An empty line between two longer lines can be found with two end-of-line: "\n\n". In order to remove all empty lines (between longer lines each) in a file with the substitute command, enter: :%s/\n\n/\r/g In order to remove all empty lines in a text file, including series of empty lines, enter :%/[\n]\+/\r/g }}} * 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 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 {{{ :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 }}}