Vim text editor
Vim (vi improved) is a terminal text editor that is often used along nano mostly when there is no graphical interface π.
# create or open a file
$ vim filename
π Some are using an unofficial "modern vim": neovim.
Get started
The current one is shown at the bottom left of the screen.
-
<nothing>
: the command/normal mode -
INSERTION
: the insert/edit mode -
VISUAL
: [...]
To exit VIM, you need to be in command mode. Press ESC (at least once) to go back to it. Then, press :
followed by the exit without saving command: q!
. π See the command section.
π¦ Not panicking is the key to success. Being trapped in VIM is a popular gag/meme, see these trolls: how-to-exit-vim and VimKiller.
Configuration
You can configure your editor by creating a file ~/.virmrc
. You can find useful configurations online. Look for dotfiles vim
.
You can also add plugins to Vim such as ale for IDE-like features.
Add ALE to VIM
mkdir -p ~/.vim/pack/git-plugins/start
git clone --depth 1 https://github.com/dense-analysis/ale.git ~/.vim/pack/git-plugins/start/ale
- viniciusgerevini dotfile
-
sudo apt-get install vim-runtime
-
sudo apt-get install vim-gui-common
Using CTRL+P, you now have autocompletion.
A basic configuration (syntax highlighting, cursor, tab size, and file encoding)
set fileencodings=UTF-8,utf-8,default,latin1 "file encoding
syntax enable "enable syntax
highlight Cursor guifg=white guibg=black "cursor color
filetype plugin indent on "indentations
set cursorline "show cursor
set tabstop=4 "tabulation size
Basics
Start editing
To switch to the INSERTION mode in which you can edit the file:
-
i
/a
: insert before/after the cursor -
I
/A
: insert at the beginning/end of the line -
o
/O
: insert at the line under/above ours
Move the cursor
π― You must be in command mode.
-
h
: move left β¬ οΈ -
j
: move down β¬οΈ -
k
: move up β¬οΈ -
l
: move right β‘οΈ
-
0
orCTRL-^
/$
: move the beginning/end of the line -
gg
/G
: start/end of the file -
w
/e
: start/end of a word
You can jump to a line with: xG
(ex: "5G") or :x
(ex: ":5"). See CTRL-G
.
You can use CTRL-F
/CTRL-B
to go down/up one screen.
Commands
-
:w
: save -
:wq
: save and exit -
:q!
: exit and discard changes -
:q
: exit
-
u
: undo -
U
: undo all actions of a line -
CTRL-R
: redo
Copy
-
yy
: copy the current line -
:yx
: copy x lines from the cursor line -
:n,my
: copy lines fromn
(>=1) tom
-
p
/P
: paste copy after/before the cursor
Delete
-
nx
/nX
:n
characters under/before the cursor -
d^
: every character of the line until the cursor -
D
ord$
: every character until the end of the line -
dd
/ndd
: delete the current line -
:n,md
: delete lines starting from the linen
until the linem
Intermediate
Search
-
/mot
: search (descending) -
?mot
: search (ascending) -
n
: next occurrence -
N
: previous occurrence -
%symbol
: return the matching symbol
Utils
-
<<
/>>
: dedent/indent -
~
: toggle lowercase/uppercase
Replace
-
:s/old/new
: replaceold
withnew
-
:s/old/new/g
: same, for all lines -
:s/old/new/gc
: same but ask for confirmation before -
:n,ms/old/new/g
: same but only n to m lines
Apply an operation before switching to the INSERTION mode:
-
s
: delete the character under the cursor -
cc
: delete the current line -
cw
: delete characters starting from the cursor position until the end of the word
Can only be used in command mode:
-
r
: replace the character under the cursor -
R
: replace characters while ESC is not pressed
π» To-do π»
Stuff that I found, but never read/used yet.
-
:w !sudo tee %
- investigate the
.
(current line?) -
$
: from the cursor until the end of the line -
e
: end of the word -
.
: under the cursor -
c
: a line -
^
: from the cursor until the start of the line
-
:!
: execute a command -
:f nom
: rename a file -
:e fichier
: open another file -
:r fichier
: print and insert file -
:!gcc
: compile inside vim