Cheat Sheet for vim

vim-wikipedia.png

Image credit: Wikipedia

I will be doing this in Kubuntu 20.04.

Vim is a powerful editor that is included in most Linux distributions. I find it is far less bulky than its main competitor: emacs.

Getting Started

Vim was developed in the bad old days of single terminals. This makes its learning curve rather steep at the beginning, but once you learn that vim has a modal design, you can begin working with it. Vim starts in command mode — to enter insert mode mash the ‘i’ button. To get back to command mode press <ESC>.

To save and exit enter (in command mode): “:wq" — meaning ‘write’ the buffer to the disk and ‘quit.’ If you haven’t saved yet you will get an error from vim telling you so. If you really do want to quit then type “:wq!”

On most Linux installations there is a vimtutor tutorial available. Just type vimtutor and a prompt and off you go!

General

Split screen

Open a terminal and type:

vim first-screen.txt

You will probably want to save this file at this stage with the text “first-screen” or similar to mark it. After doing so, make sure you are in command mode and enter:

:split second-screen.txt

Mark this file as the second screen. Then type this (again, in command mode):

:vsplit third-screen.txt

As before, mark this file as the third screen. You should have something in front of you that looks something like the screenshot, depending upon how much you fiddled around with the different screens:

vim-cheatsheet1.png

vim split over 3 screens

Tabs:

command mode:

tabnew <file>: open <file> in a new tab
tabn: go to the next tab
tabp: go to the previous tab
:wqa - write (save) all tabs and exit

edit modes:

gt: go to the next tab
gT: go to the previous tab
{i}gt: go to the tab in position i

Creating vim sessions.

:mksession <session-path>

I find I don’t typically need to use vim buffers.

Yanking and Pasting:

yy - Yank single line to the buffer
{num}yy - Yank num lines to the buffer
p - Paste lines to the screen
dd - Delete a single line and put it on the buffer
(num}dd - Delete num lines, put them on the buffer

Repeat Last Command

I find this extremely powerful as a quick replacement for grep.

.

That’s it. A period!

Search

/search - search for the string “search”
n - repeat search in the same direction
N - repeat search in the opposite direction

Undo & Redo

Command Mode:

:u - undo
<CTRL> + r - redo

Getting help

:help

Coding

Indenting

>> - indent one shiftwidth
<< - de-indent one shifwidth

Go to line:

{num}gg - go to line num

Line numbers

Converting tabs to spaces

Inserting or deleting comments quickly

Moving to matching braces

Further Reading

vi (Wikipedia)

https://www.cs.oberlin.edu/~kuperman/help/vim/windows.html

https://linuxhint.com/how-to-use-vim-split-screen/

https://www.linux.com/training-tutorials/vim-tips-using-tabs/

https://vim.rtorr.com/

https://vim.fandom.com/wiki/Quick_tips_for_using_tab_pages

https://learnvimscriptthehardway.stevelosh.com



Previous
Previous

GNU Make

Next
Next

Git 8: Remote Branches