vim-reference




Pay Notebook Creator: Salah Ahmed0
Set Container: Numerical CPU with TINY Memory for 10 Minutes 0
Total0

Vim

vim is the upgraded version of vi, a "modal" text editor that leaves the user between two major modes, insertion mode and command mode.

The user writes text in insertion mode and can enter commands in command mode, like searching or replacing text etc.

In giving you a list of commands, anything surrounding by {} braces is to be considered a variable which is to be replaced by what is appropriate for you, except for the case in defining the effects of the braces themselves, which should be evident from context

Ex commands

Ex commands are commands that start with ":", like the command to replace text for example (:s),

to get a complete list of Ex commands type

:help holy-grail

The user is by default in command mode upon opening a file with vim. There are a number of keys to get the user into insert mode, where the user can write to the file what he/she wants.

Insertion mode

Here is a list of keys to get the user into insertion mode from command mode:

  • i: insert before current position
  • I: insert at start of the current line
  • a: append after current position
  • A: append at end of the current line
  • o: insert new line after current line
  • O: insert new line after previous line

Command Mode

Movement Keys

"word"

in moving through vim, vim defines a "word" as a string of characters up to separated by white space, as well as punctuation marks, which are considered words themselves.

command result
<Enter> move to first character of next line
- first character of prev line
h move one position to the left
j move one position to the bottom
k move one position to the top
l move one position to the right
w move to the next beginning position of a "word"
b move to the next beginning position of previous "word"
e move to the next end position of a "word"
W/E/B same as their lowercase counterparts except that this ignores punctation marks in defining a "word"

Jumping to new positions

command result
# or * jump to previous/next occurrence of current word (the word your cursor is currently on)
{ or } jump to next/previous paragraph
( or ) jump to next/previous sentence
% jump to enclosing character (any of these characters are enclosing characters "<>{}()[]" )
G jump to last line
{i}G or :{i} jump to line number i
gg jump to first line
'' jump back to the start of the line you were in before jumping to your current position
`` jump back to the exact position of the line you were in before jumping to your current position

Moving inside same line

command result
f{n} move cursor to next occurrence of character n in current line
F{n} move cursor to previous occurrence of character n in current line
t{n} move cursor to one position before next occurrence of character n in current line
T{n} move cursor to one position after previous occurrence of character n in current line
; repeat forward
, repeat backward
{i}| go to ith char of line
0 move to beginning of current line
^ move to beginning of text in current line
$ move to end of line

moving inside screen:

command result
H "home", moves cursor to top of screen
L moves cursor to bottom of screen
M "middle", moves cursor to middle of screen

Repositioning screen

Sometimes you want to scroll the page to get a better view of the text and context.

Moving the page screen does not move the cursor! (unless the cursor is not within view of the movement, in which case the cursor is moved)

  • ctrl + f
    • moves screen one page forward
  • ctrl + b
    • moves screen one page backward
  • ctrl + d
    • moves screen half page down
  • ctrl + u
    • moves screen half page up
  • ctrl + e
    • moves screen one line down
  • ctrl + y
    • moves screen one line up
  • z + &lt;ENTER&gt;
    • make current line top of screen
  • z + .
    • make current line center of screen
  • z + -
    • make current line bottom of screen

Repeat/Undo actions:

  • .
    • repeat last command
  • u
    • undo
  • U
    • undo changes made to current line only
  • ctrl + r
    • redo

Text manipulation

  • ~
    • change current character's case
    • i~
      • changes i characters' cases
  • J
    • join next line with current line

Copy, Cut & Paste

Yanking text:

Copying/cutting text is referred to as yanking text. When yanking text in vim, the text gets put into a special buffer (whether copying or cutting text). Deleting text is considered the same as cutting text, so when you delete text you by default are cutting the text and placing it into the special buffer.

The buffer is interesting in that it can store up to 9 items, as you will soon see, and you can access those yanked items by cycling through it's index.

Something annoying I've found with mac os is that it is difficult to paste text from os clipboard to the vim terminal, but, as a friend tells me, it is not the case with linux. I don't know a fix other than to open the file with another text editor to paste outside text, then resume session within vim.

Copy

  • y
    • copy text

Cut

  • d
    • deletes text
  • c
    • deletes text then enters into insertion mode automatically
  • {n}x: deletes n characters forward (by default one character)
  • {n}X: deletes n characters backwards

Paste:

  • p
    • paste after cursor
  • P
    • paste before cursor

Combining commands with movement keys

you can combine movement keys with these commands for more precise typing. Examples:

