Vim

From DikapediaV2
Jump to: navigation, search


vim - Vi IMproved, a programmers text editor.

Vim Cheatsheet: https://devhints.io/vim


Go - end of file, new line.



Vim shortcut keys:

":"                              (shift + ;), Brings up the vim command line
esc                              To get out of the command line thing.
"i"                              Insert mode
"a"                              Append after cursor position
"r"                              Replace letter
"o"                              Open line below cursor position automatically goes into insert mode
"O"                              Open line above cursor in insert mode
"dd"                             CUT line
"p" or 10p                       PASTE line, or paste 10 times
"yy"                             COPY line
"3dd"                            CUT 3 lines from where your cursor is. Can use this in cmd line
"4yy"                            PASTES 4 lines from where you cursor is. Can use this in cmd line
"u"                              UNDO
:w                               Saves
SHIFT + zz                       Save file
:wq or :x                        Save & Quit
:q                               Quit
:q!                              Force quit, no save
:d                               Delete line
:5d                              Delete 5 lines
:/[strg]                         Search for a string, searches DOWNward "n"
:?[strg]                         Search for a string, searches UPward "n"
"n"                              Move to next highlighted/cursor
:1                               Moves cursor to the first line
:[x]                             Moves cursor to the line [x]
:$                               Moves cursor to the last line
:1,$s/[strg]/[replacement]/      Searches from first line to the last line ($) for [strng] and replaces it with [replacement]
:1,$s/[strg]/[replacement]/g     G is for global.
                                Replaces multiple [strg] in ONE line.
:1,$s/[strg]/[replacement]/gi    G is for global, i is for case insensitive
:1,.g/[strg]/d                   Search for that word until where my cursor is (.) and delete it
:se nu                           Shows the line numbers
:se nonu                         Hides the line numbers
:![command]                      Run command but does not exit VIM.


How to change colors in Vim for better visual (dark blue comments are hard to read)


As you are using a dark background in your terminal, you simply need to set

:set background=dark

instead of the default

:set background=light

The colors are then automatically correctly set.

If you want to have this permanently, add the line to your $HOME/.vimrc file.

set background=dark


Copy & Pasting


Copying (Yanking)

yy: Copy the current line in vi
3yy: To yank multiple lines in vim, type in the number of lines followed by yy. This command will copy (yank) 3 lines starting from your cursor position.
y$: Copy everything from the cursor to the end of the line
y^: Copy everything from the start of the line to the cursor.
yiw: Copy the current word.

Cutting (Deleting)

dd: Cut the current line
3dd: Cut 3 lines, starting from the cursor
d$: Cut everything from the cursor to the end of the line

Putting (Pasting)

P (uppercase): Paste before your cursor
p (lowercase): Paste after your cursor


Spawn a shell using Vi/Vim


This is a feature, not a bug, but you can spawn a shell with Vim by opening/creating a file using sudo/root access, and typing:

:!/bin/bash

For information about Vim privilege escalation, see articles:
https://medium.com/@davidkarpinski1/privilege-escalation-through-vim-1a6524a10cea
https://medium.com/@pettyhacks/linux-privilege-escalation-via-vi-36c00fcd4f5e