Skip to main content

Vim

726 words·4 mins

Random snippets of Vim that I need to remember and use or else they will fade

:set

Print current settings - Useful to figure out tab stops etc.

Spacing + tabs
#

expandtab = use spaces vs tabs
noexpandtab = use tabs vs spaces

Re-indent file after making any type of tabbing changes.

:retab

Covert tab to spaces What soft tab is used for

Text objects
#

  • gg - Go to top of page
  • ) - Jump forward to next sentence
  • ( - Jump backwards to next sentence
  • } - Jump forward one paragraph
  • { - Jump backwards one paragraph
  • cc - Delete the current line and go into insert mode

Word text objects
#

  • e moves you to the end of a word word perfect for then issuing a daw or diw
  • diw delete an inside word that your cursor is sitting on.

Deleting
#

Stop using d$ to delete to the end of line but use c$ instead, this puts you into auto insert mode right after deleting.

Cleanup whitespace
#

We have a custom function called TrimWhiteSpace - Call that when you want to rid yourself of trailing white space. There’s also a status bar check that will display an icon if you have trailing whitespace.

Write to current file with sudo if you don’t have editing rights
#

:w !sudo tee %

LSP Python but can be for other languages as well
#

  • gd => go to def
  • ctrl-o => go back
  • shift-k => signature
  • space-e => show error

Editing multiple lines aka inserting something like a “-” at the front of something
#

  1. Hit ctrl + v to go into visual block mode. Do not confuse this with shift + v which will place you into visual line mode.
  2. Select to the bottom of the selection and hit shift + I
  3. Add what you want to the front and hit esc. - This should then apply to all of the lines you selected.

Quickly go to the bottom of a file and add new content
#

Go
  • G goes to the end of the file
  • Enters insert mode on a line below the current one

Quickly cutting and pasting a line right under cursor position
#

  • This example is cut line 23 and paste it right under the current cursor
:23t.

Deleting up to line number
#

If you are on your current line and want to delete everything up to a line number:

:,126d

Surround
#

Change single quotes to double

cs"'

Committing hunks with fugitive
#

  1. Pull up Git Fugitive via :Git or whichkey <leader>gg
  2. Jump to file you can use ctrl + n
  3. > on file to expand the file for changes.
  4. Visual select the lines of changes.
  5. s on the lines selected.
  6. cc - Fugitive shortcut for commit
  7. <leader>q or :wq

Splits
#

  • ni -O file1 file2 - Open both files in vertical splits
  • ctrl + w + x - Swap split positions
  • ctrl + w + w - Jump swaps
  • vsp file - if you are editing a file and want to open another in a veritcal split
  • FZF - After looking up a file, if you hit ctrl-v on the file it will open in a vertical split or ctrl-x will open in horizontal split.
  • Resize - ctrl=w | - Maximizes screen on current split (it’s like hiding the other split) ctrl+w = Resize things back to normal

Marks
#

  • m + [a-z] That is m + any letter from [a-z]
  • :marks - Displays all your marks
  • Deleting a mark :delmarks a

Jumping around using marks and backtick
#

  • ctrl + o - This should jump back to prior location but occasionally it will open up a previous file. This might be because of Global marks
  • ctrl + i - Jumps Forward
  • Use backtick + . - Jump to the last edit or last change you’ve made

Shada (Shared data) file
#

I leared about these file after noticing my marks persisting after I was deleting them. They are stored here: ~/.local/share/nvim/shada

Visually Selecting from current line to line number
#

shift + V - 55 - G

Yanking
#

Yanking lines of text and pasting below cursor Source

  • :10,20t.<enter>

Visual select up to line
#

Stop using arrow instead use: V35G - where 35 is the line number you want to select up to.