Skip to content

Instantly share code, notes, and snippets.

@joshschmelzle
Last active September 8, 2020 23:10
Show Gist options
  • Save joshschmelzle/fa02f7a8ad6fcbb868d9c61c0c63656c to your computer and use it in GitHub Desktop.
Save joshschmelzle/fa02f7a8ad6fcbb868d9c61c0c63656c to your computer and use it in GitHub Desktop.
My Vim Notes and Cheatsheet

PyCharm

Use Ctrl+G to navigate to a line in the editor. Use Ctrl+F12 to navigate the code base.

Git

git commit git commit --amend use this to amend the last commit.

git commit --amend --author="Author Name <email@address.com>" to change the author.

For example, if your commit history is A-B-C-D-E-F with F as HEAD, and you want to change the author of C and D, then you would:

  1. Specify git rebase -i B if you need to edit A, use git rebase -i --root
  2. Change the lines for both C and D from pick to edit
  3. Once the rebase starts, it would first pause at C
  4. You would git commit --amend --author="Author Name <email@address.com>
  5. Then git rebase --continue
  6. It will pause at D
  7. Then you would git commit --amend --author="Author Name <email@address.com> again
  8. git rebase --continue
  9. The rebase would complete
  10. git push -f to update your origin with the updated commits

Redo/Undo

u to undo. ctrl-r to redo.

U does something else. it creates a new change to reverse the previous change. so it is undo-able.

Go to beginning or end of a line

^ to move the cursor to the beginning of the line. $ to move the cursor to the end of the ling.

Search

in normal mode type / to enter search mode. the result will be come highlighted.

n for next N for previous

using / will search forward. using /? will search backwards

search the word under the cursor. type * or g*. results will be highlighted.

Find and Replace

:s/pattern/replace/g replace "pattern" with "replace" on current line :%s/pattern/replace/g replace "pattern" with "replace" in the current file

Visual Block Mode (commenting and uncommenting)

First, move the cursor to the first char of the first line in block code you want to comment, then type:

Ctrl + v

then vim will go into VISUAL BLOCK mode.

Use j to move the cursor down until you reach the last line of your code block. Then type:

Shift + i

now vim goes to INSERT mode and the cursor is at the first char of the first line. Finally, type # then press ESC and the code block is now commented.

To decomment, do the same things but instead of typing Shift + i, you can just type x and then press ESC to remove all # after you've hightlighted them in VISUAL BLOCK mode.

Working Directory

:pwd will show you the Vim current working directory (CWD). You can change the CWD by using :cd directory.

Fixing the not an editor command ^M in a cross platform .vimrc file (use :w ++ff=unix to fix line endings)

This gist is for when you something like Not an editor command: ^M when you try to use vim with your customized .vimrc. This is caused by Vimscript files that have a Windows style CR-LF line endings when those Vimscript files are used on Linux.

Here is an example of what the errors may look like:

line    3:
E488: Trailing characters: nocompatible^M
line    5:
E492: Not an editor command: ^M

To fix this you can convert the style endings with something like the following command. Open the affected Vimscript files in Vim and use the following command:

:w ++ff=unix

Thanks @tylerl

There's several things that all need to be in place. Just to summarize them all in one location:

Set the following option:

:filetype indent on
:set filetype=html           # abbrev -  :set ft=html
:set smartindent             # abbrev -  :set si

Then either move the cursor to the top of the file and indent to the end:

gg =G

Or select the desired text to indent and hit = to indent it.

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