think tank forum

technology » Vim tips

bluet's avatar
14 years ago
link
bluet
Forking off the vimrc thread.

< and >: Decrease and increase indentation levels. Works with several lines and levels at once.
asemisldkfj's avatar
14 years ago
link
asemisldkfj
the law is no protection
whoa, that is neat.
andre's avatar
14 years ago
link
andre
v: visual selection
ctrl+v: vertical visual selection
bluet's avatar
14 years ago
link
bluet
V: Visual selection by lines.
^V: Visual selection by block (you can go horizontally too).
Carpetsmoker's avatar
14 years ago
link
Carpetsmoker
Martin
Block selection is evil. Windows' cmd window block selection makes me foam at the mouth and want to kill random people.
bluet's avatar
14 years ago
link
bluet
gg=G: Indent all lines in the buffer the way Vim wants them.
lucas's avatar
14 years ago
link
lucas
i ❤ demo
> Windows' cmd window block selection makes me foam at the mouth and want to kill random people.

haha yes!
 
14 years ago
link
arun
keep smiling !
gf - opens the file under the cursor
gq - word wrap (use v to select the lines and then gq. useful when typing a mail)
* - search the word under cursor
bluet's avatar
14 years ago
r1, link
bluet
^Wgf: Open the file under the cursor in a new tab.
gu: Make stuff lowercase.
gU: Make stuff uppercase.
maple's avatar
14 years ago
link
maple
i like large datasets
"these will delete inside the corresponding characters. you can use 'y' or 'c' instead of 'd' to yank or change.
di<
di{
di(
di"

"save with sudo (used so many times)
:w !sudo tee %

"better movments
nnoremap k gk
nnoremap j gj
nnoremap gk k
nnoremap gj j



bluet's avatar
14 years ago
link
bluet
]p: Paste and autoindent.
asemisldkfj's avatar
14 years ago
link
asemisldkfj
the law is no protection
:retab to convert existing tabs into spaces

you can guess why I know this now :)
bluet's avatar
14 years ago
link
bluet
Welcome to the right side! :D
asemisldkfj's avatar
14 years ago
link
asemisldkfj
the law is no protection
> Windows' cmd window block selection makes me foam at the mouth and want to kill random people.

I just realized what you meant by this, and yes, it does the same thing to me.
dannyp's avatar
13 years ago
link
dannyp
dʎuuɐp
http://vimgolf.com/
lucas's avatar
13 years ago
link
lucas
i ❤ demo
haha, that's great
dannyp's avatar
13 years ago
r1, link
dannyp
dʎuuɐp
Wikia's Best Vim Tips
Rayner's Best Vim Tips
Carpetsmoker's avatar
13 years ago
link
Carpetsmoker
Martin
How do you "clear" search?

For example, I typed:
/hello

And I want to clear the pattern?

I now use:
/asdflijwer

And that works ... sort of :-/
dannyp's avatar
13 years ago
link
dannyp
dʎuuɐp
:let @/ = ""

from: http://vimdoc.sourceforge.net/htmldoc/pattern … st-pattern
bluet's avatar
13 years ago
link
bluet
:noh
dannyp's avatar
13 years ago
link
dannyp
dʎuuɐp
you should be on vim golf :)
Carpetsmoker's avatar
13 years ago
link
Carpetsmoker
Martin
Super, two methods to choose!
dannyp's avatar
13 years ago
link
dannyp
dʎuuɐp
was wondering how to evoke the save-as dialog for gvim, and i thought this was cute:


:bro sav


source
dannyp's avatar
13 years ago
r1, link
dannyp
dʎuuɐp
I'm using vim as a productivity tool these days. As such I found out how awesome abbreviations can be, and macros, and commands. Pretty much vi is amazing as an editor.

Often at work I'll have to type out acronyms and their expanded definition. In this case I'd use several abbreviations for insert mode, I have a bunch more than this but it's pretty great for templating and block-completion:


:ia h Hello
:ia dp Daniel Peterson (projectdp@gmail.com)
:ia nc - dp <C-R>=strftime("%c")<CR>
:ia cc Thank you,<CR><CR>Daniel Peterson<CR>PO Box 1744<CR>San Jose, CA 95109
:ia s- ___________________________________________________________________________



To aid in speed-typing I utilize omnicomplete (type word+CTRL-N) and I want to start including a custom definitions file for custom word completion.

Commonly there's addresses I need to look up. The case may be dirty, so I clean up and enter the data. What I do in vi is:

1. paste dirty:
daniel PETERSON
po Box 1744
San jose, ca 95109

2. move to start: kk0
3. change all lines to lower case: gu3u
4. capitalize first characters of each word: 8@u
5. clean up any changes: (move to location i.e. "Po", "Ca") g~w

before you try, you need to record a macro:
to record the macro for this cleanup action:
quw~q

