Basic Vim
Vim seems complicated and it is. The goal is to make things move a little easier for the user. If you can learn the tricks of the trade. Vim becomes something special.
π‘ With the !
command you can run Cli commands inside of Vim. Using this
we can avoid writing filters. Use something like Rust
of Golang
instead.
I don't like neovimπ€¬. It's not going to help me with hacking.
Basic Vim Commands
These commands are the ones required to use and save the files.
:e [file]
- Opens a file, where[file]
is the name of the file you want opened.:w
- Saves the file you are working on.:w [file]
- Saves the file to a file name were[file]
:wq
- Save your file and close Vim:q!
- Quit without saving
Movement Commands
When you use vim the goal is to use the keyboard efficiently. How can we do this? Using the keys to navigate around. Without moving your hands around to arrow keys.
h
- moves cursor to the leftl
- moves cursor to the rightj
- moves cursor down one linek
move cursor up one lineH
- put cursor at the top of the screenM
- put cursor in the middle of the screenL
-put cursor at the bottom of the screenw
- put cursor at the start of the next wordb
- put cursor at the start of the previous worde
- put cursor at the end of a word0
- place cursor at the beginning of a line$
- place cursor at the end of a line)
- start of the next sentence(
- start of the previous sentence{
- start next paragraph or block}
- start previous paragraph or blockCtrl + f
- one page forwardCtrl + b
- one page backgg
- start of fileG
- end of file
Editing Commands
yank
- copyput
- pastey
- yankp
- putdd
- delete single lineyy
- copies a single line
π¬ You can paste anything copied. If it is highlighted or copied via yy
.
With movement commands you can add the number of times to complete that
task. For example, 5yy
copies 5 lines.
yy
- copies a lineyw
- copies a wordy$
- copies from where cursor s to the end of a linev
- highlight one character at a time using arrow buttons of the h,k,j,l buttonsV
- Highlights one line, and movement keys can allow you to highlight additional linesp
- paste what is copiedd
- deletes highlighted textdd
- deletes line of textdw
- deletes a wordD
- deletes everything from where cursor is to the end of the lined0
- deletes everything from where cursor is to the beginning of the linedgg
- deletes everything from where cursor is to the beginning of the filedG
- deletes everything from where cursor is to the end of the filex
- deletes a single characteru
- undo last operation.I do not understand this
u# allows you to undo multiple actionsCtrl + r
- redo last undo.
- repeats the last action
Searching Text Commands
π¬ Using Vim you can search your text, find and replace text within your document. If you opt to replace multiple instances of the same keyword or phrase, you can set Vim up to require or not require you to confirm each replacement depending on how you put in the command.
/[keyword]
- searches for text in the document where keyword is whatever keyword, phrase or string of characters you're looking for.?[keyword]
- searches previous text for your keyword, phrase or character stringn
- searches your text again in whatever direction you last search wasN
- searches your text again in the opposite direction:%s/[pattern]/[replacement]/g
- replaces all occurrences of a pattern without confirming each one:%s/[pattern]/[replacement]/gc
- replaces all occurrences of a pattern and confirms each one.
Working With Multiple Files
π¬ You can also edit more than one text file at a time. Vim gives you the ability to either split your screen to show more than one file at a time, or you can switch back and forth between documents. Document === buffers.
:bn
- switch to next buffer:bp
- switch to previous buffer:bd
- close a buffer:sp [filename]
- opens new file and splits your screen horizontally to show more than one buffer:vsp [filename]
- opens a new file and splits your screen vertically to show more than one buffer:ls
- list all open buffersCtrl + ws
- split window horizontallyCtrl + wv
- split window verticallyCtrl + ww
- switch between windowsCtrl + wq
- quit a windowCtrl + wh
- moves cursor to the window to the leftCtrl + wl
- moves cursor to the window to the rightCtrl + wj
- moves cursor to the window below the current windowCtrl + wk
- move cursor to the window above the one you are in
Marking Text In Visual Mode
Visual mode allows you to select a block of text in Vim. Once the block of text is selected. You can use visual commands to perform actions on the selected text. Such as deleting it, copying it, etc.
v
- starts visual mode. You can select a range of text and run a command.V
- starts line select visual mode.Ctrl + v
- starts visual block mode. selects columnsEsc
- exit visual mode
π¬ Once you have selected a range of text. You can now run command on that text.
d
- delete marked texty
- yank/copy marked text>
- shift text right<
shift text left
Tab Pages
You can use tabs inside Vim. You can work on multiple files without having to close and save.
:tabedit [file]
- opens new tab and will take you to edit [file]gt
- move to next tabgT
- move to previous tab#gt
- move to a specific tab number:tabs
- list all open tabs:tabclose
- close single tab
Sample Vim Workflow Example
- Open a new or existing file with
vim [filename]
- Type
i
to switch intoinsert
mode so that you can start editing the file - Enter or modify the text in your file
- Once you are done. Press the escape key
Esc
to get out of insert mode and back to command mode - Type
:wq
to save and exit your file