You are not logged in.

#1 2009-10-29 13:24:52

SimonPhotography
Member
From: Arkansas, US
Registered: 2008-04-03
Posts: 24
Website

Simple but effective vim tricks

I've been using Vim for about a year now, but I know that I'm not using it to its full potential.  I've finally gotten used to the "HKJL" movement within files, and I have my .vimrc set to handle autoindents and tabs how I want, but that's about it.

So - I'm looking for a few simple things that I can do to make my editor work better for me.  I've been considering swapping my Esc and CapsLock for a while now, and I think that would be a good start, but I have no idea how to do it.

I'm serious about learning to use Vim (and Linux) effectively in my work, which is web development (usually Python/Django, though I have to drop back to PHP to work on legacy stuff from time to time).  I've settled on ScrotWM for right now, but love my tiling WMs.  I just feel like a gimp because I can't do magic with my editor, and I don't know anyone else in my area that uses Linux seriously.

Last edited by SimonPhotography (2009-10-29 13:25:29)

Offline

#2 2009-10-29 14:12:52

lang2
Member
Registered: 2006-02-10
Posts: 386

Re: Simple but effective vim tricks

Just keep doing :help within Vim.

And read this link: http://rayninfo.co.uk/vimtips.html

Offline

#3 2009-10-29 14:20:53

ArchArael
Member
Registered: 2005-06-14
Posts: 504

Re: Simple but effective vim tricks

I have mapped Esc to CapsLock. This is my .Xmodmap file:

remove Lock = Caps_Lock
keysym Caps_Lock = Escape

The only problem is that this works in X but not in virtual consoles. In virtual consoles I still have to use Esc key. Not so big problem as I use X all the time. wink

Offline

#4 2009-10-29 14:34:48

SimonPhotography
Member
From: Arkansas, US
Registered: 2008-04-03
Posts: 24
Website

Re: Simple but effective vim tricks

ArchArael wrote:

I have mapped Esc to CapsLock. This is my .Xmodmap file:

remove Lock = Caps_Lock
keysym Caps_Lock = Escape

The only problem is that this works in X but not in virtual consoles. In virtual consoles I still have to use Esc key. Not so big problem as I use X all the time. wink

Thanks!  I'll take care of this as soon as I open my laptop next smile  I hate using Windows at work, but I have no choice sad

Offline

#5 2009-10-29 14:41:52

artoj
Member
From: Finland
Registered: 2009-10-27
Posts: 4

Re: Simple but effective vim tricks

I suggest watching Bram Moolenaar's (the developer of Vim) presentation on effective text editing.

Basically it boils down to:

1. Know when you're being inefficient
2. Find a more efficient way to do that
3. Make it a habit

Last edited by artoj (2009-10-29 14:42:31)

Offline

#6 2009-10-29 14:43:19

timetrap
Member
From: Here and There
Registered: 2008-06-05
Posts: 342
Website

Re: Simple but effective vim tricks

Master the find and replace:

:%s/<find>/<replace>/g

It's just like sed.

(I am so going to watch that text editing video.)

Last edited by timetrap (2009-10-29 14:44:44)

Offline

#7 2009-10-29 15:31:55

keenerd
Package Maintainer (PM)
Registered: 2007-02-22
Posts: 647
Website

Re: Simple but effective vim tricks

ArchArael wrote:

I have mapped Esc to CapsLock.  The only problem is that this works in X but not in virtual consoles. In virtual consoles I still have to use Esc key. Not so big problem as I use X all the time. wink

/etc/rc.conf:
KEYMAP="custom"

/usr/share/kbd/keymaps/i386/qwerty/custom.map.gz
... standard keycodes snipped ...
keycode  58 = Escape

Though I have it mapped to "Control", to make screen easier.

Offline

#8 2009-10-29 18:25:55

abijr
Member
Registered: 2008-05-18
Posts: 71

Re: Simple but effective vim tricks

