Table of Contents
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 modes | Description | Explanation |
---|---|---|
Command mode | Execute commands. The text can be revised with commands for copy and paste | |
Insert text mode | Enter or delete letters, punctuation marks | |
i | Enter insert text mode | |
Esc | Leave insert text mode and go back to command mode |
Keybindings in command mode
Command | Description |
---|---|
CTRL-g | Show filename, the number of lines and where you are in the file in percents |
gg | Go to the beginning of the file |
G | Go to the end of the file |
nG | Goto linenumber n. Like in 50G. Goto line number 50 |
k | Move the cursor one line up |
j | Move the cursor one line down |
h | Move the cursor one positon to the left |
l | Move the cursor one positon to the right |
CTRL+h | In insert mode, delete the character before the cursor |
dd | Delete the current line and put it in a buffer |
xdd | Delete x lines including the line the cursor on and put it in a buffer; x >= 1 If x is 1 x can be omitted |
dxw | Delete x words |
xyy | Copy x lines including the line the cursor on and put it in a buffer; x >= 1 If x is 1 x can be omitted |
p | If 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 |
C | Put 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 :edit | Reload the file. edit! Discard changes and reload the file |
:h | Help. Leave help with :q |
:h :x | Show help on command :x. Leave help with :q |
:q | Exit vim |
:q! | Exit vim without saving the changes made to the file |
:w | Write file to disk |
:up | Write file to disk when there are unsaved changes |
:wq or ZZ | Write file to disk and exit vim |
:x | Write file to disk when there are unsaved changes and exit vim |
:f | Show the filename |
:r filename | Read data from file filename |
:w filename | Write to file filename. This is save as |
:w! filename | Overwrite to file filename |
:u | Undo last change. More to undo repeat :u |
:U | Undo all changes on the last modified line |
Ctrl-R | Redo 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 |
v | Select text starting form the cursor position |
V | Select whole lines |
CTRL-V | Select a square block of text |
> | Indent, shift a text block one tab to the right |
:%normal 2x | Remove 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 number | Display linenumbers |
set nonumber | Hide linenumbers |
Some more
Key | Action |
---|---|
a | Enter insertion mode after current character |
b | Back word |
e | End of word |
f | Find character after cursor in current line |
i | Enter insertion mode before current character |
o | Open line below and enter insertion mode |
r | Replace single character at cursor |
s | Substitute single character with new text |
u | Undo |
w | Move forward one word |
x | Delete single character |
y | Yank command |
z | Position current line. CR to top, . to center, - to bottom |
A | Enter insertion mode after end of line |
B | Move back one Word |
C | Change to end of line |
D | Delete to end of line |
E | Move to end of Word |
F | Backwards version of “f” |
H | Home cursor. Goto first line on screen |
I | Enter insertion mode before first non-whitespace character |
J | Join current line with next line |
L | Goto last line on screen |
M | Goto middle line on screen |
O | Open line above and enter insertion mode |
P | Put buffer before cursor |
Q | Leave visual mode (go into “ex” mode) |
R | Replace mode – replaces through end of current line, then inserts |
S | Substitute entire line. Deletes line and enters insertion mode |
W | Forward Word |
X | Delete backwards single character |
0 | Move to column zero. Move to the beginning of the line |
1-9 | Numeric 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 |
Search
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
Command | Explanation |
---|---|
/\%x20\%x65 | [space]e |
e\%x5Ct | e\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
Useful links
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.