Skip to content

Instantly share code, notes, and snippets.

@umidjons
Last active April 26, 2020 07:15
Show Gist options
  • Select an option

  • Save umidjons/11137734 to your computer and use it in GitHub Desktop.

Select an option

Save umidjons/11137734 to your computer and use it in GitHub Desktop.
Vim Tips

Set vim as default editor

Add vim into alternatives list

# /usr/bin/vim is a link, /usr/bin/vim73 is a executable, 100 priority
update-alternatives --install /usr/bin/vim editor /usr/bin/vim73 100

# change default editor
update-alternatives --config editor

Reload configuration (vimrc) without closing Vim

:source $MYVIMRC<CR>

Motion

H window top

M window middle

L window bottom

:157<CR>, 157G, 157gg go to 157 line

Search and replace step by step

Move to the word you want to search, then press *. Or manually search for some word by typing /{word}.

Move between found words by hitting n or N.

To replace word type cw, then type new word, then <Esc>.

Move to next occurence, then press . to repeat last change.

Uppercase/Lowercase

~ uppercase/lowercase a character

g~w uppercase/lowercase word from cursor until end of a word

g~aw uppercase/lowercase a word in any position

gUaw uppercase a word

guaw lowercase a word

gUU uppercase a line

guu lowercase a line

gUap guap uppercase/lowercase a paragraph

Autocomplete

Type <C-n><C-p> or <C-p><C-n> to open auto complete popup, then continue typing, popup items will be filtered as you type.

To choose from list just type <C-n> or <C-p> following by <C-y> or <CR>.

<C-x><C-n> complete from buffer.

<C-x><C-i> complete from included files.

Autocomplete for PHP

cd ~/.vim/bundle
git clone git://github.com/shawncplus/phpcomplete.vim.git

Installing exuberant-ctags on ubuntu

sudo apt-get install exuberant-ctags

Generating tags:

# change dir
cd my_project_root_dir_OR_my_framework_root_dir
# generate tags
ctags-exuberant -f php.tags --languages=PHP -R
# tell vim to use our tags file
:set tags=~/path/to/php.tags

Open up a new PHP file, set the tag file and start typing the name of a class that you know exists. After a few characters type <c-x><c-o> (insert mode) and the omnicompletion will kick in. Then, on the completed class name in command mode, type <c-]>. You will be taken to the class definition, or given a list of definitions if it matches more than one.

Another neat feature is jumping to a tag from the Vim command line. For example, if you know you need to get to the file containing the class MyLovelyClass, just type:

:tag MyLovelyClass

And hit return. You will be taken straight to it. This is almost always quicker than trying to navigate a file tree to find the file you want. Also, this supports tab completion, so you could just type :tag My<tab> and you would get a list of options.

SuperTab

Installing supertab to open omni via Tab instead of <c-n><c-o>

git clone https://github.com/ervandew/supertab.git

Set default completion type in your .vimrc:

let g:SuperTabDefaultCompletionType="<c-x><c-o>"

Open your source file in vim, type some characters, then hit Tab. To navigate in omni type Tab some more time.

Correction in Insert mode

<C-h> removes one character (backspace)

<C-w> removes one word back

<C-u> removes one line back

Rebuild documentation/How to add plugin documentation

:helptags c:\Program\ Files\ (x86)\Vim\vimfiles\bundle\vimtodo\doc\

Change directory in Vim

:cd mydir or to change disk drive on Windows :cd c:\.

List files & directories

Type :e, then space, then hit CTRL-D to list files & directories in the current directory.

Vim File Explorer

Open File Explorer vertically splitting window: :Vex or with :Vexplore or with :Explore!.

Open File Explorer with :Ex or with :Explore.

Open File Explorer horizontally splitting window: :Sexplore.

Open File Explorer in new tab with :Texplore.

Walk between windows with CTRL-w.

Enter to open file or directory. CTRL-6 to return to File Explorer.

Change netrw window size with :let g:netrw_winsize=25.

Enable netrw file preview :let g:netrw_preview=1.

Preview file under cursor, type p.

Close preview window CTRL-w-z.

Syntax checking

Syntastic

It will always check syntax on every write.

:SyntasticInfo show info

:SyntasticCheck php run php checker

Effectively open a file

To efficiently open a file in the current directory or subdirectories do following:

Set path variable in your vimrc:

set path+=**

Change current directory to your project path:

:cd c:\openserver\domains\mysite.loc\

Search and open a file:

:find MyController.php
:fin MyController.php

Search and open a file in new tab:

:tabfind MyController.php
:tabf MyController.php
:tabf myfolder\MyController.php

To autocomplete file name, for example, type :tabf myfolder\ind then hit <TAB>, Vim will search files and if found autocompletes file name with relative path.

Invoke normal mode command with other language keyboards (langmap option)

Russian

:set langmap=ФИСВУАПРШОЛДЬТЩЗЙКЫЕГМЦЧНЯ;ABCDEFGHIJKLMNOPQRSTUVWXYZ,фисвуапршолдьтщзйкыегмцчня;abcdefghijklmnopqrstuvwxyz

Uzbek

:set langmap=ФИСВУАПРШОЛДЬТЎЗЙКҚЕГМЦЧНЯ;ABCDEFGHIJKLMNOPQRSTUVWXYZ,фисвуапршолдьтўзйкқегмцчня;abcdefghijklmnopqrstuvwxyz

Visual Block mode

Suppose, we have following code:

var a='hello world'
var b=123456
var c=null

To put semicolon (any character or text, no matter) at the end of lines, do following steps:

  • gg to go to the begin of a file
  • Switch to visual block mode with <C-v>
  • Select lines moving down with j
  • Go to end of line with $
  • Switch to append mode with A (or insert mode with I)
  • Type ;, then <Esc>
  • Now Vim puts semicolon at the end of all selected lines
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment