User Tools

Site Tools


vim

If you want to send us your comments, please do so. Thanks
More on comments


vim

Alternatives

levee : Extremely small vi based editor

Start vim

vi

Alternative way if vim is the default editor as set with update-alternatives

^x ^e


Main modesDescriptionExplanation
Command modeExecute commands. The text can be revised with commands for copy and paste
Insert text modeEnter or delete letters, punctuation marks
iEnter insert text mode
EscLeave insert text mode and go back to command mode

Keybindings in command mode

CommandDescription
CTRL-gShow filename, the number of lines and where you are in the file in percents
ggGo to the beginning of the file
GGo to the end of the file
nGGoto linenumber n. Like in 50G. Goto line number 50
kMove the cursor one line up
jMove the cursor one line down
hMove the cursor one positon to the left
lMove the cursor one positon to the right
CTRL+hIn insert mode, delete the character before the cursor
ddDelete the current line and put it in a buffer
xddDelete x lines including the line the cursor on and put it in a buffer; x >= 1 If x is 1 x can be omitted
dxwDelete x words
xyyCopy x lines including the line the cursor on and put it in a buffer; x >= 1 If x is 1 x can be omitted
pIf lines are cut or yanked: Paste the contents of the buffer to the line under the line the cursor is on. Otherwise Paste on the cursor position
CPut the contents of the line from the cursor position to the end in a buffer. Clear the line from the cursor position to the end. Go to insert mode
:e :editReload the file. edit! Discard changes and reload the file
:hHelp. Leave help with :q
:h :xShow help on command :x. Leave help with :q
:qExit vim
:q!Exit vim without saving the changes made to the file
:wWrite file to disk
:upWrite file to disk when there are unsaved changes
:wq or ZZWrite file to disk and exit vim
:xWrite file to disk when there are unsaved changes and exit vim
:fShow the filename
:r filenameRead data from file filename
:w filenameWrite to file filename. This is save as
:w! filenameOverwrite to file filename
:uUndo last change. More to undo repeat :u
:UUndo all changes on the last modified line
Ctrl-RRedo what has been undone
.Repeat previous change at the current cursor position
»Indent, shift the line to the right one tab. Set the tab with with set tabstop=2 in .vimrc. See below at “tab with”
«Unindent, shitft the line to the left on tab
vSelect text starting form the cursor position
VSelect whole lines
CTRL-VSelect a square block of text
>Indent, shift a text block one tab to the right
:%normal 2xRemove the first 2 characters of every line
:s/|/ / 
Insert a white space at the beginning the current line
:%s/|/ / 
Globally insert a white space at the beginning of each line
:%s!|!qq!
Insert qq at the beginning of each line
:%s!$!qq!
Insert qq at the end of each line
:%s/192.168.1./192.168.2./g 
Replace 192.168.1. with 192.168.2. in the whole file
:%s/\"\.\//\"\/mnt\/directory1\/directory2\//g
Replace “./ with ”/mnt/directory1/directory2/ globaly
set numberDisplay linenumbers
set nonumberHide linenumbers

Some more

