Various notes on vi/vim editor
- Command Mode - for entering commands. Press Esc to enter Command Mode
- Insert Mode - for entering text. Type i to enter Insert Mode. Note there is an over strike version called replace mode, but for the purposes of distinguishing behaviors of key strokes, it is insert mode that will be contrasted with other modes in these notes.
- ex Mode - technically not a mode, but a separate line editor. Type : (colon) to activate
- :w - while in ex mode saves the file
- :w filename - while in ex mode save with the specified filename
- :wq - save and exit while in ex mode
- ZZ - save and exit while in command mode
- :q! - quit without saving while in ex mode
- :e! - abandon changes reverting to the original file while in command mode
Note: while you can navigate with the arrow keys, for efficiency you can use standard letter keys as well in command mode, so you don't have to lift your hands from the home keys.
- j - move down a line
- k - move up a line
- l - move right a character
- h - move left a character
- H - move to the top
- L - move to the last
- M - move to the middle of the screen
- $ - move to the end of the line
- 0 - (zero) move to the beginning of the line
- 8gg - move to line 8
- ^f - page down
- ^b - page up
- G - move to the end of the file
- gg - move to the beginning of the file
- :n filename - open a new file
- :e filename - open an existing file if it exist, otherwise open a new file
- :n - switch to the next file (if there are unsaved changes in current file you must save before switching)
- :N or :prev - switch to the previous file (also must save changes first)
- :e # - switch to the prior file (note the # is a literal and NOT a placeholder for a number). The # symbol is referred to as the alternate file. You use the percent symbol % to refer to the current file.
Note: if you want to abandon changes, instead of saving, when switching between files you can enter :e! in command mode to revert to the saved version of the file, and then switch to the other file without vi complaining.
If you open multiple files from the command line, you can switch back and forth between them with :n and :N. However, if you have one file open, and open another file within vi with :e filename you can no longer switch between them using :n an :N (it will say the new file is the only file). You can switch back to the other file by entering :e # (not a place holder, enter the literal #). It has a different behavior when you start with multiple files and then open a new file with :e filename, you can now switch back with :n to the other files, but you no longer have access to the file you opened with e:
- Position your cursor where you want to start copying text
- v - in command mode to go into Visual Mode
- Use the navigation keys to select the text that is highlighted
- y - to copy the selected text (yank), once yanked you return to command mode
- yy - copy the current line
- 6yy - copy the next 6 lines
- p - paste the yanked or deleted text after the current line
- P - paste the yanked or deleted text before the current line
- 2p - repeat the paste 2 times after the current line.
- :20,70y - yank 51 lines starting at and including line 20 through and including line 70.
- :put - in ex Mode paste the text starting on the next line (saves from having to go in and out Insert Mode to open up a new line)
To move text you first delete it, which places the text in a buffer, and the paste it
dd - delete the line or lines with #dd that you want to move p - paste the deleted lines after the current line
- u - in command mode undo last change. You can type u multiple times for multiple levels of undo
- Ctrl-R - redo the prior undo
- dd -- deletes the current line
- 6dd -- deletes 6 lines
- dw -- delete to the end of the word
- d$ -- delete to the end of the line
- d0 -- delete to the beginning of the line
- dG -- delete from current line to end of file
- dgg -- delete from current line to the beginning of file
- x -- deletes the character under the cursor
- :d -- deletes the current line
- :d3 -- deletes the next 3 lines
- cw -- from the current position remove text to the end of the current word placing you in insert mode to type the new text
- c$ -- from the current position remove text to the end of the current line placing you in insert mode to type the new text
- c0 -- from the current position remove text to the beginning of the current line placing you in insert mode at the beginning of the line
- cc -- replace the entire current line
- r -- replace the character under the cursor. You remain in command mode so you don't need to hit Esc afterword
- i -- enter insert mode
- I -- enter insert mode at the start of the line
- a -- enter insert mode to append text (one character to the right of where the i command would have placed you
- A -- enter insert mode at the end of the line
- s -- replace the character under the cursor and enter insert mode (x replaces without entering insert mode
- S -- remove the entire line placing you in insert mode at the beginning of the line
- R -- places you in replace mode which is an over strike version of insert mode (note you can toggle back and forth between insert mode and replace mode with your insert key.
- J -- in command mode joins two lines together
- . -- while in command mode repeats the prior command
-
From the command line:
To go to line 33 when the file is opened:
vi +33 myfile.c
-
In command mode -- type the line number and then press shift-g
-
In Ex mode -- type :33 to goto line 33
- /pattern -- search forward through the text for the pattern
- ?pattern -- search backward through the text for the pattern
- / -- repeat the search in the forward direction
- / -- repeat the search in the backward direction
- n -- repeat the search in the same direction
- N -- repeat the search in the opposite direction
Uses substitute to do search and replace using regular expressions
- :%s/oldtext/newtext or :%s/oldtext/newtext/g -- search and replace in the entire file. With the /g flags or no flag specified it will replace globally, and it will NOT prompt for confirmation.
- :%s/oldtext/newtext/gc -- search and replace in the entire file. With the /gc flags it will replace globally, but it will prompt for each one.
- :s/oldtext/newtext -- without the % in front of the s it will replace on the current line only.
- :,$s/oldtext/newtext/g -- search and replace from the current position to the end of the file. With the /g flags it will replace globally, and it will NOT prompt for confirmation.
- :,$s/oldtext/newtext/gc -- search and replace from the current position to the end of the file. With the /gc flags it will replace globally, but it will prompt for each one.
Place in .vimrc or enter the following in ex mode:
:set hlsearch
Place the cursor on the word you want highlighted and type * (asterisk) in command mode. This will highlight every occurrence of the word in the file.
To un-highlight the matched searches enter :noh in ex mode. Highlighting will be removed until the next search.
Un-highlight until the next search (ex mode in vim:
:noh
To turn it off:
set nohlsearch
To toggle on and off:
set hlsearch!
# or assign a key in .vimrc
nnoremap <F4> :set hlsearch!<CR>
Type Ctrl-N on the word to do code completion, for those languages that are supported.
In command mode highlight a header file or another file and type gf and it will go to that file if it is on the path (include files should be)
Highlight C word that is defined in a header file and type [i to display the header definition in bottom status area, or type [<tab> to jump to the definition in the header file.
To place vim in column mode press Ctrl-V, which is similar to visual mode in that it starts to highlight text as you move the mouse, with the difference being it does so in column mode. Once you copy or delete/cut the text it will then also paste in a column oriented manner when using p in command mode. You may have to go into insert mode to get things positioned first.
Examples of both .vimrc settings and settings made in the editor : mode
- Edit or create
~/.vimrcfile
For koehler colorscheme type:
syntax on
colorscheme koehler
From within vi type:
:colorscheme press space and then cycle through each one with tab. Simply hit return to set the currently displayed scheme. Note that schemes selected like this within vi will only apply to the current session. If you want it to be permanent across sessions then add it to the .vimrc file as described above.
" Tabs
set tabstop=4 " Change tab width. This will impact exiting text
set shiftwidth=4 " Indent width used by re-indent (<< and >>)
set softtabstop=4 " Width when pressing TAB (if less than tabstops additional
" tabs and spaces will be inserted to equal tabstops
set expandtab " Expand TABs to spaces
set smarttab " when using spaces instead of tabs, the backspace will
" remove shiftwidth number of spaces, making it appear as
" if there is an actual tab when there is not. Without
" the backspace removes a single space
For a detailed list of spell checking options refer to the Vim Reference Manual spell checking section.
" Spellcheck
set spelllang=en
set spellfile=$HOME/source/repos/viNotes/spellcheck/en.utf-8.add
Note this adds the spellfile, which has the custom words you added (by selecting the word in command mode and typing gz to mark it good), to the repo directory so that it can be used across computers.
To activate spell check withing a vi session, in ex mode enter:
:setlocal spell # for the local buffer only
:set spell # for all buffers
:set nospell # turn spell checking off
To mark a word good (adding it to your spellfile), to jump to the next or previous misspelled word, to get a list of suggestions, enter the following in command mode
zg # mark selected word as good
]s # jump to next misspelled word
[s # jump to the previous misspelled word
z= # get a list of suggested corrections on selected misspelled word
In the editor enter the following to set hex mod
:%!xxd
and enter the following to return to normal mode
:%!xxd -r
You can have 26 (letters a through z) named buffers to store yanked and deleted text. To use a named buffer in command mode, proceed the command with a double quote (") and the designated letter for that buffer.
- "ayy -- yank a single line to buffer a
- "b4yy -- yank four lines to buffer b
- "ap -- paste from buffer a
- "b2p -- paste twice from buffer b
By default the bell sounds whenever you move to the top, bottom, left side of file, which gets really annoying. To turn it off edit the .vimrc file in the home directory adding the following:
" Turn annoying bell off
set visualbell
Note how this config file uses a double quote for a comment
This can also be set within vi command mode but it will only apply to that session:
:set visualbell