SEARCH

Enter your search query in the box above ^, or use the forum search tool.

You are not logged in.

#1 2012-02-14 23:22:21

rstrcogburn
CrunchRanger
From: NM - The Land of Enchantment
Registered: 2010-06-12
Posts: 1,947
Website

ViM Tricks

Pop a wheelie on your favorite editor and post the means how.  Yes, this is a place to share your favorite vim tips and tricks with the whole community.

To start off I'll post my latest find with yall.  If you're like me and like tweaking .Xdefaults, there's some caveats.  For one vim doesn't always use .Xdefaults exclusively for it's highlighting.  The snippet below will translate your .Xdefaults to vim highlighting correctly without the need for a custom theme.

" syntax highlighting groups
hi Comment      ctermfg=12
hi Constant     ctermfg=6
hi Identifier   ctermfg=4
hi Statement    ctermfg=2
hi PreProc      ctermfg=1
hi Type         ctermfg=3
hi Special      ctermfg=5
hi Underlined   ctermfg=7
hi Ignore       ctermfg=9
hi Error        ctermfg=11
hi Todo         ctermfg=1

(that's for the tail end of a .vimrc and not .Xdefaults to clarify smile

Last edited by rstrcogburn (2012-02-14 23:23:46)


Pack em in snow!

Offline

Help fund CrunchBang, donate to the project!

#2 2012-02-14 23:39:20

gensym
#! Junkie
Registered: 2011-10-17
Posts: 447

Re: ViM Tricks

This backs up the files you edit under the directory .vim/backup (you have to create the directory first)

set backup
set backupdir=~/.vim/backup
 

'Multiple exclamation marks,' he went on, shaking his head, 'are a sure sign of a diseased mind.', {Eric}

Offline

#3 2012-02-14 23:44:42

rstrcogburn
CrunchRanger
From: NM - The Land of Enchantment
Registered: 2010-06-12
Posts: 1,947
Website

Re: ViM Tricks

@gensym  good one!  I know about a ~ in the same directory as the file edited but this presents an alternate backup method.  Kewl tip.


Pack em in snow!

Offline

#4 2012-02-14 23:59:10

gensym
#! Junkie
Registered: 2011-10-17
Posts: 447

Re: ViM Tricks

This does the same but in an other  directory smile

Last edited by gensym (2012-04-12 10:19:25)


'Multiple exclamation marks,' he went on, shaking his head, 'are a sure sign of a diseased mind.', {Eric}

Offline

#5 2012-02-15 02:05:02

nablacksax
#! Member
From: Houghton, MI
Registered: 2011-08-26
Posts: 58

Re: ViM Tricks

Can't wait to see what tips and tricks people add to this thread. I don't have any cool things to add and my .vimrc is nothing fancy. Thanks for starting this!

Offline

#6 2012-02-15 02:23:36

rstrcogburn
CrunchRanger
From: NM - The Land of Enchantment
Registered: 2010-06-12
Posts: 1,947
Website

Re: ViM Tricks

nablacksax, awesome dood.. same here. dunno why this isn't already going. TBH, I searched it hard before creation because it was wierd it didn't exist.


Pack em in snow!

Offline

#7 2012-02-15 22:01:17

mahatman2
#! Die Hard
From: Chattanooga TN
Registered: 2009-09-13
Posts: 551

Re: ViM Tricks

I found on the internet (and now I can't live without it) an idea to

inoremap jj <Esc>

. It's really sped up the whole movement from Input to Command mode, and when I use vim somewhere else I'm always backspacing!


Punch all your friends.

Offline

#8 2012-03-09 02:29:45

Herocks
New Member
Registered: 2012-03-09
Posts: 3

Re: ViM Tricks

(New to #! and the forums)

SPELLCHECK SHORTCUT
Have been using Vim more regularly of late and have ran into some useful tweaks. Here are a few lines you can add to your .vimrc to be able to easily activate the spellcheck function when in command mode: '  ,e <return> ' to toggle on english spellcheck. I also include ' ,f  <return> ' to toggle on the french spellcheck. Finally, I have '  ,,  <return> ' to turn off the spell check.

It would be nice to not have to hit <return> , but I am not so familiar with coding to figure it out. If someone knows the answer it would be a great addition to add. Also, people can obviously add any other language they would like.

____________________________________
let mapleader = ","
nmap <silent><leader>e :set spell spelllang=en
nmap <silent><leader>f :set spell spelllang=fr
nmap <silent><leader>, :set nospell
____________________________________

Cheers

Offline

#9 2012-04-12 10:18:15

gensym
#! Junkie
Registered: 2011-10-17
Posts: 447

Re: ViM Tricks

<Bump>
This is a simple one but I still find it useful

function! NuToggle()                                                          
    if(&number)                                                            
        set relativenumber                                                      
    elseif (&relativenumber)                                               
        set norelativenumber                                                    
    else                                                                        
        set number                                                              
    endif                                                                       
endfunc
nnoremap <silent><F2> :call NuToggle()<cr>

It basically switches between absolute line numbers, relative line numbers and no line numbers.
</Bump>

Last edited by gensym (2012-04-12 10:51:45)


'Multiple exclamation marks,' he went on, shaking his head, 'are a sure sign of a diseased mind.', {Eric}

Offline

#10 2012-04-12 19:08:58

mahatman2
#! Die Hard
From: Chattanooga TN
Registered: 2009-09-13
Posts: 551

Re: ViM Tricks

@HeRocks: you can avoid hitting enter by just putting <cr> at the end of each line.


Punch all your friends.

Offline

#11 2012-04-12 19:20:02

safetycopy
urban legend
From: The Chatsubo
Registered: 2010-04-03
Posts: 1,311

Re: ViM Tricks

^^ Nice, thanks for that smile

Here's a couple that allow you to edit your .vimrc a bit easier:

" Edit .vimrc with <leader>ev shortcut:
nnoremap <leader>ev :e! $MYVIMRC<CR>

" Re-source .vimrc on save so changes are effective immediately:
if has('autocmd')
    autocmd! BufWritePost .vimrc source %
endif

Last edited by safetycopy (2012-04-12 19:21:22)


i wonder if i missed the warning
Skinny Puppy, Love in Vein

Offline

#12 2012-04-12 19:35:49

SlowMutant
#! CrunchBanger
From: End-World
Registered: 2012-02-26
Posts: 138

Re: ViM Tricks

I really like the undo tree. Its a real tree and you can traverse it.

The g- / g+ commands go back chronologicaly. this lets you undo undos that were done time ago.
and :earlier goes back in time.
:later is the reverse of :earlier, ofc only till latest change wink.

Haven't seen any editor that can do this. But on unixoids I only use vim.

Cant wait for what this thread will bring forth.


Nothing 'real' is solid.

Offline

#13 2012-04-12 19:58:57

gensym
#! Junkie
Registered: 2011-10-17
Posts: 447

Re: ViM Tricks

SlowMutant wrote:

The g- / g+ commands go back chronologicaly. this lets you undo undos that were done time ago.
and :earlier goes back in time.

Thanks for that,I had no idea that this was possible!

Edit: This is not vim related per se, but it is still very useful if one uses vim through ssh in a remote box (which just happens to be ***slow***):
I have this in my .zshrc  / .bashrc files:

function vi {                                                                                   
    if [ -z "$(jobs | grep vim)" ]                                                 
    then                                                                                        
        vim $@                                                                                  
    else                                                                                        
        fg %vim $@                                                                              
    fi                                                                                          
}

If vim is suspended (i.e with Ctrl+Z), the function vi foregrounds that vim, it isn't it starts a new vim process.

Last edited by gensym (2012-04-12 20:42:26)


'Multiple exclamation marks,' he went on, shaking his head, 'are a sure sign of a diseased mind.', {Eric}

Offline

#14 2012-04-16 14:20:57

kbmonkey
#! Die Hard
Registered: 2011-01-14
Posts: 839
Website

Re: ViM Tricks

I'll be watching this thread for more tricks smile

If you use ViM to compose in mutt, add F8 spellcheck (requires aspell):

:autocmd FileType mail :nmap <F8> :w<CR>:!aspell -e -c %<CR>:e<CR>

Offline

#15 2012-04-16 15:12:28

SlowMutant
#! CrunchBanger
From: End-World
Registered: 2012-02-26
Posts: 138

Re: ViM Tricks

I only used the build in ":set spell" functionality. I don't know the difference to aspell, why do you use it in this case (or in general) ? It's a normal vim instance that you use for composing in mutt, right ?


Nothing 'real' is solid.

Offline

#16 2012-04-17 17:42:42

kbmonkey
#! Die Hard
Registered: 2011-01-14
Posts: 839
Website

Re: ViM Tricks

Just another way of doing things (tm), isn't variety great!

Offline

#17 2012-04-17 20:40:15

SlowMutant
#! CrunchBanger
From: End-World
Registered: 2012-02-26
Posts: 138

Re: ViM Tricks

The following could be a little more than just a vim-trick. Anyways...

This is how I mostly use ViM (apart from editing config files and writing the occasional script on linux):

I consider myself a very creative person. Creative in the sense that I do all kinds of stuff that comes to my mind but I'm often not getting things done (the boring/tedious) right now. So I need something to keep this fscking creativity (yeah sure, its the creativity not just laziness)  in check. This is where ViM comes into play.

The key for me is to stay organized and to know what to do next. With the VimWiki-plugin I use ViM as a personal wiki. Is has :Calendar integration so you can easily create a wiki-page for every day (I use this for daily todo lists I prepare in the evenings for the next day). Normal wiki pages are created by writing something in camel case like

 * [ ] TODO: fetch some water for the ProjectPlantWatering <-- 

The VimWiki uses the (a kind of) wikiwiki markup so the above translates to a bullet point with an unselected checkbox, some text and a WikiWord. You enter the Page of the WikiWord with <enter> in normal mode. Every Wikiword has its own plain text file. The markup is used for later html processing (I never use this) or other input help like checking checkboxes with C-Space.

Now you have thousand (hopefully not) uncheckt TODO:s in a lot of different files. With :vimgrep you can search these files for "* [ ] TODO:". You can then use :cw to go into quickfix mode and jump to the TODOs in the files to edit them (:[count]cn and C-Space checks the next TODO's checkbox).

A key mapping to open the quickfix window with all TODOs would look like

map <f7> <esc>:vimgrep /\* \[ \] TODO:/j **/*.wiki <cr><esc>:cw<cr>

The awesomeness of this (IMHO) comes from the combination of the three tools VimWIki, Calendar and vimgrep. Every tool by itself "is old hat". Together they help me to stay on top of things.

disclaimer: I don't want to leave the impression that I am always that organized. This only gives me some opportunities wink

PS: has anyone ideas on how to combine this with time/activity tracking in vim?

Last edited by SlowMutant (2012-04-18 07:37:08)


Nothing 'real' is solid.

Offline

#18 2012-04-18 19:32:18

kbmonkey
#! Die Hard
Registered: 2011-01-14
Posts: 839
Website

Re: ViM Tricks

Anybody knows how to integrate vim with the X clipboard?

I tried a couple of methods to achieve this, but none seem to work.

The default '*' or '+' X clipboard registers aren't compiled into the default #! vim (as per "vim --version | grep clipboard")

Offline

#19 2012-04-18 21:14:26

SlowMutant
#! CrunchBanger
From: End-World
Registered: 2012-02-26
Posts: 138

Re: ViM Tricks

try installing vim-gtk this enables +xterm_clipboard for my vim --version.

it's the an older statler and I have only ssh access. could just verify the --version output.


Nothing 'real' is solid.

Offline

#20 2012-04-18 21:20:15

gensym
#! Junkie
Registered: 2011-10-17
Posts: 447

Re: ViM Tricks

kbmonkey wrote:

Anybody knows how to integrate vim with the X clipboard?

I tried a couple of methods to achieve this, but none seem to work.

The default '*' or '+' X clipboard registers aren't compiled into the default #! vim (as per "vim --version | grep clipboard")

I use the console vim, so these lines in my .Xresources do the trick for me (though this is more of a vt100 workaround)



xterm.VT100.Translations:         #override \n\                                 
        Shift <KeyPress> Insert:  insert-selection(CLIPBOARD) \n\               
        <Btn2Up>:                 insert-selection(SELECT, CUT_BUFFER0) \n\     
        ~Shift<BtnUp>:            select-end(PRIMARY, CUT_BUFFER0) \n\          
        Shift<BtnUp>:             select-end(CLIPBOARD, CUT_BUFFER1)   

Shift+Insert copies from the x clipboard to the terminal.


'Multiple exclamation marks,' he went on, shaking his head, 'are a sure sign of a diseased mind.', {Eric}

Offline

#21 2012-04-19 08:51:20

SlowMutant
#! CrunchBanger
From: End-World
Registered: 2012-02-26
Posts: 138

Re: ViM Tricks

this does not use the vim buffers, right ?


Nothing 'real' is solid.

Offline

#22 2012-04-19 15:45:48

redgore
Member
Registered: 2011-03-08
Posts: 10
Website

Re: ViM Tricks

If you create files of the same type a lot then this trick is handy. I have it set-up for .php files but you can do it for any. In your .vimrc you need

autocmd BufNewFile *.php source ~/.vim/ftplugin/phpskel.vim

This will load the file you give (as far as im aware it doesnt have to be in ftplugin but im not 100% sure on that. And then in ~/.vim/ftplugin/phpskel.vim you put:

call setline(1, '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 transitional//EN" "http://w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">')
call setline(2, '<html xmlns="http://www.w3.org/1999/xhtml">')
call setline(3, '<head>')
call setline(4, '<meta http-equiv="content-type" content="text/html;charset=UTF-8" />')
call setline(5, '<title></title>')
call setline(6, '</head>')
call setline(7, '<body>')
call setline(8, '</body>')
call setline(9, '</html>')

Your file needs to be in the format

call setline(n,'this is my text')

where n is the line number and you replace 'this is my text' with the text you want in that line.

Offline

#23 2012-04-19 17:20:21

gensym
#! Junkie
Registered: 2011-10-17
Posts: 447

Re: ViM Tricks

SlowMutant wrote:

this does not use the vim buffers, right ?

No, it uses the x-clipboard and the primary buffer.


'Multiple exclamation marks,' he went on, shaking his head, 'are a sure sign of a diseased mind.', {Eric}

Offline

#24 2012-04-19 17:35:56

SlowMutant
#! CrunchBanger
From: End-World
Registered: 2012-02-26
Posts: 138

Re: ViM Tricks

@redgore I always did this with the registers as registers are preserved over vim sessions. But your solution is better somehow.

@gensym: when I said buffers I meant registers. answer's the same I guess.


Nothing 'real' is solid.

Offline

Help fund CrunchBang, donate to the project!

#25 2012-04-20 11:15:50

redgore
Member
Registered: 2011-03-08
Posts: 10
Website

Re: ViM Tricks

SlowMutant wrote:

@redgore I always did this with the registers as registers are preserved over vim sessions. But your solution is better somehow.

@gensym: when I said buffers I meant registers. answer's the same I guess.

At a guess portability would be the main benefit of doing it the way I do.

For those wanting to get their vim knowledge up I recommend reading A byte of vim, which is available free at: http://www.swaroopch.org/notes/Vim

Offline

Board footer

Powered by FluxBB

Copyright © 2012 CrunchBang Linux.
Proudly powered by Debian. Hosted by Linode.
Debian is a registered trademark of Software in the Public Interest, Inc.

Debian Logo