6.4 KiB
Vim
I just noted down some vim commands/ motions /concepts etc. that were previously unknown to me.
I will probably make this more readable and consistent sometime. It's not complete as i am not finished learning vim.
Basics like navigating with hjkl were omitted. If you're reading this to learn, start with the :vimtutor.
Basic Commands
- u undo
- ctrl + r redo
Makros
- q to start recording
- w assign w to recording
- ... input key sequence
- q to end recording
- 85@w play recoding/makro 85 times
Searching and Replacing
-
/test jump to test in current line
-
:[range]s/{pattern}/{string}/[flags] [count]
-
????????????:%s/test/penis/g
- replace test with penis in whole file
- /g is for "global" - whole line
- % is the range -> entire file
- % == filename
-
If you changed the default case setting and you want to perform case sensitive search, use the
Iflag:- :%s/test/penis/gi
-
You can match regex here too, just keep in mind to escape things like + -> \+
-
more here: https://linuxize.com/post/vim-find-replace/
view search results
-
It is n for next and N for previous.
jump to the next/previous instance of the current word
-
Put the cursor on a word and hit the * key and you will jump to the next instance of that word.
The # key does the same, but it jumps to the previous instance of the word.
Deleting until
- dtc delete upto but not including c
- dfc delete upto and including c
- d$ delete until end of line
- dG delete until end of file
- dgg delete until start of file
- di( delete in ()
- printf("test"); - "test" would be deleted
- da( delete around (
- printf("test"); - ("test") would be deleted
Comment/ Uncomment multiple lines
Put your cursor on the first # character, press Ctrl V (or Ctrl Q for gVim), and go down until the last commented line and press x, that will delete all the # characters vertically.
For commenting a block of text is almost the same:
- First, go to the first line you want to comment, press Ctrl V. This will put the editor in the
VISUAL BLOCKmode. - Then using the arrow key and select until the last line
- Now press Shift I, which will put the editor in
INSERTmode and then press #. This will add a hash to the first line. - Then press Esc (give it a second), and it will insert a
#character on all other selected lines.
.vimrc customization
add these two lines to be able to customize your vimrc while still retaining the defaults
- unlet! skip_defaults_vim
- source $VIMRUNTIME/defaults.vim
- Comment lines out with "
Setting line numbers
- :set number and :set nonumber respectively for normal line numbers
- toggling is possible with :set number!
- :set relativenumber (:set rnu) and :set norelativenumber (:set nornu) respectively for relative line numbers
- toggling is possible with :set relativenumber! (:set rnu!)
- When both are active at the same time (:set number relativenumber (:set number rnu)) Hybrid line numbering is active
- Hybrid line numbering is the same as the relative line numbering with the only difference being that the current line instead of showing
0shows its absolute line number.
- Hybrid line numbering is the same as the relative line numbering with the only difference being that the current line instead of showing
- You can add those in your .vimrc to set those as default and not have to change that every time you open vim
A Command for Saving, Compiling and Running the Current C Code
Add this to your .vimrc:
- command M w | !gcc % -o %<.o && ./%<.o
to run it just type :M in vim
or make a Custom key Combination with the leader key (, + m):
- noremap m :w | !gcc % -o %<.o && ./%<.o
A key Combination for viewing the currently open file in Okular
- this is useful for writing markdown files and viewing them with the images, just press , + o
- noremap o :w | :silent !okular % &
Leader Key
Is by default / but can be changed with:
-
let mapleader=","
here i have set it to ,Vim waits for 1000 milliseconds after the
<Leader>key has been pressed, so if you take too long to press the next key in the sequence it won't be matched. This timeout can be changed by using:set timeoutlento set specific value.
Change Tab width
in your .vimrc
-
set tabstop=4will set the tab width equal to 4 spaces
indent/ unindent lines
Normal mode
- >> indent the current line
- 3>> indent the current line and two lines below (same as 2>j)
- >k indent the current line and the line above (same as 1>k or >1k)
- << unindent the current line
- 5<< unindent the current line and four lines below (same as 4<j or <4j)
- 2<k unindent the current line and two lines above (same as <2k)
- = auto indent code, use motion commands to indicate the portion to be indented
- =4j auto indents the current line and four lines below
- =ip auto indents the current paragraph
You can use any motion command with > and <. For example, >} indents till the end of the paragraph.
Visual mode
- > indent the visually selected lines once
- 3> indent the visually selected lines three times
- < unindent the visually selected lines once
- = auto indent code
be able to use bash aliases in vims ! mode
Bash doesn’t load your .bashrc unless it’s interactive. To make the setting permanent, add set shellcmdflag=-ic to the end of your .vimrc file
resize vim correctly with the kitty terminal emulator
set term=kitty
Vim motions
-
Use
w(word) command to jump to the beginning of the next word -
Use
b(back) to jump to the beginning of a word backwards -
Use
e(end) to jump to the end of a word -
Use
geto jump to the end of a word backwards -
0: Moves to the first character of a line -
^: Moves to the first non-blank character of a line -
$: Moves to the end of a line -
g_: Moves to the non-blank character at the end of a line -
}jumps entire paragraphs downwards -
{similarly but upwards
Vim tabs
open more than one file at startup using the -p option. If you want to open three files in separate tabs, you’d use this syntax:
vim -p file1 file2 file3
- Or in Normal mode run
:tabnew filename - You can switch between tabs using
:tabnand:tabp, or you can usegtwhile you’re in normal mode. - If you have a lot of tabs open, you can use
:tabfirst, or just:tabfir, to jump to the first tab, and:tablastto jump to the last tab that’s open. - Close all tabs:
:qa - To save work in all tabs and quit:
:wqa