Showing posts with label vim. Show all posts
Showing posts with label vim. Show all posts
Tuesday, October 15, 2013
Tuesday, July 9, 2013
Vim golf: delete back one word and change to end of line
Start with ...
The LookupError
... in a text file with the cursor positioned at orange.
Then use the Vim command TEC (yes, all are caps) to get ...
The LookupE
... which enables easy typing of ...
The LookupExpression
... in a single go.
The secrets are the T motion to go back before the last occurrence of a linewise character and the C command to delete everything to the end of the line and drop automatically into insert mode.
The LookupError
... in a text file with the cursor positioned at orange.
Then use the Vim command TEC (yes, all are caps) to get ...
The LookupE
... which enables easy typing of ...
The LookupExpression
... in a single go.
The secrets are the T motion to go back before the last occurrence of a linewise character and the C command to delete everything to the end of the line and drop automatically into insert mode.
Wednesday, July 3, 2013
Using Vim to grep projectwide
You can use ...
:grep -Ir --exclude=*.svn* foo *
... to grep for foo in all files projectwide. The -I flag tells grep to ignore binary files; the -r flag tells grep to descend directories recursively; the --exclude=*.svn* tells grep to avoid directories with .svn anywhere in the name; foo is the search string; and the terminal * starts grep on the contents working directory.
:grep -Ir --exclude=*.svn* foo *
... to grep for foo in all files projectwide. The -I flag tells grep to ignore binary files; the -r flag tells grep to descend directories recursively; the --exclude=*.svn* tells grep to avoid directories with .svn anywhere in the name; foo is the search string; and the terminal * starts grep on the contents working directory.
Tuesday, July 2, 2013
Using Vim's quickfix with the output of grep
Set makeprg and errorformat to use Vim's quickfix list with a custom external program.
These settings ...
:setlocal makeprg=abj-grp\ READ
:setlocal errorformat=%f:%l:%m
... work when running the custom command abj-grp READ on a tree of the filesystem.
Thursday, June 27, 2013
Using Vim to break long Python import statements
You can use ...
:%s/^\(from.*\)\(import.*[a-z]\)\%>79v/\1\\\r\t\2/g
... to change long lines like ...
from experimental.tools.musicexpressiontools.RhythmRegionExpression import RhythmRegionExpression
:%s/^\(from.*\)\(import.*[a-z]\)\%>79v/\1\\\r\t\2/g
... to change long lines like ...
from experimental.tools.musicexpressiontools.RhythmRegionExpression import RhythmRegionExpression
... to two shorter lines like this ...
from experimental.tools.musicexpressiontools.RhythmRegionExpression \
import RhythmRegionExpression
... in a single command.
Labels:
import statements,
python,
search and replace,
vim
Using Vim to replace line-terminal ReST :: with line-separate ReST :: indicators.
You can transform this ...
def foo(x, y):
r'''Explanation of function::
>>> foo(7, 8)
...
Return something.
'''
...
... to this ...
def foo(x, y):
r'''Explanation of function:
::
>>> foo(7, 8)
...
Return something.
'''
...
... using Vim substitution.
The command ...
:%s/\(\s*\)\(.*\w\)\(::\)$/\1\2:\r\r\1::/g
... will do the trick.
Changing Python list comprehensions to generator comprehensions
The list comprehension ...
assert all([x % 2 == 0 for x in sequence])
assert all([x % 2 == 0 for x in sequence])
... can be rewritten as the generator comprehension ...
assert all(x % 2 == 0 for x in sequence)
... instead.
You can use the Vim substitution :%s/all(\[\(.*\)\])/all(\1)/g to change all([...]) to all(...) everywhere in a file.
Labels:
generator comprehensions,
list comprehensions,
python,
substitution,
vim
Using Vim to yank and put from the clipboard
Use "*2yy in normal mode to yank two lines into the clipboard.
Then use "*p in normal mode to paste the contents of clipboard register.
Under OS X you can use "+2yy and "+p, too. This is because Vim's quotestar and quoteplus registers are both assigned to the clipboard under OS X.
Note that none of the commands here contains a colon because all commands are issued in normal mode.
Then use "*p in normal mode to paste the contents of clipboard register.
Under OS X you can use "+2yy and "+p, too. This is because Vim's quotestar and quoteplus registers are both assigned to the clipboard under OS X.
Note that none of the commands here contains a colon because all commands are issued in normal mode.
Wednesday, June 26, 2013
Using Vim to open all classfiles recursively in a directory tree
Assuming your classfiles are MixedCase, you can use ...
:args **/[A-Z]*.py
... to open all classfiles into Vim. And you can use ...
:args **/[^test]*.py
... to open all files that are not test files. And you can use ...
:args **/[^test_]*.py
... to open all files that are neither test files nor __init__.py files.
And you can use ...
:args **/subpackage/[A-Z]*/[A-Z]*.py
to open all the classfiles in specific subpackage (assuming each classfile is housed in an eponymous directory).
After running any of these commands in Vim, you can use ...
:args
... to see the complete list of files Vim has opened.
:args **/[A-Z]*.py
... to open all classfiles into Vim. And you can use ...
:args **/[^test]*.py
... to open all files that are not test files. And you can use ...
:args **/[^test_]*.py
... to open all files that are neither test files nor __init__.py files.
And you can use ...
:args **/subpackage/[A-Z]*/[A-Z]*.py
to open all the classfiles in specific subpackage (assuming each classfile is housed in an eponymous directory).
After running any of these commands in Vim, you can use ...
:args
... to see the complete list of files Vim has opened.
Friday, June 7, 2013
Using Tim Pope's pathogen to manage vim plugins
Using Tim Pope's pathogen to manage vim plugins is essential.
Tim hosts the project on Git.
Type this ...
That's all there is to it.
Tim hosts the project on Git.
Type this ...
mkdir -p ~/.vim/autoload ~/.vim/bundle; \
curl -Sso ~/.vim/autoload/pathogen.vim \
https://raw.github.com/tpope/vim-pathogen/master/autoload/pathogen.vim
... to install pathogen.That's all there is to it.
(Re)compiling vim from source
Vim sources are hosted at Google code here.
The sources are maintained in a Mercurial repository. So if you want to (re)compile vim from source then you should first make sure a Mercurial client is installed on your machine.
Then type these commands into your shell. Make sure to include the --enable-pythoninterp flag if you want to compile vim with +python attribute set.
$ hg clone https://vim.googlecode.com/hg/ vim
$ cd vim/src
$ ./configure --enable-pythoninterp --with-features=huge --prefix=$HOME/opt/vim
$ make && make install
$ mkdir -p $HOME/bin
$ cd $HOME/bin
$ ln -s $HOME/opt/vim/bin/vim
Then type ...
which vim
... and sure that the shell finds your freshly compiled $HOME/bin/vim.
If the shell finds a different version of vim then you may need to add something like ...
export PATH=$HOME/bin:$PATH
If the shell finds a different version of vim then you may need to add something like ...
export PATH=$HOME/bin:$PATH
... to your .profile.
Finally, type ...
vim --version
... and check that the +python attribute is set.
These instructions are taken verbatim from Martin Brochhaus's presentation on using vim as an IDE. Martin's presentation is available on his github page at https://github.com/mbrochh/vim-as-a-python-ide.
(The whole presentation is recommended. As it Martin's .vimrc file.)
Thursday, June 6, 2013
Running Python commands in vim
Run ...
:py print 2 ** 38
... to print 274877906944 to screen in vim.
The feature works only when vim has been compiled with the the +python option enabled.
Run ...
vim --version
... to see if your install of vim was compiled with the +python option enabled.
Thursday, May 22, 2008
vimrc
The current
.vimrc
looks like this.
set ts=3
set sw=3
set expandtab
set noai
set fileencoding=utf-8
set encoding=utf-8
Vim s-underdot
Vim
accepts Unicode characters in insert mode following CTRL-v
. Prefix Unicode values with u
, as in Python, and enter two byte codes as four character hex. Vim ṣ
is CTRL-v u1e63
in insert mode. Thanks to Mark for the tip.
Vim theta
:digraphs
gives the vim
table of digraphs.And
CTRL^K
followed by h*
gives lowercase theta.
Tuesday, April 22, 2008
Gene's .vimrc and .screenrc
"What're you using for
Then email:
And in
And
.vimrc
and .screenrc
?"Then email:
filetype indent on
filetype plugin on
colorscheme evening
"colorscheme default
source ~/.vim/WordLength.vim
syntax on
set mouse=a
set printoptions=paper:letter,duplex:off,collate:n,syntax:n
set tags=~/.tags
set tabstop=3
set shiftwidth=3
set textwidth=75
set autoindent
set noignorecase
set nobackup
set noexpandtab
set clipboard=unnamed
set grepprg=grep\ -nH\ $*
set foldmethod=indent
WordLength.vim
is a script Gene put together when editing a ton of SQL with fieldnames absolutely limited to 18 characters.
function! WordLength()
let saveLine = line(".")
let savePos = col(".")
execute "normal b"
let startPos = col(".")
execute "normal e"
let endPos = col(".")
if (savePos < endPos) && (savePos > startPos)
let leftCount = endPos-savePos
execute "normal " . leftCount . "h"
elseif savePos == startPos
execute "normal b"
endif
return endPos-startPos+1
endfunction
command! Wl echo WordLength()
nmap ^C :Wl^M
And in
python
:
set expandtab
set wrap
set textwidth=80
set wrapmargin=1
And
.screenrc
:
hardstatus alwayslastline "[%n: %t]%? [%u]%? %h"
vbell on
multiuser on
acladd root
Apple keyboards and .vimrc delete and backspace
From the
"In 3.0 both the delete key and the backspace key worked as a backspace in insert mode; they deleted the character to the left of the cursor. In 4.0 the delete key has a new function: it deletes the character under the cursor, just like it does on the command-line. If the cursor is after the end of the line and 'bs' is set, two lines are joined."
Apply keyboards have a delete key -- two, sometimes, actually -- but no backspace key. So users of Apple keyboards effectively have, by default under
The solution's in the next part of the same helpscreen:
If you prefer backspace and delete in Insert mode to have the old behavior, put this line in your vimrc:
And you may also want to add these, to fix the values for <BS> and <Del>:
(Enter
vim 4.0
docs on backspace and delete:"In 3.0 both the delete key and the backspace key worked as a backspace in insert mode; they deleted the character to the left of the cursor. In 4.0 the delete key has a new function: it deletes the character under the cursor, just like it does on the command-line. If the cursor is after the end of the line and 'bs' is set, two lines are joined."
Apply keyboards have a delete key -- two, sometimes, actually -- but no backspace key. So users of Apple keyboards effectively have, by default under
vim 4.0
, no backspace. If you find yourself pressing delete and repeatedly joining a bunch of lines, that's probably what's going on.The solution's in the next part of the same helpscreen:
If you prefer backspace and delete in Insert mode to have the old behavior, put this line in your vimrc:
inoremap ^? ^H
And you may also want to add these, to fix the values for <BS> and <Del>:
set t_kb=^H
set t_kD=^?
(Enter
^H
with CTRL-V CTRL-H
and ^?
with CTRL-V CTRL-?
or <Del>.)
Subscribe to:
Posts (Atom)