Or to remove complications you can just do:
Ctrl-[
To get Escape.

Offline

#9 2009-10-29 18:49:04

portix
Member
Registered: 2009-01-13
Posts: 757

Re: Simple but effective vim tricks

ArchArael wrote:

The only problem is that this works in X but not in virtual consoles. In virtual consoles I still have to use Esc key. Not so big problem as I use X all the time. wink

You can put

echo keycode 58 = Escape | loadkeys -

into /etc/rc.local.

Offline

#10 2009-10-29 19:54:44

tlvb
Member
From: Sweden
Registered: 2008-10-06
Posts: 297
Website

Re: Simple but effective vim tricks

Or do it the fun way!:

for i in $(find /usr/share/kbd/keymaps/ -name $(sed -n "/^KEYMAP/{s/.*\"\\(.*\\)\".*/\\1.map.gz/;p}" /etc/rc.conf)); do gunzip -c <$i| sed "s/Escape/Caps_Lock/;t;s/Caps_Lock/Escape/"|gzip - > $(echo $i|sed "s/.map.gz$/-capsesc.map.gz/") ; done

(fetches your rc.conf set keymap switches caps lock and escape and saves it in the same place as the other, but instead of with x.map.gz it sets x-capsesc.map.gz as name...)
Edit in case it's not obvious: You'll need write permissions whereever it wants to save the file, yes it normally means root, no I will not take any responsibility, but of course I've run it on my own computer without detecting any probl
20:59 tlvb [n=leo@c-83-256-146-293.cust.bredband2.com] has quit [Read error: 110 (Connection timed out)]

Last edited by tlvb (2009-10-29 20:03:54)


I need a sorted list of all random numbers, so that I can retrieve a suitable one later with a binary search instead of having to iterate through the generation process every time.

Offline

#11 2009-10-29 20:32:03

linkmaster03
Member
Registered: 2008-12-27
Posts: 269

Re: Simple but effective vim tricks

I personally map Caps Lock to Backspace which I think you would like more than if you mapped it to Escape. One thing you will love is this though:

map ; :

No more shift+; to enter commands!

Last edited by linkmaster03 (2009-10-29 20:32:19)

Offline

#12 2009-10-29 20:32:12

skottish
Forum Fellow
From: Here
Registered: 2006-06-16
Posts: 7,942

Re: Simple but effective vim tricks

The greatest thing ever. Maps : to ; in normal mode:

map ; :

Offline

#13 2009-10-29 21:16:17

Lich
Member
Registered: 2009-09-13
Posts: 437

Re: Simple but effective vim tricks

SimonPhotography wrote:

I've finally gotten used to the "HKJL" movement within files

Needs moar NetHack!

Apply settings only for gVim

"GUI Options
if has("gui_running")
set gfn=Monospace\ 12
set guioptions-=rTm
colorscheme zenburn
set showtabline=2
endif

This way I can use a colorscheme that looks fine under the terminal, and one that looks fine under X, depending on my mood.
Some nice text "wrapping"

set wrap nowrap
set sidescroll=3
set listchars+=precedes:<,extends:>

Last edited by Lich (2009-10-29 21:30:44)


Archlinux | ratpoison + evilwm | urxvtc | tmux

Offline

#14 2009-10-29 22:17:39

Yannick_LM
Member
Registered: 2008-12-22
Posts: 142

Re: Simple but effective vim tricks

Registers are nice:

CTR+R %   will insert file name in insert mode
:let @/="" 

use g* then
:grep <CTRL-R /> *   to look for the word under the cursor, then grep it it all files
:copen                     to see the results

(using * on word foo will put \<foo\> on the search register, which is less useful)

quick-fix mode is nice:
:copen, :cn, and so on

nice plugins:
A.vim , supertab, ...

CTRL-Y in insert mode insert the same character that are on the above line.

CTR-V in normal mode to edit rectangle of text:

foo(bar, baz);
foo(42.  21);

press CTR-V then j then e then c then fun then escape (twice), and you'll have:

fun(bar, baz);
fun(42.  21);

CTRL-A / CTRL-Z in insert mode to increment / decrement the number under the cursor.
Very useful combined with macros.

Offline

#15 2009-10-29 22:26:13

Gigamo
Member
Registered: 2008-01-19
Posts: 394

Re: Simple but effective vim tricks

You might find some at http://dailyvim.blogspot.com. Nice blog imo.

Offline

#16 2009-10-29 23:08:45

Pox
Member
From: Melbourne, AU
Registered: 2007-10-04
Posts: 66

Re: Simple but effective vim tricks

skottish wrote:

The greatest thing ever. Maps : to ; in normal mode:

map ; :

Dear FSM. How did I not think of this before?

Offline

#17 2009-10-29 23:24:58

gog
Member
Registered: 2009-10-13
Posts: 103

Re: Simple but effective vim tricks

press qq before doing anything you're going to repeat

alt+key in insert mode means key in normal mode, faster than pressing esc

Offline

#18 2009-10-30 09:43:33

ArchArael
Member
Registered: 2005-06-14
Posts: 504

Re: Simple but effective vim tricks

keenerd wrote:
ArchArael wrote:

I have mapped Esc to CapsLock.  The only problem is that this works in X but not in virtual consoles. In virtual consoles I still have to use Esc key. Not so big problem as I use X all the time. wink

/etc/rc.conf:
KEYMAP="custom"

/usr/share/kbd/keymaps/i386/qwerty/custom.map.gz
... standard keycodes snipped ...
keycode  58 = Escape

Though I have it mapped to "Control", to make screen easier.

portix wrote:
ArchArael wrote:

The only problem is that this works in X but not in virtual consoles. In virtual consoles I still have to use Esc key. Not so big problem as I use X all the time. wink

You can put

echo keycode 58 = Escape | loadkeys -

into /etc/rc.local.

Thank you for your tips guys.

Offline

#19 2009-10-30 15:46:45

Mr.Elendig
#archlinux@freenode channel op
From: The intertubes
Registered: 2004-11-07
Posts: 4,092

Re: Simple but effective vim tricks

:h object-select

Strangely, a lot of people doesn't know about these.


Evil #archlinux@libera.chat channel op and general support dude.
. files on github, Screenshots, Random pics and the rest

Offline

#20 2009-10-30 15:48:55

skottish
Forum Fellow
From: Here
Registered: 2006-06-16
Posts: 7,942

Re: Simple but effective vim tricks

Pox wrote:
skottish wrote:

The greatest thing ever. Maps : to ; in normal mode:

map ; :

Dear FSM. How did I not think of this before?

Repeating what was in the post before me doesn't make me look smart? Damn!

*** Beaten by nine seconds ***

Offline

#21 2009-10-30 16:08:05

GGLucas
Member
Registered: 2008-03-13
Posts: 113

Re: Simple but effective vim tricks

skottish wrote:

The greatest thing ever. Maps : to ; in normal mode:

map ; :

So what do you use for ; ? The ";" key is very useful for efficient editing, it repeats the last f or t command.

If you have:

[f]unction(arg1, arg2, arg3)

I'm sure you all know doing f,  will give you:

function(arg1[,] arg2, arg3)

But instead of pressing f, again, you can use ; to repeat your command and get.

function(arg1, arg2[,] arg3)

Very useful (though in this case you could use W or E as well.

Offline

#22 2009-11-02 23:08:11

rson451
Member
From: Annapolis, MD USA
Registered: 2007-04-15
Posts: 1,233
Website

Re: Simple but effective vim tricks

I second MrElendig on :help text-objects/object-select, but I'd like to add g;/g, and friends.  Very useful to navigate somewhere and hit g; to go back where I was without having to set and remember marks all the time.


archlinux - please read this and this — twice — then ask questions.
--
http://rsontech.net | http://github.com/rson

Offline

#23 2009-11-12 07:34:19

mjunx
Member
From: Chicago
Registered: 2009-11-01
Posts: 17
Website

Re: Simple but effective vim tricks

Reindent a file (in default mode): gg=G
Select all text: ggVG

Also, learn how to use the aw/iw (a word/inner word) et al. modifiers. This way, you could have the cursor anywhere in the word and can do commands like "daw" to delete the whole word, or "caw" to change it, etc.

About the "qq" macro thing: that defines a macro (the first q) called "q" (the second q). You type in all the stuff you would normally do, type "q" again to end it. Then you can use the "@q" command to repeat the "q" macro (or whatever letter you save it in).

Also, one of my favourite visual selectors in vim is the Ctrl+V visual mode. You can select a two-dimensional block of text (similar to the mark text thing in MS cmd.exe) and perform commands on it (doesn't seem to work with :s//, however).

Last edited by mjunx (2009-11-12 07:35:50)

Offline

#24 2009-11-12 15:18:20

steve___
Member
Registered: 2008-02-24
Posts: 452

Re: Simple but effective vim tricks

mjunx wrote:

(doesn't seem to work with :s//, however).

This script adds that functionality.

http://www.vim.org/scripts/script.php?script_id=1195

The syntax is:

B s/ooga/booga/g

Offline

#25 2009-11-13 06:27:52

Pox
Member
From: Melbourne, AU
Registered: 2007-10-04
Posts: 66

Re: Simple but effective vim tricks

GGLucas wrote:
skottish wrote:

The greatest thing ever. Maps : to ; in normal mode:

map ; :

So what do you use for ; ? The ";" key is very useful for efficient editing, it repeats the last f or t command.

Oh, that's very useful as well... dammit. Guess I'll have to press shift every now and again for :, then. tongue

Last edited by Pox (2009-11-13 06:29:00)

Offline

Board footer

Powered by FluxBB