tag:blogger.com,1999:blog-66377362520810115832024-03-04T21:16:01.865-08:00Daily Vim: Text Editor Tips, Tricks, Tutorials, and HOWTOsA blog dedicated to text editing and general exploration of computing knowledgeTravis Whittonhttp://www.blogger.com/profile/14592647486468034166noreply@blogger.comBlogger254125tag:blogger.com,1999:blog-6637736252081011583.post-5944034874827819632014-01-21T06:30:00.000-08:002014-01-21T06:30:05.185-08:00Generating Mock UserdataI often find myself needing to generate dummy data when doing software projects. The rig command-line utility makes it easy to do so.
<pre>
# apt-get install rig
$ rig
Maureen Cummings
614 Spring County Blvd
Phoenix, AZ 85026
(602) xxx-xxxx
</pre>Travis Whittonhttp://www.blogger.com/profile/14592647486468034166noreply@blogger.com0tag:blogger.com,1999:blog-6637736252081011583.post-75823438858672423012014-01-14T07:48:00.000-08:002014-01-14T07:48:27.950-08:00Removing Lines that Don't Match a PatternFollowing up on a <a href="http://dailyvim.blogspot.com/2013/12/removing-blank-lines.html">previous tip</a>, you can also use the "g" and "d" commands to remove lines in your current buffer that don't match a specific pattern. The following example would remove all lines that don't match "www.example.com".
<pre>
:g!/www\.example\.com/d
</pre>Travis Whittonhttp://www.blogger.com/profile/14592647486468034166noreply@blogger.com1tag:blogger.com,1999:blog-6637736252081011583.post-37773036184939745062014-01-06T08:13:00.000-08:002014-01-06T08:13:08.669-08:00Finding and Replacing Unicode CharactersIf you'd like to find a particular multibyte character, you can do the following.
<pre>
/\%u001c
</pre>
You can also do a find and replace as you normally would.
<pre>
%s/\%u001c//g
</pre>Travis Whittonhttp://www.blogger.com/profile/14592647486468034166noreply@blogger.com1tag:blogger.com,1999:blog-6637736252081011583.post-62807941847347123392013-12-19T08:02:00.001-08:002013-12-19T08:02:37.967-08:00Removing Blank LinesHere's a handy one-liner to remove blank lines from a file.
<pre>
:g/^$/d
</pre>
Breaking this down. The "g" will execute a command on lines that match a regular expression. The regular expression matches blank lines, and the command is "d" (delete).Travis Whittonhttp://www.blogger.com/profile/14592647486468034166noreply@blogger.com1tag:blogger.com,1999:blog-6637736252081011583.post-3603499134993931402013-12-13T09:32:00.000-08:002013-12-13T09:32:28.900-08:00Vim.jsI'm not sure what utility this provides, but it's a pretty darn cool hack. Kudos to Lu Wang for this Javascript port of Vim.
<a href="http://coolwanglu.github.io/vim.js/web/vim.html">http://coolwanglu.github.io/vim.js/web/vim.html</a>Travis Whittonhttp://www.blogger.com/profile/14592647486468034166noreply@blogger.com0tag:blogger.com,1999:blog-6637736252081011583.post-12277340106049563862013-11-11T12:00:00.000-08:002013-11-11T12:00:00.201-08:00SnipmateIf you like Textmate Snippits, consider installing Snipmate. From the <a href="https://github.com/garbas/vim-snipmate">Github page</a>.<br><br>
SnipMate aims to provide support for textual snippets, similar to TextMate or other Vim plugins like UltiSnips. For example, in C, typing for<tab> could be expanded to:
<pre>
for (i = 0; i < count; i++) {
/* code */
}
</pre>
The project linked above appears to be a fork of the original plugin, but it's up to date, and it works!Travis Whittonhttp://www.blogger.com/profile/14592647486468034166noreply@blogger.com3tag:blogger.com,1999:blog-6637736252081011583.post-56635124654688320432013-11-08T12:00:00.000-08:002013-11-08T12:00:01.956-08:00SupertabIf you like Bash auto-completion, you'll love Supertab. Installation and configuration details are available via its <a href="https://github.com/ervandew/supertab">Github Page</a>.Travis Whittonhttp://www.blogger.com/profile/14592647486468034166noreply@blogger.com0tag:blogger.com,1999:blog-6637736252081011583.post-9306184953846226792013-11-06T12:00:00.000-08:002013-11-08T17:36:29.008-08:00Vim GitgutterVim Gitgutter <a href="https://github.com/airblade/vim-gitgutter">Vim Gitgutter</a> shows a diff in the sign (left-hand) column of your editing window. This is a really cool way to see what portions of a file have changed without resorting to git diff.<br><br>
<div class="separator" style="clear: both; text-align: center;"><a href="https://raw.github.com/airblade/vim-gitgutter/master/screenshot.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" src="https://raw.github.com/airblade/vim-gitgutter/master/screenshot.png" /></a></div>
<br><br><br><br><br><br>
<br><br><br><br><br><br>
<br><br>
Edit:<br>
Droggl asked how to disable vim-gutter when viewing log files because of slowness. I've come up with the following solution which seems like it should work.
<br><br>
Assuming your log files end in *.log, add the following to your ~/.vimrc.
<pre>
autocmd BufNewFile,BufReadPost *.log setlocal filetype=log
filetype plugin on
</pre>
Then make an entry like so in ~/.vim/ftplugin/log.vim
<pre>
:GitGutterDisable
</pre>
Hope that helps!Travis Whittonhttp://www.blogger.com/profile/14592647486468034166noreply@blogger.com2tag:blogger.com,1999:blog-6637736252081011583.post-53053218747490978022013-11-06T10:14:00.000-08:002013-11-06T10:14:37.979-08:00Basic RecoveryThe following commands are useful for recovering an editing session.
<br><br>
Retrieve a list of recoverable files.
<pre>
vim -r
</pre>
Recover a specific file.
<pre>
vim -r .file.extension.swp (from swap files listed above)
</pre>
Recover an unnamed (unsaved) file.
<pre>
vim -r .swp
</pre>
Or from inside of Vim:
<pre>
:recover .file.extension.swp
</pre>
Travis Whittonhttp://www.blogger.com/profile/14592647486468034166noreply@blogger.com0tag:blogger.com,1999:blog-6637736252081011583.post-8057994229454281692013-11-05T06:00:00.000-08:002013-11-06T10:19:27.973-08:00Watching Memcached Traffic with TCPDumpHere's a fun little one-liner I just hacked together to keep tabs on the get/set commands coming in on a memcached server that I administer.
<pre>
sudo tcpdump -i eth0 -s 65535 -A -ttt port 11211| cut -c 9- | grep -i '^get\|set'
</pre>
It'd be easy to feed the output of this command into another script to aggregate key summary data over a given sample period.Travis Whittonhttp://www.blogger.com/profile/14592647486468034166noreply@blogger.com0tag:blogger.com,1999:blog-6637736252081011583.post-44700066501992032292013-11-04T07:36:00.000-08:002013-11-06T10:18:59.300-08:00Wrap Git With FugitiveFrom the <a href="https://github.com/tpope/vim-fugitive">Fugitive</a> Github page:
<br><br>
<i>
I'm not going to lie to you; fugitive.vim may very well be the best Git wrapper of all time. Check out these features:
<br><br>
View any blob, tree, commit, or tag in the repository with :Gedit (and :Gsplit, :Gvsplit, :Gtabedit, ...). Edit a file in the index and write to it to stage the changes. Use :Gdiff to bring up the staged version of the file side by side with the working tree version and use Vim's diff handling capabilities to stage a subset of the file's changes.
</i>
<br><br>
Quite simply, Fugitive provides a ton of awesome hooks for integrating Vim with Git's best features. Check it out!Travis Whittonhttp://www.blogger.com/profile/14592647486468034166noreply@blogger.com0tag:blogger.com,1999:blog-6637736252081011583.post-86612602673972398982013-10-31T06:31:00.000-07:002013-11-06T12:37:57.009-08:00Cheap Process Monitoring: Echo, Watch, and NetcatI frequently work with memcached, gearman, and a variety of other services that allow you to retrieve useful stats by connecting via telnet and issuing a status command. In a lot of cases, retrieving these stats in realtime is beneficial for debugging and diagnosing data pipeline issues.
<br><br>
Fortunately, a few basic tools found on almost any Linux system can make this easy.
<br><br>
The watch utility runs whatever command you provide it at the interval you specify. Here's the world's cheapest realtime clock.
<pre>
$ watch -n 1 'date'
</pre>
Netcat is too versatile to cover in depth in this post, but one of its most basic capabilities is accepting data via standard input, sending that data to a host / port combination of your choosing, and then dumping the result to standard output.
<pre>
$ printf "HEAD / HTTP/1.0\r\n\r\n" | nc google.com 80 | grep Server
Server: gws
</pre>
The above command takes the HTTP HEAD request from printf, sends it to netcat, which then passes it on to google.com on port 80. The results are printed to standard output, and the grep command limits the result output to the server returned (in this case gws).
<br><br>
Combining these two commands provides a ton of utility. Here are two useful examples that I use regularly.
<br><br>
1) Monitoring Gearman queues in realtime
<pre>
watch -n 1 '(echo status; sleep 0.1) | nc localhost 4730'
publishMessage 4 0 1
consumeMessage 7 0 8
archiveMessage 28 0 12
.
</pre>
2) Monitoring memcached get/set commands in realtime
<pre>
watch -n 1 '(echo stats; sleep 0.1) | nc localhost 11211 | grep cmd'
STAT cmd_get 506
STAT cmd_set 8
STAT cmd_flush 0
STAT cmd_touch 0
STAT auth_cmds 0
</pre>
Travis Whittonhttp://www.blogger.com/profile/14592647486468034166noreply@blogger.com0tag:blogger.com,1999:blog-6637736252081011583.post-31830943387856026312013-10-30T12:00:00.000-07:002013-11-06T10:18:19.898-08:00Sensible Defaults with Vim-SensibleIf you have a fresh Vim install, and you'd like a sensible set of defaults, consider installing the <a href="https://github.com/tpope/vim-sensible">vim-sensible</a> plugin. If you've already installed <a href="https://github.com/tpope/vim-pathogen">Pathogen</a>, installing the plugin can be accomplished as follows.
<pre>
cd ~/.vim/bundle
git clone git://github.com/tpope/vim-sensible.git
</pre>Travis Whittonhttp://www.blogger.com/profile/14592647486468034166noreply@blogger.com0tag:blogger.com,1999:blog-6637736252081011583.post-29650385765317348882013-10-29T11:35:00.000-07:002013-11-06T10:18:08.608-08:00Sort: Human NumericThe sort command can accept input data formatted in human readable form. The du -h command is a longtime favorite for finding file sizes in a format that's easy to read with the naked eye. Coupled with sort, you can easily find and display the largest files in a directory tree.
<pre>
$ du -h | sort -r --human-numeric | head
</pre>
Update: I should have mentioned that --human-numeric is a recent addition to GNU coreutils, so this won't work on OS X or older distros. Here's a more generic solution (borrowed from <a href="http://www.cyberciti.biz/faq/how-do-i-find-the-largest-filesdirectories-on-a-linuxunixbsd-filesystem/">here)</a>.
<pre>
for i in G M K; do du -ah | grep [0-9]$i | sort -nr -k 1; done | head -n 11
</pre>Travis Whittonhttp://www.blogger.com/profile/14592647486468034166noreply@blogger.com6tag:blogger.com,1999:blog-6637736252081011583.post-35862078852057078252013-10-28T12:00:00.000-07:002013-11-06T10:17:53.813-08:00Pathogen Makes Plugin Management EasyI have been using <a href="https://github.com/tpope/vim-pathogen/">Pathogen</a> for installing new plugins whenever possible for quite a while now. In my opinion, Pathogen is the simplest path to extending your existing Vim runtime with additional functionality.
<br />
<br />
You can install Pathogen with the following command (assuming you're using a Linux operating system).
<br />
<br />
<pre>
mkdir -p ~/.vim/autoload ~/.vim/bundle; \
curl -Sso ~/.vim/autoload/pathogen.vim \
https://raw.github.com/tpope/vim-pathogen/master/autoload/pathogen.vim
</pre>
Once Pathogen is installed, enable it by adding the following to your .vimrc.
<pre>
execute pathogen#infect()
</pre>
Once Pathogen is enabled, you can install a wealth of plugins by simply cloning them to your ~/.vim/bundle directory. As an example, you can install the <a href="https://github.com/tpope/vim-sensible">vim-sensible</a> plugin as follows.
<pre>
cd ~/.vim/bundle
git clone git://github.com/tpope/vim-sensible.git
</pre>Travis Whittonhttp://www.blogger.com/profile/14592647486468034166noreply@blogger.com0tag:blogger.com,1999:blog-6637736252081011583.post-3204677068070737482013-10-25T12:00:00.000-07:002013-11-06T10:17:35.692-08:00The Wonderful "F" KeyIf you press the "f" key in normal mode, Vim will move the cursor forward to whatever character you input after "f" is pressed. As an example, consider the following line:
<br><br>
a quick brown fox
<br><br>
If the cursor was at the absolute beginning of the line, and you pressed "fb" in normal mode, Vim would move the cursor so that it was positioned over the "b" in "brown".
<br><br>
If you press "F", Vim will move the cursor backwards instead of forward. Given the previous sentence, if pressed "Fq", and the cursor was at the end of the line, it would move to the "q" in "quick". Travis Whittonhttp://www.blogger.com/profile/14592647486468034166noreply@blogger.com1tag:blogger.com,1999:blog-6637736252081011583.post-11951497282502167692013-10-24T18:15:00.000-07:002013-11-06T10:17:20.151-08:00Back From The DeadAfter an almost four year hiatus, I've decided to bring this blog back from the dead. Looking over the last decade, my career as a programmer has been an interesting and challenging endeavor, and over that time period, Vim has been a constant in my professional toolbox. There are countless work days where Vim has saved me hours of pushing bytes around the screen, and a lesser editor would have fallen short of the task.
<br><br>
With this in mind, I feel that I still have a good deal of useful information to share. With the advent of Github and other powerful open source tools, the landscape of the hacker community has evolved. The tools are better, and with reasonable motivation, it feels like any problem is solvable.
<br><br>
In the midst of all of this change, I still believe that Vim is the best tool for the admittedly rote task of munging source code into the best state one can manage given the time constraints and project demands that many of us face.
<br><br>
In lieu of my longstanding love for this text editor (and Linux tools in general), I will resume posting tips to this blog (as they come to mind) in hopes that they can on occasionally help all of you make your hardest problems a little easier and your easy problems a little more fun. Happy Vimming! -TravisTravis Whittonhttp://www.blogger.com/profile/14592647486468034166noreply@blogger.com12tag:blogger.com,1999:blog-6637736252081011583.post-13739613973391634092009-12-09T05:58:00.000-08:002009-12-09T11:31:20.000-08:00Vimgrep TipsI've mentioned vimgrep <a href="http://dailyvim.blogspot.com/2007/12/quickfix.html">in a previous post</a>, but I neglected to mention a few useful flags that can be used in conjunction with it.<br /><br />If you apply the 'g' flag to your vimgrep, it will return all matches instead of just one match per line.<br /><br />:vimgrep /foo/g **/*<br /><br />If you apply the 'j' flag, Vim will not automatically jump to the first match.<br /><br />:vimgrep /foo/j **/*<br /><br />Thanks to Chanel for pointing these out.Travis Whittonhttp://www.blogger.com/profile/14592647486468034166noreply@blogger.com7tag:blogger.com,1999:blog-6637736252081011583.post-28745258942436021692009-12-03T06:38:00.000-08:002009-12-03T06:42:24.532-08:00More on Text StatesA few people have asked me for more information on text states. An anonymous reader contributed the following in the comments of my previous post on the topic.<br /><br />Using g+ and g- is very different than using u and ^r.<br /><br />Try following:<br />* Create new file<br />* (in normal mode) Type iOne - Esc<br />* Type oTwo - Esc<br />* Type oThree - Esc<br />* Type oFour - Esc<br />* Type oFive - Esc<br />* Type 2u<br />* Type oSix - Esc<br />* Type oSeven - Esc<br /><br />Now you have an undo tree with 2 branches. Typing u only goes up the last branch. Using g- goes up by time - branch doesn't matter here.<br /><br />Have a look in :help usr_32.txt for good explanation of using the undo tree.<br /><br />Thanks to whomever contributed the tip!Travis Whittonhttp://www.blogger.com/profile/14592647486468034166noreply@blogger.com4tag:blogger.com,1999:blog-6637736252081011583.post-72440341627173613752009-12-02T05:12:00.000-08:002009-12-02T05:12:00.167-08:00Indent From Normal ModeFrom normal mode, pressing == will indent the current line.Travis Whittonhttp://www.blogger.com/profile/14592647486468034166noreply@blogger.com3tag:blogger.com,1999:blog-6637736252081011583.post-79044287778060183282009-12-01T06:55:00.000-08:002009-12-01T06:57:04.822-08:00Starting on a Specific LineAn anonymous reader writes:<br /><br />You can open a file on the command line and automatically put the cursor on the last line by typing:<br /><br />vim + file<br /><br />If you want vim to start at a specific line you can do the following instead:<br /><br />vim +LINENUMBER file<br /><br />Thanks for the tip!Travis Whittonhttp://www.blogger.com/profile/14592647486468034166noreply@blogger.com3tag:blogger.com,1999:blog-6637736252081011583.post-48081377620459984232009-11-30T06:21:00.000-08:002009-11-30T06:23:23.842-08:00Text StatesIf you're using Vim 7+, you can use g- and g+ to go between text states. For a more advanced usage see the :help earlier and :help later.Travis Whittonhttp://www.blogger.com/profile/14592647486468034166noreply@blogger.com4tag:blogger.com,1999:blog-6637736252081011583.post-5759892714902818032009-11-24T06:11:00.000-08:002009-11-24T17:12:52.400-08:00Jumping to a BufferIf you have split windows open with multiple buffers, you can jump directly to a specific buffer number by doing the following:<br /><br />N<C-w><C-w><br /><br />N is the window number you want to move the cursor to.<br /><br />Thanks to Duff for the coffee!Travis Whittonhttp://www.blogger.com/profile/14592647486468034166noreply@blogger.com3tag:blogger.com,1999:blog-6637736252081011583.post-43946507106245010272009-11-23T06:52:00.000-08:002009-11-23T06:55:57.293-08:00Indent From InsertIn the comments of my last post, graywh left a great tip that I didn't know about. If you're working in insert mode, you can change the indent level of the current line using <C-t> and <C-d>. These commands work no matter where your cursor is positioned on the current line and adjust the indent level based on your shiftwidth setting.Travis Whittonhttp://www.blogger.com/profile/14592647486468034166noreply@blogger.com4tag:blogger.com,1999:blog-6637736252081011583.post-18241556239327355462009-11-19T06:04:00.000-08:002009-11-19T08:44:50.672-08:00Shift-Tab'ingI have the following in my vimrc:<br /><br />set sts=4<br />set et<br /><br />This allows me to take advantage of the convenience of the tab key when editing while actually inserting spaces into the current buffer. Intuitively I've always wished I could shift-tab to unindent the current line one tab stop. After a minute of fiddling with my vimrc, I found a solution.<br /><br />imap <S-Tab> <C-o><<<br /><br />I should also mention that << and >> shifts the provided text based on your shiftwidth setting.Travis Whittonhttp://www.blogger.com/profile/14592647486468034166noreply@blogger.com9