Vim tips and tricks

Thanks for this post! I've bookmarked this page.

Excelent summary of hotkeys.
Thanks.

How to change Tab width in vim?
Say I have a file, columns are separated with single space (or maybe tab). I want to change space to tab (or different Tab width) so that the columns are aligned and nicely padded.

bob 100 90 30000 40 5
brian 40 30 20 10 10000
rob 10 2 30 40 50
mike 60 88 92 100 80

I tried setting different tabstop width to find the best one as :set ts=4 ; or :set ts=8 , or :set ts=10 , but tab width did not change at all in vim. What did I miss? Thanks!

:set tabstop=number
     and
:set ts=number

will display lines being processed with tab stops set every number columns. It will not convert spaces to tabs. It will not replace sequences of spaces and tabs to make words on various lines magically align as columns in a table.

Thanks Don!
Ah ha! Yes, after I did :%s/\ /\t/g then :set ts=6 . It worked!
Does that mean I have to convert all the spaces with Tab first?
Thanks!

The vim or vi utility will display each <tab> found in the input as the number of <space>s needed to reach a tab stop. It doesn't matter whether the <tab>s are put in place before or after you use :set to set the tab stops.

In the post #1 of this thread, it is mentionned :

dd : Deletes three lines from current cursor position downwards.(also :d)

Shouldn't it be dd : Deletes the current line instead ???

I would also add :
cw : change 1 word begining from current cursor position
ncw : change n word begining from current cursor position
cW: change word treating only whitespace (or line ends) as word delimiters
C : Change the whole line from the current cursor position
D : Delete the current line from current cursor position (leaving in command mode contrary to the C command which do the same but switch to insert mode).
R : Replace (kind of overwrite, this command switch to insert mode)
My 2 cents.

Excellent post

Now I have to figure out how to switch from Insertion Mode to Command Mode :slight_smile:

It is simple; just hit the escape key.

Just another usefull vim tip, to customize your own skin :

To just view the setting, just use it with no argument :

:highlight

also shortened as

:hi

This will display the current settings.

To display the current setting of the Comment class, just enter:

:hi Comment

For example, if your comment lines appear in a Dark blue and you expect a lightblue instead (0,255,255) or #00ffff try :

:hi Comment ctermfg=6

You could then set your own settings in your $HOME/.vimrc ... activate :set syntax=on if necessary)

The different classes (Comment, Title, Constant, Special, ...) and much more, are documented at :
Vim documentation: syntax

Just play with the :highlight command , get use to it and enjoy !

not quite: The difference is what "word" means. "cw" (or any other command using "w" as a range assignment) will treat special characters as the end of the word, whereas "cW" will treat only whitespace (or line ends) as word delimiters. In the following text:

typeset foo=bar        # comment

if the cursor is under the "f" of "foo", then "cw" will replace "foo" with what you type afterwards, whereas "cW" will replace "foo=bar". To change the line from the cursor position to the line end use "C".

I hope this helps.

bakunin

1 Like

Ok fixed ... Thanks bakunin for noticing this point :b:

how to do case-insensitive search in VI ?

in vim
set
:set ic
switch (toggle)
:set ic!
unset
:set noic

--- Post updated at 08:39 ---

I always use only one command (toggle), for example
:set nu!
Print line numbers
:set nu!
Print line nonumbers

1 Like

Named buffers (or registers) have always been one of my most used features in vi; I can't overstate how useful they can be. Some versions of vi allow for double named cursors, but you can't count on it. All vi versions allow for 26 named buffers (a–z) that are available for copying and pasting text. To yank into a named buffer, precede the yank command with a double quote ( " ) and the character for the name of the buffer you want to load. For example:

"ayy - Yank current line into buffer a (or)
"a20yy - Yank the next 20 lines into buffer a (or)
"d7dd - Delete (and yank) the next 7 lines into buffer a (or)

You can then move and paste that content:

"aP - Put the contents of buffer a before the cursor
"ap - Put the contents of buffer a after the cursor

Show the contents of all named (and numbered) buffers:

:di
--- Registers ---
""   127.0.0.1^Ilocalhost^J
"0   # localhost is used to configure the loopback interface^J# when the system is
"1   ^J
"2   ^J
"3   Mon: 04 Tue: 04 Wed: 04 Thur: 04 Fri: 06 Sat: 02 Sun: 01 = 25 hours - Chris Hu
"4   ^J
"5   ^J
"6   ^J
"7   ^J
"8   tmsh create /ltm snatpool SNAT_${TAG3} members add { $SNAT }^J
"9   # create snat pool^J
"a   # when the system is booting.  Do not change this entry.^J
"b   ##^J
"c   127.0.0.1^Ilocalhost^J
"q   255.255.255.255^Ibroadcasthost^J
"-
":   1,4 "yc
"%   /private/etc/hosts
"/   sdil
3 Likes
Moderator comments were removed during original forum migration.

A post was split to a new topic: Help with script

that's what happens when you read documentation - you learn stuff :brain:

2 Likes

I'm trying to remove all the lines in a vcf file that starts with "PHOTO" AND contains the random character soup with the photo info. The problem is that each section (from PHOTO to a blank line) is noy a continuous line and the number of lines differs for each entry.

I'm looking for a way to delete from the line that starts with "PHOTO" and all the lines that follow until it reaches a blank line. Is that possible in vim?