or you can just set it on cmd line:
:let @u='w~'

all together now:
<CTRL+V>kk0gu3u8@ug~wk0e~k0Vjj<CTRL+C>

it may not seem intuitive but if you have the motions down it's not difficult to parse out :D so awesome!
lucas's avatar
13 years ago
link
lucas
i ❤ demo
dp is lovin'! :D
bluet's avatar
13 years ago
r1, link
bluet
Vim 7.3 has persistent undo, so I do this:

if v:version >= 703
    set undodir=~/.vim/undo
    set undofile
    set undolevels=10000
    set undoreload=100000
endif


This saves an undo file for every file I edit in ~/.vim/undo that persists when closing and opening files again.
lucas's avatar
13 years ago
link
lucas
i ❤ demo
so good
asemisldkfj's avatar
13 years ago
link
asemisldkfj
the law is no protection
excellent posts by dp and bluet.
dannyp's avatar
13 years ago
link
dannyp
dʎuuɐp
some tips on use:


:ia cc Thank you,<CR><CR>Daniel Peterson<CR>PO Box 1744<CR>San Jose, CA 95109



if you notice the result (of typing cc[space]) indenting like this:
Thank you,

      Daniel Peterson
      PO Box 1744
      San Jose, CA 95109


you'll want to do ":set nocindent". I thought this was an autoindent issue, but I could only resolve it with nocindent.

todo: incorporate backup feature, come up with a faster way to restore swap files and clean things up after a crash, define and find more useful macros for vim bot work, do more tips!
Carpetsmoker's avatar
13 years ago
link
Carpetsmoker
Martin
:ia

I am loving the command already!

Good tip!
Carpetsmoker's avatar
13 years ago
r1, link
Carpetsmoker
Martin
Persistent undo! *drool*
dannyp's avatar
13 years ago
r4, link
dannyp
dʎuuɐp
...and persistent redo? I didn't see anything in the help. Don't forget about the redo hotkey: (CTRL+R)!

Ok so I have sets of rooms that I regularly need to clean up, similar to addresses. Today I found it so useful to use macros -- I'm getting better!

input:
XAI0-2-DANNYP MASTER KEY (Offline) (4) DCP1
BOH-7-BECKONING SHADOW (Online) (1) OXG2 
LKM1-58-WAN CHAI (Online) (1) RTS4
DGM-0-LAKE SHUDDER (Online) (5) MAM3
RCDP3-0-EMERALDS (Online) (9) GTS1
BLDG04-6-ROOT (Offline) (5) TSP4
MRXAN-1-LOTUS LEAF (Online) (9) GTA1
SKGE1-21-TERRIBLE THINGS (Online) (6) ADP3
HKKG-41-TREBUCHET (Online) (2) RMA1
MRBRO-1-UNGULATE TIPS (Online) (6) CNK5

magic:
@r<CTRL+C>


output:
Dannyp Master Key
Beckoning Shadow
Wan Chai
Lake Shudder
Emeralds
Root
Lotus Leaf
Terrible Things
Trebuchet
Ungulate Tips

behind the magic:
stripping room names:
let @s='0d4w$7bhd$j' 

change case, next word (used in addresses too):
let @u='~w' 

set a mark, strip 10 lines of room junk
    ...jump to mark, lowercase 10 lines, capitalize 18 words
    ...jump to mark, visual select 9 lines down:
let @r='ms10@s`sgu10u18@u`sV9j'
dannyp's avatar
13 years ago
link
dannyp
dʎuuɐp
input:
b is
a this
c awesome

magic:
Vjj!sort


output...
bluet's avatar
13 years ago
r1, link
bluet
Shave off some keypresses:

3:sort


or

:%sort


for the whole buffer.
bluet's avatar
13 years ago
link
bluet
I just found out that

@:


repeats the last :command.
Carpetsmoker's avatar
13 years ago
link
Carpetsmoker
Martin
You can also use @@ to repeat the last macro (Didn't know about the @: though).
Carpetsmoker's avatar
13 years ago
r1, link
Carpetsmoker
Martin
> Vjj!sort

That calls the /usr/bin/sort. Vim also includes a built-in sort :sort
dannyp's avatar
13 years ago
link
dannyp
dʎuuɐp
Learning stuff every day, thanks guys. Turning clay to terracotta. Can you guys just write an awesome new vi guide or is this a rite of passage?
Carpetsmoker's avatar
13 years ago
link
Carpetsmoker
Martin
vimtutor (command) is the best vi guide!
dannyp's avatar
13 years ago
link
dannyp
dʎuuɐp


:Ni!

bluet's avatar
13 years ago
link
bluet
:h nun<CR>/nun<CR>nnn
dannyp's avatar
13 years ago
link
dannyp
dʎuuɐp
Thanks to db:

This is obvious but I couldn't do this properly without this tip:

In Windows, to make Windows and DOS find gvim / vim, just add the path to the PATH environment variable.

Control Panel -> System Properties -> Advanced -> Environment Variables -> System Variables -> PATH

Edit PATH, append:
"; C:\Program Files (x86)\Vim\vim73\"

(or your vim path :P)
Carpetsmoker's avatar
13 years ago
link
Carpetsmoker
Martin
If you run the installer there's an option for that (IIRC it doesn't add to the PATH, but places vim.bat in C:/windows/system32/
asemisldkfj's avatar
13 years ago
link
asemisldkfj
the law is no protection
I really need to write a script that adds every subdirectory of %programfiles% to my PATH. I think I've made this post on ttf before, but it's worth repeating.
Carpetsmoker's avatar
13 years ago
r1, link
Carpetsmoker
Martin
This may work:

move %PROGRAMFILES%\*.exe %SYSTEMROOT%\system32\

;-)
bluet's avatar
13 years ago
link
bluet
-X makes vim start faster in a terminal.
Carpetsmoker's avatar
13 years ago
r1, link
Carpetsmoker
Martin
[~]% time vim -c ':q' 
        0.12 real         0.10 user         0.01 sys

[~]% time vim -X -c ':q' 
        0.12 real         0.10 user         0.01 sys

[~]% time vim -c ':q' /bin/csh 
        0.17 real         0.13 user         0.02 sys

[~]% time vim -X -c ':q' /bin/csh
        0.17 real         0.13 user         0.02 sys


Ran the tests several times ... Sometimes it varies by .01 second, but it seems fairly consistent.
Carpetsmoker's avatar
10 years ago
r2, link
Carpetsmoker
Martin
this post has been archived.
Carpetsmoker's avatar
9 years ago
r1, link
Carpetsmoker
Martin
There's a vi StackExhange site now :-)

https://vi.stackexchange.com/

I've learned a lot more about Vim in the last month.
bluet's avatar
9 years ago
link
bluet
I recently added:

map <silent> <leader>x :silent exe "!chmod +x %" \| w<CR>:e<CR><C-L>
andre's avatar
9 years ago
link
andre
I started using syntastic recently. It's really cool: https://github.com/scrooloose/syntastic
Carpetsmoker's avatar
9 years ago
link
Carpetsmoker
Martin
Yeah, syntastic is cool, although the Ruby validations whine a bit too much for my liking; especially the "assigned but unused variable" and "statement not reached" errors are more annoying than useful... I need to see if I can configure that...
bluet's avatar
9 years ago
link
bluet
I stopped using syntastic recently. It would lock up Vim for seconds at a time. Now I run entr in a second terminal instead.

http://entrproject.org/
bluet's avatar
9 years ago
link
bluet
I finally fix one of the most annoying things in my Vim.

set nrformats-=octal
lucas's avatar
9 years ago
link
lucas
i ❤ demo
i was playing with entr for a while. pretty fun/handy.
phi_'s avatar
5 years ago
link
phi_
... and let the Earth be silent after ye.
I set my tab size to 4 and tab everything multiple times so it looks like a normal 8-space tab on my end just so if I ever send the file to someone else it'll be annoying as hell.
bluet's avatar
5 years ago
link
bluet
I put byte order marks at the end of my files.
phi_'s avatar
5 years ago
link
phi_
... and let the Earth be silent after ye.
That's vicious.
lucas's avatar
5 years ago
link
lucas
i ❤ demo
what's that do? just a latent bomb to cause a mess for non-unicode programs?
asemisldkfj's avatar
4 years ago
r2, link
asemisldkfj
the law is no protection
this is basic as f, but I somewhat recently learned about the 'w' and 'b' keyboard shortcuts in command mode to kind of "jump" between "words". so many vi/vim things to discover! I'm still using the same vimrc I was using like 10 years ago lol. (I mostly use vs code for actual code/script editing—terraform and python mostly—and just use vi/vim for quick stuff. oh and I use the vi/vim extension with vs code because I don't know how to edit text without at least hjkl cursor motion keys.)
Carpetsmoker's avatar
4 years ago
link
Carpetsmoker
Martin
I used Vim for years and didn't know a whole lot of fairly basic stuff; it wasn't until the Vi & Vim Stack Exchange site was launched that I started answering questions and diving deeper; I learned a lot from that.

There have actually been a number of pretty neat additions to Vim in the last year or two, and with LSP things like autocomplete and other IDE-like features work much smoother. I think Vim at this stage really is just the same as an IDE (if you want that, anyway).
Carpetsmoker's avatar
4 years ago
link
Carpetsmoker
Martin
LSP is the best thing to happen to Vim in years.

Also, in really new versions you can use "set spelloptions=camel" to spellcheck CamelCase words. as "camel" and "case".