Skip to content

Instantly share code, notes, and snippets.

@stoeckley
Forked from tkersey/xcode-vim.markdown
Created October 2, 2024 19:21
Show Gist options
  • Save stoeckley/8ef487b9f102a248d7a9094a7771dda6 to your computer and use it in GitHub Desktop.
Save stoeckley/8ef487b9f102a248d7a9094a7771dda6 to your computer and use it in GitHub Desktop.
Notes for working with Xcode VIM mode

Xcode VIM

This document is a scratchpad for helping me learn commonly used actions in Xcode's VIM mode.

Commands are case-sensitive. A command of N means pressing shift + n on the keyboard.

[Formatting is a WIP]


Normal Mode Commands

Basic Navigation

Vim Input Xcode Standard Action
h move left
l move right
k move up
j move down
0 move to first character in line
$ move to last character in line
^ ⌘← move to first non-whitespace character in line
g0 ⌘←← goto before first non-whitespace character in line
g_ ⌘→ goto to last printed character in line
gg ⌘↑ goto to first line in file
G ⌘↓ goto to last line in file
% - move to matching character in pair, e.g. {}, ()
{ - jump to next paragraph above
} - jump to next paragraph below

Word Navigation

Vim Input Xcode Standard Action
w ⇧→ jump forward to start of word
W ⌃→ jump forward to start of Word, ignoring punctuation
e - jump forward to end of word
E - jump forward to End of word, ignoring punctuation
b ⇧← jump backward to start of word
B - jump Backward to start of word, ignoring punctuation

Scrolling the Viewport

Actions that scroll the code viewport without moving the cursor.

Vim Input Action
zz scroll viewport to center on current line
zt scroll line to top of viewport
zb scroll line to bottom of viewport
H move cursor to top of viewport
M move cursor to middle of viewport
L move cursor to bottom of viewport

Jumping the Cursor

Actions that scroll the code viewport by moving the cursor.

Vim Input Action
^e scroll one line down
^y scroll one line up
^d scroll one half page down
^u scroll one half page up
^f scroll one page forward
^b scroll one page back

Cut, Copy, and Paste

Vim uses yank as a naming convention analgous to the copy clipboard command. The delete command is analogous to the cut command as it places the deleted content into a buffer that can be pasted.

Vim Input Xcode Standard Action
yy Copies the current line and places it into a buffer that can be used with p to paste.
y Copies characters from the current cursor position.
Y Copies the entire line at the cursor position.
p –  Pastes characters copied from y or d commands at the current cursor position.
P –  Pastes characters copied from y or d commands before the current cursor position.

Deleting Text

Vim Input Xcode Standard Action
dP N/A Pressing d begins a delete that matches a selection based on the next position command of P. For example, dw deletes all characters jumping forward to the start of the next word. Usable for most all cursor position commands such as w, e, b, B.
dd ⌘D Deletes the current line.
dN N/A Deletes N number of lines where N from the cursor position. e.g.: d5 deletes the next 5 lines.
D –  Deletes characters to the end of the line from the current cursor position.
x Deletes the character at the current cursor position.
X Backspace Deletes the previous character from the character position.

Move to Characters in Line

Vim Input Xcode Standard Action
f{char} - move to next character ahead of cursor in line
F{char} - move previous character behind cursor in line
t{char} - move behind next character ahead of cursor in line
T{char} - move ahead of next character behind cursor in line
; - repeat previous f, t, F or T movement
, - repeat previous f, t, F or T movement, backwards

Selecting Text

Vim Input Action
vP Selects text up to the next position, P. e.g. ve selects from the cursor to the end of the word.
V Selects the current line. Subsequent position commands will extend the selection. Very handy for beginning multi-line selections and edits.

Indentation

NOTE: Xcode will drop the current selection after the action is performed, which makes these commands not as good as a standard Xcode indentation command with ⌘+([ or ]) which retains the selection. Its really only useful for making the indent once, unlike the built-in action which can indent multiple times.

Vim Input Action
<< Indent line(s) left
>> Indent line(s) right

Misc. Handy Actions

Vim Input Action
~ Changes the case of the character at the cursor position and moves the cursor to the next character.

Undo and Redo

Vim Input Xcode Standard Action
u ⌘z Undo last action.
Nu –  Undo last N number of actions. e.g. 3u undo 3 times.
^r ⌘Z Redo last action

Search

Vim Input Xcode Standard Action
/ ⌘f Begin a forward text search.
? ⌘G Begin a backward text search.
n ⌘g Move to next search result.
N ⌘G Move to previous search result.

Changing Modes

Vim Input Action
i "Insert here". Enter visual mode at the start of the current cursor position.
a "Append here". Enter visual mode at the end at the current cursor position.
I "Insert at start". Enter visual mode at the beginning of the line.
A "Append at end". Enter visual mode at the end of the line.

Visual Mode Commands

Section

Vim Mode Action
esc exit to normal mode

VIM Usage in Practice

Below are tips for using and combining the commands in VIM to perform common actions.

New Lines

O - add a new line above the cursor and enter insert mode

o - add a new lnie below the cursor and enter insert mode

Selecting Text

ce - Edit from cursor to end of word and replace

v$ - Select from cursor to end of line

Find & Replace

Basic finding

  1. / Start the find, enter the text to fnid and press Enter
  2. n and N to move between the occurences found in the file

Replace

  1. vey - Selects a word and yanks (copies) it
  2. viwp - Selects a word and puts (pastes) the copied text

Copy/Paste Lines

To copy 2 lines… 2yy - Copies 2 lines

To cut 2 lines 2cc - Cuts two lines

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment