Vim: Insert a line of characters
I need an 80 character divider of text in Vim. I can never remember the sequence so I'm writing it down:
80i/<ESC>
Will generate:
////////////////////////////////////////////////////////////////////////////////
I need an 80 character divider of text in Vim. I can never remember the sequence so I'm writing it down:
80i/<ESC>
Will generate:
////////////////////////////////////////////////////////////////////////////////
Vim is my code editor of choice as it has great features, excellent syntax highlighting, and a robust plug-in infrastructure. I'm very used to look and feel of Vim. If you want to share some of your code online, converting things to HTML can be problematic. Luckily Vim has a built in code to HTML converter:
:TOhtml
Simply run the :TOhtml
command in an open text file and your buffer will be redrawn in HTML which you can then save. Here is a quick sample of my pcg32.pl script converted to HTML.
If you're editing a file in Vim you can rewind time to a previous version of the file with the earlier command. You can go backwards (or forwards) in the history of a file based on a given time measurement. This can be helpful if you mess up your file and just want to rollback to a previous version.
:earlier 5m
or
:later 5m
Reddit had some interesting discussion on what you can do with this feature.
Note: Alternately you can use :e!
to reload the file from disk if you haven't saved since your mess up.
Vim has it's own internal scripting language called Vimscript, which is complicated and only appropriate in Vim. Most versions of Vim ship with Perl support. I taught myself how to write a simple Vim script in Perl. The following will define a Vim function named CommaToggle, that calls a perl function named comma_toggle. This will toggle spaces after commas on/off.
function! CommaToggle()
perl << EOF
# Get the current line number, and line text
my ($line_num,$column) = $curwin->Cursor();
my $line = $curbuf->Get($line_num);
if ($line =~ /,/) {
my $fixed = comma_toggle($line);
$curbuf->Set($line_num,$fixed);
}
sub comma_toggle {
my $line = shift();
if ($line =~ /, /) {
# Remove spaces after commas
$line =~ s/, /,/g;
} else {
# Add a space after commas
$line =~ s/,/, /g;
}
return $line;
}
EOF
endfunction
Other Vim/Perl commands are available from the documentation. Then you can map a key combination to call that function:
nnoremap <Leader>, :call CommaToggle()<cr>
If you use Vim on any regular basis you've probably created your own custom .vimrc
file, and maybe installed a plugin or two. This config is machine specific and is not the easiest thing to move from one machine to another. I found this cool project called myvim that packages up your entire Vim installation into a single portable file. This file is a self-extracting archive of your Vim config that you can transfer to a new machine.
myvim -j /tmp/vim.bakers
This will create a file /tmp/vim.bakers
which you can transfer and then run on a new machine.
Personally I'm a fan of using tabs for indentation in my code, but not all people feel the same way. I was recently contributing to a project that used four spaces as indentation. This causes a problem because my .vimrc
says to use tabs. This can create some formatting problems obviously. Vim allows you to set simple commands inside the text the file you are editing using modelines. I was able to add:
// vim: tabstop=4 shiftwidth=4 expandtab autoindent softtabstop=4
as the last line of my file and now Vim converts my indentations to spaces (expandtab
) for this one file only.
I wanted to launch Vim with a search string already set. That way I could load my file, and just hit N to jump to that location.
vim myfile.txt +/search_text
Whatever you set search_text to, will pre-fill the search buffer.
Vim has simple built in encryption. If you have a text file you want to encrypt you can run the commands:
:set cryptmethod=blowfish2
:X
Vim will prompt you for a password, and then the next time you save the file it will be encrypted automatically. When you open the file it will prompt you for the password. If you enter the wrong password you'll see a bunch of gibberish.
To make vim do case insensitve searches turn on ignorecase
:set ignorecase
And alternately do smartcase also. Smartcase tries to determine if you're looking for something specifically based on case and searches accordingly. For more info use the vim help :help smartcase
:set smartcase