KeyAction
aEnter insertion mode after current character
bBack word
eEnd of word
fFind character after cursor in current line
iEnter insertion mode before current character
oOpen line below and enter insertion mode
rReplace single character at cursor
sSubstitute single character with new text
uUndo
wMove forward one word
xDelete single character
yYank command
zPosition current line. CR to top, . to center, - to bottom
AEnter insertion mode after end of line
BMove back one Word
CChange to end of line
DDelete to end of line
EMove to end of Word
FBackwards version of “f”
HHome cursor. Goto first line on screen
IEnter insertion mode before first non-whitespace character
JJoin current line with next line
LGoto last line on screen
MGoto middle line on screen
OOpen line above and enter insertion mode
PPut buffer before cursor
QLeave visual mode (go into “ex” mode)
RReplace mode – replaces through end of current line, then inserts
SSubstitute entire line. Deletes line and enters insertion mode
WForward Word
XDelete backwards single character
0Move to column zero. Move to the beginning of the line
1-9Numeric precursor to other commands like 20G (goto line 20)
(SPACE) Move right one character
$Move to end of line
%Match nearest [],(),{}
^Move to first non-whitespace character of line
(Move to previous sentence
)Move to next sentence
|Move to column zero
Move to first non-whitespace of previous line
_Move to nth non-whitespace character of line. 6^ (move to 6th non-whitespace character)
+Move to first non-whitespace of next line
[Move to previous “{…}” section
]Move to next “{…}” section
{Move to previous blank-line separated section
}Move to next blank-line separated section
`Move to marked line, memorized column
:Ex-submode
Access numbered buffer. Load or access lettered buffer
~Reverse case of current character and move cursor forward
.Repeat last text-changing command
<Unindent command
>Indent command

To highlight all search results

:set hlsearch

Unset permanently

:set nohlsearch

Unset temporarily

:nohlsearch

Use / to search
Use hexadecimal from the ascii -x table
[A-Z]: all capital letters A uptil Z

CommandExplanation
/\%x20\%x65[space]e
e\%x5Cte\t
/\%x7D\%x5C033}\033
/echo\%x20[A-Z]echo [A-Z]

Using tabed pages

Vim allows to have multiple files open in tabs. See Using tab pages and vim tips using tabs

Configuration

Settings to make working with vim more comfortable
These changes and additions are made to vimrc

Normal locations of vimrc

The vimrc file can be located in

/home/user/.vimrc
/home/user/.vim/vimrc

Watch the dots !

Show where the .vimrc file is

:echo $MYVIMRC

Reload the /home/user.vimrc file

:source $MYVIMRC

Auto source .vimrc

Add to /home/user/.vimrc

au! BufWritePost .vimrc source % 

or

au! BufWritePost $MYVIMRC source $MYVIMRC

if MYVIMRC is set

Syntax highlighting

Color schemes can be found in /usr/share/vim/vim81/colors/
We like the color schemes: elflord, moloakai (needs to be installed), slate

The simple way

Set the terminal to 256 colors. See bashrc settings
Show the current colorscheme: :colorscheme
Add to /home/user/.vimrc color [colorscheme]
Example

color elflord

vim help

:help xterm-color

Add to /home/user/.vimrc

syntax on

molokai

Download the molokai syntax highlighting profile from molokai. On the github page click on

molokai.vim
RAW button

Save the page molokai.vim in

/home/user/.vim/colors

Edit

/home/user/.vim/colors/molokai.vim

and change 59 in 159 in

hi Comment         ctermfg=159

Save the file and add to .vimrc

set t_Co=256
syntax on
colorscheme molokai
"let g:molokai_original = 1
let g:rehash256 = 1

If you get E185: Cannot find color scheme remove the full path to the colorscheme. Just the name will do

tmux

To have syntax highlighting work fine with tmux do

  • cp /usr/share/vim/vim80/colors/murphy.vim /usr/share/vim/vim80/colors/tmux.vim
  • Edit /usr/share/vim/vim80/colors/tmux.vim
    • Add: “ Color scheme for tmux (including the double quotes = comment token)
    • Change let g:colors_name = “tmux” (murphy changed to tmux)
    • Change colors to (or your own preferences)
      • hi Comment term=NONE ctermbg=Blue ctermfg=Green guibg=White guifg=Blue
      • hi Special term=bold ctermfg=Yellow guifg=magenta
      • hi Statement term=bold ctermfg=Cyan guifg=#ffff00 gui=NONE
  • Add to ~/.vimrc
    • syntax on
    • set background=dark
  • Add to ~/.tmux.conf
    • # So it works fine with vim (this is a comment)
    • set -g default-terminal “screen-256color”
  • Test if it works

line numbering

set number
highlight LineNr ctermfg=214
highlight LineNr ctermfg=yellow

Status line

:set laststatus=2

0: never, 1: only if there are at least two windows, 2: always
vimrc: set laststatus=2

  :echo expand('%:p') - Temporarily
  :f -  Temporarily
  :set statusline+=%F
  :set statusline =%4*\ %<%F%*

Filename with full path but no position information
vimrc: set statusline+=%F

tabs

Open a new tab

:tabnew

Always show the tab line

:set showtabline=2

Switch between tabs in normal mode

gt - forward
gT - backward
nnngt - to tab number nnn

Other tab commands

:tabs - list all tabs
:tabclose or q - close current tab
:tabclose {n} - close tab n
:tabonly - close all other tabs

vimrc

nnoremap <C-Left> :tabprevious<CR>
nnoremap <C-Right> :tabnext<CR>

Tab with

set tabstop=2 softtabstop=2 shiftwidth=2 noexpandtab

Line wrapping

set nowrap

History

set history=200

Save file

Save file to disk when there are unsaved changed when leaving INSERT mode map <Esc><Esc> :up<CR>

Encrypted files

Vim can work with encrypted files. Do

vi -x file.txt

And vi will ask for a password twice. After entering the password you can start editing text. Then save the file with :x

Plugins

A lot of plugins can be found on Github. So git must be installed
Instructions on how to install the plugins can be found on the Github pages of the plugins

Install the vim pluginmanager
Comment out with ( ” ) the example Plugin lines in /home/user/.vimrc

Status bar on the bottom of the page
Comments ( “ ) are not allowed in the

let g:lightline = {
    \ 'colorscheme': 'wombat',
    \ }

statement

Syntax checking when the file is saved and when the file is opened
Some commands

:SyntasticInfo
:help syntastic-checker

Errors

".bash_aliases.sh" 41L, 1140C
Error detected while processing BufRead Autocommands for "*.sh":
E117: Unknown function: dist#ft#SetFileTypeSH
Error detected while processing BufRead Autocommands for ".bash[_-]aliases*":
E117: Unknown function: dist#ft#SetFileTypeSH

Check .vimrc for errors

To be solved

Set the duration of the errormessage

Autosave when leaving INSERT mode

Add to /home/user/.vimrc

"Save file to disk when there are unsaved changed when leaving INSERT mode
map <Esc><Esc> :up<CR>

Vim on Windows

Install gvim81.exe or the newer version when it is available

vim documentation overview
vim documentation
vim user manual, 2002
Vim Tips Wiki
vim cookbook
Advanced Vi Cheat Sheet
VI(Visual) Editor Reference manual
Seven habits of effective text editing, 2000
Daily Vim
Vim Wiki on Things and stuff

Using undo branches
Vim modes like normal, insert, visual
Vim-key-bindings
Vim Cheat Sheet key bindings

Make search results appear in the middle of the screen


Main subjects on this wiki: Linux, Debian, HTML, Microcontrollers, Privacy

RSS
Disclaimer
Privacy statement
Bugs statement
Cookies
Copyright © : 2014 - 2024 Webevaluation.nl and the authors
Changes reserved.

This website uses cookies. By using the website, you agree with storing cookies on your computer. Also you acknowledge that you have read and understand our Privacy Policy. If you do not agree leave the website.More information about cookies
vim.txt · Last modified: 02-02-2024 00:28 by wim