Vim Tips
Copy and Paste
-
copy/delete word under cursor in Vim
yw / byw
-
Assuming that the cursor is at the first character of the word simply do this in command mode:
yw
y is for yank and w is for word.
Other ways of doing the same thing which are not as efficient:
vey
the v starts visual select mode. e tells vim to move to end of word. y yanks or copies the word. to delete replace y with x.
if the cursor is somewhere in the middle of the word, add a b before the command as in:
byw
or
bvey
- I want to know if there is any way by which I can paste yanked text to the command window. For eg. if I have yanked a word and I want to grep it in some location I cant simply paste the word using ‘p’. However if I copy it to clipboard, Shift-Insert will paste the same thing. Is there any tweak available which would allow me to paste yanked text to the vim command prompt? I am using gvim on Windows.
<ctrl+r>"
where “ stands for default register.
Split Layouts
:sp <filename>
split the layout vertically
:vs <filename>
horizontal split
Pro Tip: Make sure to utilize tab completion to find files after typing :sp.
Pro Tip: You can also specify different areas of the screen where the splits should occur by adding the following lines to the .vimrc file:
set splitbelow
set splitright
Pro Tip: Want to move between the splits without using the mouse? Simply add the following to .vimrc and you can jump between splits with just one key combination:
split navigations
nnoremap <C-J> <C-W><C-J>
nnoremap <C-K> <C-W><C-K>
nnoremap <C-L> <C-W><C-L>
nnoremap <C-H> <C-W><C-H>
Buffers
While VIM can do tabs, many prefer buffers and splits. You can think of a buffer as a recently opened file. VIM provides easy access to recent buffers, just type :b
Pro Tip: At the end of the :ls output, VIM will prompt with Hit enter to continue. You can instead type :b
PEP8
To add the proper PEP8 indentation, add the following to your .vimrc:
au BufNewFile,BufRead *.py
\ set tabstop=4
\ set softtabstop=4
\ set shiftwidth=4
\ set textwidth=79
\ set expandtab
\ set autoindent
\ set fileformat=unix
and
Plugin 'vim-scripts/indentpython.vim'
Flagging Unnecessary Whitespace We also want to avoid extraneous whitespace. We can have VIM flag that for us so that it’s easy to spot – and then remove.
au BufRead,BufNewFile *.py,*.pyw,*.c,*.h match BadWhitespace /\s\+$/
Other file:
au BufNewFile,BufRead *.js, *.html, *.css
\ set tabstop=2
\ set softtabstop=2
\ set shiftwidth=2
Syntax Checking/Highlighting
Plugin 'scrooloose/syntastic'
Plugin 'nvie/vim-flake8'
let python_highlight_all=1
syntax on
Saving a read-only file edited in vi / vim
:w !sudo tee %