Wednesday, March 23, 2022

Vim popular shortcuts


    In this article I will walk you through some of the popular commands that you can use to get up fast and start using the Vim editor. Vim was written by Brian Moolenar as a charity ware, meaning all of its donations goes to the non - profit in Uganda I think.

Why one should learn vim?

    Vim is a popular visual editor from almost the beginning of computers whose predecessor was vi. It has been used widely by mainly Open source developers since the development of *nix systems.

    Today's popular editor's like Visual Studio Code has vim support through its extensions.

    Vim has three main modes Edit, Visual and Command, Enter Edit by typing i, Visual by typing v and Command by typing :

We will go through the commands for each of the modes below.

Edit mode

Editing of text is done in this mode and hence have no shortcuts.


i - insert or start typing or to enter edit mode
A - go to end of line and edit
o - creates new next line and you can start editing

Visual  Mode

Read only mode mainly for navigation, searching, register operations.

v - begin visual mode
Ctrl+v - visual block
gg - go to top of file
GG - go to end of file
y - yank ( copy ) selection
yy - yank line
p - paste after cursor
P - paste before cursor
3p - paste three times after cursor
/word - search for word in forward (top to down) direction
?word - search for word in reverse (bottom to up) direction
w - jump word
e - jump to end of word
b - jump word backwards
2w or 2e - jump two words or jump to end of two words
r - replace letter
x - delete letter
cw - change word
ce - change to end of word
c2w or c2e - change to two words or end of two words

Searching -
n - search for next position of word
N - search for previous position of word/ back search
* - find next word
# - find previous word

Navigation -
`" - go to last cursor position after opening a file
`. - go to last edit position after opening a file
0 - go to start of line
$ - go to end of line
A - go to end of line and edit
G - go to beginning of file
% - go to end or start of braces {} or [] or () .....
[{ - go to opening of braces
]} - go to ending of braces



Ctrl + I - go previous cursor positions even other files
Ctrl + O - go after cursor positions even other files
Ctrl + ] - on tag link goes to link
Ctrl + t - goes back to link

h - nav left
l - nav right
k - nav up
j - nav down

Command  Mode

Mainly to perform file operations of the file and run shell commands as well.

:w - write to disk
:wq - write to disk and close editor
:q! - close editor but not write any changes
:tabedit filename - creates new tab opening filename in it
:gt - go to next tab
:gT - go to previous tab
!ls - execute shell commands, in this case ls
:sort - after selecting say a list of words or numbers in visual. Typing this sorts them in alphabetical or numerical ascending order

:colorscheme solarized - switching themes found in $vimruntime/colors. Others include `morning` , `evening`, `peachpuff`, `industry`, `murphy`, `blue`, `darkblue`, `desert` and all
:set background=light - or `dark` depend on time of day for a specific theme.

:%s/word/new/gc - replace word with new in the whole file with confirmation each time. Regex patterns is supported here.

FAQ

How to copy text to command mode?
After yanking the text and pressing : Do Ctrl+R and "

How to paste the previously yanked ( copied ) text?
"0p - pastes the second last copied
:reg shows the paste registers. Use "6p to paste the 6th register


Browser background color or monitor brightness

Have you been in a situation where you are almost sitting the whole day in front of your browser and going through a document and find that ...