Skip to content
Sergey Lukin edited this page Mar 18, 2023 · 15 revisions

If there is a filetype plugin distributed with Vim that you want to completely disable, make your own (perhaps empty) settings file in ~/.vim/ftplugin/ directory and add this line to it:

let b:did_ftplugin = 1

Trigger filetype recognition

Just pretend opening a file:

:e

See more details in this SO thread

Enable/Disable Tabs

Sometimes you just need to quickly move between tabs and spaces:

  • :set expandtab disables tabs
  • :set noexpandtab enables tabs

Search and Replace in Visual block

Select block of text and enter something like this in command mode (probably by pressing :):

:'<,'>s/red/green/g

Prepend multiple lines with anything

  • Ctrl-v to switch to Visual Block mode
  • Shift-i to enter Insert mode compatible with Visual Block mode
  • Type anything and exit insert mode

Rename current file

Warning: check your current working directory first, if it's not the same as your current file's directory path, you will have to provide full/relative path to your current directory. If you add autocmd BufEnter * silent! lcd %:p:h in your .vimrc though you should be always in the file's directory.

  • :saveas NEWFILENAME
  • :!rm -f OLDFILENAME

Save a file you opened without write permission

  • :w !sudo tee %

Run multiple commands

Separate them with |, for example: :set nolist | set nonumber

Juggle with buffers

Assuming you're in NORMAL mode and cursor is currently right before number 5, you can save it in the buffer under name a by typing "ayw, then after moving cursor, let's say, right before number 3 you can save it in the buffer under name b with "byw (or "byt<space> etc.). Then you can multiply them and save result in buffer c by running following command: let @c = @a * @b and paste value of buffer c with "cp in NORMAL mode or with Ctrl + r c in INSERT mode

Run macro only on matching lines

This command will only run macro m on lines the begin with _:

:g/^_/norm @m

Extract all lines with more than 2 commas

Assuming following text:

Foo, Bar, Baz, Qux
Foo, Bar, Baz
Foo, Bar, Baz, Qux
Foo, Bar, Baz, Qux
Foo, Bar, Baz
Foo, Bar, Baz, Qux

and you want to separate lines that have 2 commas from lines that have more than 2 commas. Here is how to do it:

:g/^\([^,]\+,\)\{3,\}/norm "Add

Then go to the end of file or to another file and you can paste the lines with more than 2 commas using:

"Ap

Delete ; character if it appears in the end of the line

Assuming following text:

Foo; bar;
Foo bar
Foo bar;

and need to remove ; in the end of each line where it appears, this command will do it:

:g/;$/norm $x
Clone this wiki locally