for k = command (whether it be y, d, or c)

  • {n}kk
    • does the command to n lines starting from current line (by default 1)
  • k{n}G
    • does the command to all text from the current line to the nth line of the document
  • k/{search_term}
    • changes text up to first occurrence of term example: "d/" deletes text until closing tag
  • k{n}{movement_key}
    • does the command to n tokens as given by the movement key
  • kiw
    • does command to current word (from its beginning)
  • ki{enclosing character}
    • enclosing characters are (""''<>{}()[])
    • does command to text enclosed with characters
    • example:
      • my name is "salah ahmed"
      • di" in this text results in:
        • my name is ""
  • k0
    • does the command to the start of the current line
  • k^
    • does the command to the start of text in the current line
  • k\$
    • does the command to end of the current line
  • K
    • shortcut for k\$

Unnamed buffer:

This buffer can store up to 9 yanked items. It keeps the text in a last in first out order.

  • "{n}p
    • paste yanked text from buffer number n

another method to pasting what you want is

  • p
  • check if thats what you want
  • if not, -> u
  • index is automatically incremented, so repeat process until you paste desired yanked item

Named buffer

This buffer can store up to 26 yanked text, ranging from buffer a to z

  • "{letter}{yanking_command}
    • example:
      • "fyy -> copies line into buffer 'f'
    • to append to a named buffer use the Upper case of the desired buffer
      • "Fyy -> appends line into buffer 'f'
  • "{letter}{paste_command}
    • paste from named buffer
    • example:
      • "fp -> paste buffer 'f' contents after cursor

replacing text

when you want to replace a single occurrence of text with something else you can use these commands

  • c/{search_term}
    • deletes text up to first occurrence of term, automatically goes into insert mode
  • {n}r{i}
    • replace n characters from current character cursor is on with i
  • R
    • goes into Replace mode, overwrites all text, press < esc > to exit
  • s
    • same as "c< space >", delete and go into insert mode
    • i + s
      • delete i chars, go into insert mode ("i + c + &lt;SPACE&gt;")

Visual Mode

typing v in command mode takes you to visual mode where you can highlight the text you want to manipulate.

  • v -> go back to command mode
  • V -> highlight entire lines (enter visual line mode)
  • y -> copy highlighted text
  • c -> delete highlighted text then enter insertion mode
  • d -> delete highlighted text
  • x -> delete highlighted text
  • : -> enter command with the range being the highlighted text (like replacing text etc)

saving and quitting file

  • ZZ
    • save and quit
  • ZQ
    • quit and don't save saving file:
  • :w
    • save file (write to disk)
  • :q
    • quit file, go back to terminal prompt:
  • :q!
    • force-quit, will exit even if changes not svaed
  • :e!
    • wipe changes from last save

unix commands

  • :!{cmd}
    • example
      • !ls

inserting output into file

  • :r !{cmd}
    • inserts unix command cmd output into file
    • example
      • :r !ls
  • :r {filename}
    • paste filename contents into current file

Useful commands

Snooze screen

  • ctrl + z
    • goes to terminal (z for sleep zzzzz's)
    • fg
      • go back to vim screen (foreground)

file information

  • ctrl + g

pasting from current file to new file

  • :a,b!cat > {newfile.txt}
    • pastes lines a-b from current file to newfile.txt

help page

  • :h or :help
  • :help :&lt;help-topic&gt;
    • example
      • :help :d

opening a file:

  • vim {filename}
  • vim {n} {filename}
    • go to line n
  • vim /{pattern} {filename}
    • go to line with first occurrence of pattern in filename

arguments

  • vim -o {filename1} {filename2}
    • views both files in split screen mode
  • vim -O {filename1} {filename2}
    • views both files in vertical split screen mode
  • vim -R {filename}
    • view filename in readonly mode
  • vim -r {filename}
    • recovers file
  • vim -c {cmmd} {filename}
    • : executes command cmmd after vim opens filename
  • vim -d {filename1} {filename2}
    • shows the differences between the two files

Configuration file

vim settings (like plugins to install, highlight colors, number of spaces in place of tabs etc) are to be placed in home directory (~/ in unix systems) in a file called .vimrc.

if you want to use custom setup for a file, make a custom configuration file and type in your working file :so {config_filename}

Final Tips

When trying to master vim, avoid using arrow keys and train yourself to use vim's movement keys, especially the search tools.

Learn to copy text from one file to another well and to switch between files quickly (however way you choose).

Plug in error checking syntax for the programming language you plan to write in, vim supports syntax checking for multiple languages.