Vim
Introductory Vimming
To start using Vim, first install it from here
To get some quick tips and information you can use the Vim tutor application, this can be launched from CMD using the
vimtutorcommand. For some reason this doesn't work right from Powershell
Creating a file
- From Powershell or CMD navigate to a directory in which you can create a new file, then run
vim filename.txtto create a new file with the given name and open the file in the Vim Editor - Now that you have the file open you can move around using either your arrow keys or the
h,j,k,lkeys
Basic Modes
Vim has two main modes, a viewing mode and an editing mode, to exit the editing mode you simply need to click the esc key. To enter the edit mode you have the following two options:
iwill allow you to insert text before the cursorawill allow you to append text after the cursorAwill allow you to append text at the end of the line
Save and Edit Files
Next, once out of edit mode you can:
:wto save changes:wqsave changes and close:q!abandon changes and close
To open and continue editing a file you can again run vim filename.txt
To delete content from a file you have the following options:
xto delete the current highlighted characterdwto delete the currently highlighted wordd$to delete until the end of the line
If you're editing and would like to go and do something else and then come back to edit the file, you can do the following:
shift + zto pause the editing session and put it in the backgroundfgto resume editing the file
Operators and Motions
Commands in Vim consist of an operator and a motion, for example the d$ command d is the delete operator and $ is the motion. Some other motions are:
wuntil before the next wordeuntil the end of the current word$until the end of the line
Additionally, you can add a number before an operator to repeat it. e.g 2w will move two words. By combining this with the command we can delete 2 words with d2w or d3$ to delete three lines
Since deleting an entire line is also a common task, you can delete entire lines with
dd, so3ddcan also be used to delete three lines
Put
Use p to put previously deleted text after the cursor (this is sort of like cutting and pasting), you can use this with dw or de for word based deletions or dd and d$ for line based deletions
Replace
To replace a character wth another you can use r followed by the character you want to replace, for example re will replace the highligted character with an e, additionaly to replace multiple characters you can use R
Change
To change until the end of a word use ce, this will allow you to overwrite the current word from the current current position. This operator works the same as when using the delete operator
Moving Around
ctrl gto see where in a file you currently areGto move to the bottom of a fileggto move to the top of a file- A line number followed by
Gto go to a line, e.g12Gwill take you to line 12
Searching
To search you can use / followed by a search phrase and then click enter
nto search in the same directionNto search in the opposite direction?to search in the backward directionctrl oto go back to where you were beforectrl ito go forward%will search for a matching bracket,), ], }:nohwill clear the highlighting from the search results
Substitution
Substitution is done using the :s command, the structure of this command works like so:
:s/old/new- replace the first occurence in the line:s/old/new/g- thegflag means to substitute in the lines/old/new/gc- the addedcflag means to replace every occurence and ask for a confirmation each time:#,#s/old/new/g- to replace all the occurences withing a line range, e.g between line 1 and 10::1,10/old/new/g
Executing Shell Commands
From Vim, you can execte a command on the shell in which you've launched from using the :! followed by the command you want to execute
Selecting Text
Aside from the normal edit and view modes we also have visual mode which allows you to read in/select a section of text
- You can use
vto enter visual mode - When in visual mode
:wfollowed by a file name will write the selection to a file. Verify that you see:'<,'>w Filenamebecause this will indicate you are in the correct mode - To insert the contents of the written file you can use
:rfollowed by the filename to insert the contents into your current file - You can also use
xto delete the highlighted text - Using the above concept you can also read the output from a system command with
:r !followed by your command, so to read the contents of your current directory into the file you can do:r !ls
Inserting Lines
To insert and enter edit mode you can use o or O
owill insert a line below your cursorOwill insert a line above your cursor
Copy and Paste
To copy and paste text you can use the yank operator, this is done using y
- Start visual mode with
v yto copy the highlighted textpto put the text
y also functions as an operator so you can use the normal functionality as with other Vim commands, like yw to copy a word
Undo and Redo
- To undo use
u - To redo use
ctrl + r
Setting Options
Options/settings for Vim can be set using the :set command followed by the option name, some common options are:
icto ignore case when searchinghlsto set the incsearch option- To ignore case for a single search you can use
/searchterm\c :nohlsearchwill clear the search highlighting
Multiple Windows
To split your screen into multiple windows you can use:
:spfollowed by the file path for a horizontal split:vspfollowed by the filepath for a vertical splitctrl + wtwice to toggle focus between windows:wato save all windows:wqato save all windows and quit:qato quit all windows
Also for resizing windows you can make use of some of the following commands:
ctrl + W =to make all windows equal sizes:res nto resize windows byn
And ctrl + W r to rotate the windows
Opening a Terminal
You can open a terminal using the :term command, and you can exit terminal mode using ctrl + \ ctrl + n, thereafter using :q to close the terminal window
Help
To get help you can use the :help command, to search for a specfic topic you can just add it after the help command like :help nohlsearch, you can then type :q to close the help menu
Enabling Features
To enable Vim features you can make use of a startup script or a vimrc file
Miscellaneous Options
:set numberto turn on line numbers:syntax onto turn on syntax highlighting