Sorry, I really can't help it but I like the vi editor on Linux. Not that I'm a vi wizard, absolutely not. But I know enough keystrokes to create and modify files quickly on Linux.
Call me an old-fashioned Linux addict that still lives in the dark ages of the prehistoric computer era, why not. Well, maybe I still am?
Because of that, one of the first things I'm always doing when installing a Linux distro is also installing vim if not installed already.
Still, there are some things that I do forget sometimes (or is it: always?). Hence, I've written some useful things about vim - at least for me - on this page. The list might grow in the future, we'll see.
vi is the basic, original editor and the letters stand for Visual.
vim on the other hand, is a kind of superset of vi and stands Vi IMproved. Improved in such a way that it is supported on a more wider range of OS'es and has also syntax highlighting, among other things.
The motor and original author behind vim was Bram Moolenaar. Unfortunately, Bram passed away recently (August 3rd, 2023). Rest in peace, Bram.
Chances are that vi is always part of the initial Linux distro, but vim is not. Therefore, it's always interesting to also install vim like so:
sudo apt install vim
It will add a couple of tens of megabytes to the disk but they're worth it.
In most Linux distro's, the crappy nano text editor is the default one. But glorie to Linux, we can change that.
Run the command select-editor and you will see most probably the following choices:
geertvc@TL0355418:~/mystuff$ select-editor
Select an editor. To change later, run 'select-editor'.
1. /bin/nano <---- easiest
2. /usr/bin/vim.basic
3. /usr/bin/vim.tiny
4. /bin/ed
Choose 1-4 [1]:
By default - and suggested - nano is proposed (as you can see in the above list). When you want to use vim basic, select 2.
From here onwards vim should be used as default editor.
One of the benefits of vim is the syntax highlighting. There's a default colour scheme available with the name... default (surprise!!!) but I don't like it so much (although, on an UBUNTU distro, it's not that bad at all...). Too much blue on a black background, hardly readable.
My favourite colour scheme is desert. It's smooth and restful for the eyes.
To add it to a running vim session, type :colo desert and the colours of the text should change, based on the type of file you're editing (bash script, java file, c file,...)
However, if you quit vim, the colour scheme is gone to. But fear not, there's always someone to the rescue!
To have it permanent, you can create a .vimrc file in your home directory. Using vim (what else?), create the file .vimrc and add the following two lines:
colo desert
syntax on
Save the file and edit for instance a bash file. You'll see immediately the syntax highlighting which - to me - is beautiful. However, one can't argue about tastes and flavours, can he?
When you install vi or vim chances are that you're getting strange behaviour when pressing the up/down/left/right keys. If you see A's and B's and so on printed instead of moving the cursor around in the correct way, add the following line to your ~/.vimrc file (create that file if not present yet):
set nocompatible
Save and quit the file et voilà: problème résolu!
When using vi or vim then by default, indentations are made of hard tabs with a width of 8 spaces. Nowadays, this is not the standard anymore. Favourite numbers are 2 or - mostly - 4 spaces as indentation and not using tabs anymore! The reason for this is that tabs can be set with different spaces on every individual's text editor and the resulting files can look very different from one computer to another.
Having 4 spaces as indentation is a far better way since spaces are... well... spaces on whatever text editor.
To change the default settings in vi or vim, add the following lines to the file ~/.vimrc:
set autoindent
set expandtab
set tabstop=4
set shiftwidth=4
All the above will make sure that:
when you press the "enter" key, the cursor will not move to the beginning of the line anymore, but to the location where the above line started. Very handy compared to the default behaviour!
space will be used instead of tabs when pressing the TAB key
4 spaces will be used when pressing the TAB key
4 spaces will be used when you use the commands << or >> in an already existing file to remove or add indentations
To add line numbers to the files, add the following to the ~/.vimrc file:
set numbers
To work with more than one window in vim you can do the following:
Open vim with a file
Press Ctrl-W, v to split the screen in 2 vertical parts
Press Ctrl-W, q to close one
Press Ctrl-W, Ctrl-W to jump between two windows
Press Ctrl-W, s to split the screen in 2 horizontal parts
Open a first file in VIM using the command vim bar.ic (example given).
If you want to open a second file in VIM run the command :e <other_file>. Example: :e hello.ic (example given).
To navigate to the next/previous file in VIM use :bn for the next file and :bp for the previous file.
To see the name of the file currently visible run the command Ctrl-G.
Command Action
k Move one line upwards
l Move one character to the right
h Move one character to the left
w Move one word to the right
W Move one word to the right past punctuation
b Move one word to the left
B Move one word to the left past punctuation
e Move to the end of the current word
1G Move to the beginning of the file
H Move to the top of the current screen
M Move to the middle of the current screen
L Move to the bottom of the current screen
Ctrl-G Move to the last line in the file
Ctrl-F Move one screen towards the end of the file
Ctrl-D Move 1/2 screen towards the end of the file
Ctrl-B Move one screen towards the beginning of the file
Ctrl-U Move 1/2 screen towards the beginning of the file
Ctrl-L Refresh the screen
5G Move to line 5 of the file (5 can be any line number)
/string Find text string forward
?string Find text string backward
n Find forward next string instance after a string search
N Find backward next string instance after a string search
ZZ Save the file exit vi
x Delete the character at the cursor
X Delete the character behind the cursor
dd Delete the line the cursor is on
d'a Delete from current position to marker a
10dd Delete the 10 lines following the cursor
yy Yank the current line
p Put the yanked line below the current line
P Put the yanked line above the current line
Make sure you've set vi or vim to use spaces if you press the TAB key
In the file you want to replace all tabs with spaces:
Make sure you're not in the INSERT mode anymore
Run the command :retab
General pattern: :[range]s/<pattern>/<string>/[flags] [count]
First occurrence in the current line: :s/<pattern>/<string>/
All occurrences in the current line: :s/<pattern>/<string>/g
First occurrence in the current file: :%s/<pattern>/<string>/
All occurrences in the current file: :%s/<pattern>/<string>/g
You can also search in a range: :3,15s/<pattern>/<string>/g
Case insensitive search: :%s/<pattern>/<string>/gi
Limited replacements: :%s/<pattern>/<string>/g 3
Start replacing from the current line till the end of the file: :,$s/<pattern>/<string>/g
ma: marker with identifier a (mb: same but marker identifier is b and so on)
d'a: delete everything from current position until marker a
vim has since long a built-in file explorer I recently discovered. It's called netrw. Many people use the NerdTree vim plugin but that doesn't seem to be necessary at all. At least, if you're satisfied with basic file explorer movements. Like me.
When you open vim with a file you can type the following commands when not in insert mode:
:Explore
Short: :Exp
Will show the content of the directory of the currently active file in vim but it will occupy the whole window. So, not that interesting.
:Hexplore
Will split the screen horizontally and show the file explorer in the bottom window. We're not used to see a file explorer like this, so for me that's a no-go.
Short: :Hex
When typing :Hex! the file explorer will be at the top of the window.
:Vexplore
Short: :Vex
Will split the screen vertically and will show the file explorer on the left side. This is a familiar file explorer view, so that's better.
When typing :Vex! the file explorer will be shown a the right side of the screen.
:Lexplore
Short: :Lex
Is a variant of Vexplore but but Lexplore will open a file on the window where we called the command. This is my favourite and I'm using this variant by default
The annoying thing about all the types of explorers mentioned above is that there's always a (totally useless, to me) banner visible at the top of the file explorer window that takes precious space. To get rid of this, I added the following option in my .vimrc file:
let g:netrw_banner=0
Another annoying thing is that the split is by default 50%, both horizontally and vertically (depending on the explorer flavour you've chosen). This means the file explorer will take up 50% of the screen real estate, which is way too much.
To correct this, I added the following option in my .vimrc file:
let g_netrw_winsize=15
This means the width (in case of a vertical explorer) or height (in case of a horizontal explorer) is restricted to 15 characters or lines respectively. This looks much better and you have more space left for the real vim editor to work in.
To autostart my favourite explorer Lexplore when vim starts, I added the following option to my .vimrc file:
autocmd VimEditor * Lexplore
let g:netrw_liststyle=1
This nicely opens the file explorer the way I want (filenames sorted, 15 characters wide). The netrw_liststyle option defines how the file structure will be show. There are 4 possibilities:
0: short listing (one item per line) but not sorted in the way I want the files to be sorted
1: long listing, including date/time/size... info but sorted in the way I want the files to be sorted
2: wide listing: files shown in multiple columns (although I've not seen such behaviour, honestly)
3: tree style listing
Possibility 1 is the only one that sorts the files the way I want them to be sorted. If possibility 2 would have had the same behaviour I would've gone for that option. Alas.
The only drawback of option 1 is that it's also takes the directories along in the sorting algorithm while it would be nice if the explorer would put the directories first in a sorted manner and then show all the files below the directory block, also in a sorted manner.
Maybe that's possible but I didn't find out how yet.
One disadvantage of the above "auto-enter-vim-with-netrw" is that the cursor will "stick" to the file explorer by default. So you have to move the cursor to the file instead. Always...
Lazy as I am, I don't want this. A nice solution is to submit another command, Ctrl-w w, after vim is started with netrw. This is done with the following, complete, command:
autocmd VimEnter * Lexplore | wincmd w
wincmd represents Ctrl-w so wincmd w sends out the Ctrl-w w key combination to vim, which means "jump to the other screen".
Credits: https://vi.stackexchange.com/a/37280
Below is the content of my .vimrc file on my Raspberry Pi's. Nothing fancy, nothing exotic but - to me at least - functional:
syntax on
colo desert
set smartindent autoindent
set tabstop=4
set softtabstop=4
set expandtab
set shiftwidth=4
set number
set mouse=a
"Change colour of line numbers to Red
highlight LineNr term=bold cterm=NONE ctermfg=DarkGrey ctermbg=NONE gui=NONE guifg=DarkGrey guibg=NONE
let g:netrw_banner=0
"let g:netrw_browse_split=1
let g:netrw_winsize=15
map fe :Lexplore<CR>
let g:netrw_sort_options="i"
let g:netrw_liststyle=1
" The below command will enter VIM and do the following extra things:
" - Start netrw with Lexplore as default view
" - Move the cursor to the file instead of leaving it into the netrw directory structure
autocmd VimEnter * Lexplore | wincmd w