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 modeINSERTION: the insert/edit modeVISUAL: [...]
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-runtimesudo 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 cursorI/A: insert at the beginning/end of the lineo/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 β‘οΈ
0orCTRL-^/$: move the beginning/end of the linegg/G: start/end of the filew/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: undoU: undo all actions of a lineCTRL-R: redo
Copy
yy: copy the current line:yx: copy x lines from the cursor line:n,my: copy lines fromn(>=1) tomp/P: paste copy after/before the cursor
Delete
nx/nX:ncharacters under/before the cursord^: every character of the line until the cursorDord$: every character until the end of the linedd/ndd: delete the current line:n,md: delete lines starting from the linenuntil the linem
Intermediate
Search
/mot: search (descending)?mot: search (ascending)n: next occurrenceN: previous occurrence%symbol: return the matching symbol
Utils
<</>>: dedent/indent~: toggle lowercase/uppercase
Replace
:s/old/new: replaceoldwithnew: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 cursorcc: delete the current linecw: 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 cursorR: 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 linee: end of the word.: under the cursorc: 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