bits and pieces

this is the part where i explain things i've picked up about vim

this is where i point to the other things i have written

I wrote about command line here. most of the things i anticipate appearing on this page are going to be me learning more about this.

I wrote about the basics of vim here. i don't know how one would edit things in the tilde-verse without using vim, nano, or emacs...


practice, practice, practice

Like many people at work, I have a standing desk. Unlike many people at work, I do not have a chair for that standing desk. I assume that if I am given a chair, I will sit on it, instead of standing, which is the whole point of having a standing desk in the first place.

Similarly, I've heard there are ways to edit text that ends up in tilde.town without having to use vim, but I am not going to seek them out.

Editing lots of text (you've seen the rambling), makes it easier by the minute. For double bonus points, there is a Chrome plug in called Vimium, which allows you to practice vim while browsing the internet. :)

I assume it's a lot like yoga?


back to top

Cheat sheet of the stuff I covered in the vim blog

Quitters
:w writes your file
:w newname.txt writes your file and names it newname.txt
:q quits vim
:wq saves and quits
ZQ quits vim without saving
Getting Around
h to move the cursor one space left
j to go down one line
k to go up one line
l to go one space right
gg takes you to the beginning of the file
G takes you to the end of the file
0 takes you to the beginning of the line
$ takes you to the end of the line
w takes you to the next word
b takes you back a word
control + b takes you back a page
control + f takes you forward a page
control + d takes you down half a page
control + u takes you back a word
Exterminate!!!
dw to delete a word
d$ to delete from where you are to the end of the line
dd to delete the whole line you are on
dgg to delete from where you are all the way back to the beginning of the file.
ci<with your cursor in the middle of a <tag>, this will remove the words between the < & > and go into insert mode, so you can type in a new tag (works with any type of bracket, including {[()
citwith your cursor in the middle of text between <tags>, it will delete everything between the tags, so you can rewrite your content
Copy/Paste
v begins your selection
y ends and yanks your selection
p pastes your selection
yy yanks the line you are on
yap yanks the paragraph you are on
vi{ with your cursor in the middle of {two curly braces}, it will copy everything between them (so you can paste the same thing elsewhere) (works with any type of bracket, including <[()
Doing Things With Words
igoes into Insert mode so you can add text
I goes into insert mode at the begining of the current line
A goes into insert mode at the end of the current line
o adds a new line below the current line and goes into insert mode
O adds a new line above the current line and goes into insert mode
/bird searches for "bird" in your file
n scrolls through the returned hits for "bird" in your file after searching
:%s/dogs/cats/gc replaces all of the "dogs" in your file with "cats"
:2,6s/dogs/cats/g replaces all of the "dogs" between rows 2 and 6 with "cats"


back to top

Today I Learned:


back to top

11/22 - TIL - SETTING UP SHOP

The internet revealed the `-p` flag to me today (I can type `vim -p file1.html file2.html` to open both files and move between them with `:n` and `:N`, only the flag will put the names of the files at the top, like tabs!), so I decided today was a good day to figure out how to customize vim and make the layout work for me.

SPLITTING - What if I want to have more than one file open to edit--but I want to be able to see the files? I'll open up a file like I noramlly do with `vim file1.html`, and then I will type `:sp file2.html` -- this will cause file2 to appear on the bottom half of my screen! Awesome! Similarly, if I typed `:vsp file2.html`, it would cause file 2 to appear on the right side of the screen instead of the bottom.

You can combine `:sp` and `:vsp` however you like!

To move around between these panes (i think the commands mean "split pane & vertical split pane") you hold control + w then one of the following:

  • `w` to move your cursor one pane clockwise
  • `R` or `r` to rotate the panes around
  • `h`, `j`, `k`, or `l` to move left, down, up, or right, respectively
  • `n` to open a new file

I realized recently that if I'm editing in tilde.town, when I open vim, each line is numbered (super handy!), but when I open vim locally, there aren't numbers :(

To number your lines in the session you are in, you type `:set number`. Ig you ahve more than one pane open, it will only number the active pane.

But what if you want to always have numbered lines?

Glad you asked!

We get to explore the magical world of VIMRC

If you go to your home directory, whether it's local, or ssh (`cd`), there is a hidden file called .vimrc (reveal hidden files with `ls -a`). That's where all the defaults are hidden! The tilde.town file was already pretty well pre-populated, but I knew I wanted to wrap some text, since I've got so many medium-to-long-ish paragraphs.

I scanned the file until I saw 'set nowrap " don't warp display' (double quote mark is used to 'comment out' words that mean something to humans, but not to vim). To make sure this did what I wanted, I did a bit of Googling and also used vim's `:help` command, which I always forget to give mad props to.

I changed the line to `set wrap linebreak`, which made all of my long paragraphs wrap at the end of the window. It sounded like linebreak was going to make those breaks happen between words, rather than in the middle of words, but that didn't happen. I'll have to figure that out...

OK, now color schemes! What color should the background/standard text/highlighted text/syntex highlight style 1 through infinity be? I started off in vim. in command mode, I typed `:colorscheme ` and then hit tab. This will show you all of the loaded colorschemes. Choose one!

Better option: go through all of them! I chose one that I think paralysed my eyeballs for a few minutes at first. A lot of the differences were almost imperceptible (though I am editing HTML-- maybe they would be drastically different in another language).

Adventure for next time: tweaking the colorscheme I chose to be just right by editing ~/.vim/colors!


back to top

11/15 - TIL - COPYING FROM OTHER FILES

~dhgwilliam showed me this *super duper awesome thing!*

At some point, I learned I could type `:r filename.html` if I wanted to copy filename.html into my current file. The problem is, I normally just want to use the <head> section, not a whole file.

`:r !head filename.html -n 50` will pull the top 50 lines from filename.html.

take a minute to just think of all the possibilities

Breaking down the command:

  • `:r` says 'read me this upcoming thing'
  • `!` says LET'S PLAY IN COMMAND LINE!
  • `head` is a command line term for 'begining of a file' (while `tail is for the end)
  • `filename.html` = whatever you want to copy
  • `-n 50` says 'the number of lines i would like is 50' (if you don't include this, the default number is 10)

~~~~~~~~~~~~~~~~~~~~~~~~~~~

So any time I want to write about what I've learned in command line/bash/terminal and show the result of a new command, I can type `:r !(command)` ---for example, I'm going to type `:r !ls -l | grep duke` (and add html formatting around it for you afterwards).

-rw-r--r-- 1 shanx shanx 123096 Nov 10 01:18 black_duke.jpg

-rwxr--r-- 1 shanx shanx 684113 Oct 28 05:45 the_duke.jpg

seriously, I'm about to start mad scientist laughing! W00!


back to top

11/15 - THINGS I THNK I FORGOT TO WRITE DOWN

The longer your file is, the more you don't want to be stuck using arrows and j/k's to move up and down. I like using

  • `:77` to jump to row 77 (even if I have no idea what's there, I'm usually vaguely cognizant of how much further down a file a section I'm looking for is).
  • `control + d` to go down half a page.
  • `control + u` to go up half a page.
  • I don't usually use `control + b` to go back a full page or `control + f` to go forward a full page, but that's just probably a quirk of mine.
  • If I want to go to the very bottom of the page, I'll type `G` (which is the opposite of `gg`)
  • If I am using anchor tags for sections on a page, I'll use the search function. For example, when I want to update this, I type `/TIL`, hit return, then `n` to move to this section.


I know it's in the reference table (which I should really update), but find and replace is one of my favorite things. I just love doing stuff with words.

If you find that you habitually type 'anythign', you can use this to fix ALL THE THIGNS! `:%s/thign/thing/g` ~~~ `%` means 'check the whole file', `s` means 'substitute', and `g` means 'global' (or at least 'do this for everything you find').

Now let's say you wanted to leave some mispellings in place (like in the line ablve), or only wanted to change the name of a thing in part of your file. `:1,40s/thign/thing/gc` ~~~ we've replaced `%` with `1,40`, which means 'only check between rows 1 and 40- I don't care about the rest of the file'. You'll also notice that we added a `c` at the end. This means 'check before making the change'. When you use this, vim will highlight each instance of the word you are searching and ask "replace with thing (y/n/a/q/l/^E/^Y)?"

If you type:

  • `y` it says 'yes, replace this instance of thign with thing
  • `n` it says 'nope! leave this thign alone!'
  • `a` it says 'all of it! I'm done checking these thigns! Just replace them all!'
  • `q` it says 'quit this- i've replaced all the thigns that I'm going to replace and I don't need to see any more
  • `l` it says 'last one!'-It changes the highlighted thign and quits the search.
  • straight up, I don't know what `^E` and `^Y` do...


back to top

11/11 - TIL: DUAL WEILDING

So there you are, learning all sorts of wild and crazy things about unix and vim and irc and *, and you keep `:wq` and opening up a file to edit it, then `:wq` and opening up another file to edit it.

what if it didn't have to be like that?

While editing in one file, you can type `:e file_name.html` and it will open up the other file for you to edit. Whoah.

You can open tons of files- then see which ones you've got by typing `:buffers`-- this will show a list of open files with numbers before them. You can switch to a file by typing `:buffer #`, where # is the preceding number.

Yeah, that's cool and all, but it doesn't seem like it's saving that many keystrokes :(

`:q`uit whatever you're doing. Now, rather than getting into vim with `vim file_name.html` do it with `vim file_name.html other_file_name.html`. It opened both of them. And nowwwwwwww...

`:n` will take you to other_file_name.html! And from there, you can return with `:N`! Lovely!


UPDATE: the `-p` flag will add tabs! So if I type `vim -p vim.html cli.html other.html` in command line, vim will open up and have tabs at the top of the screen with the names of each file that is open. You navigte the same way with `:n` and `:N`


back to top

11/9 - TIL: FILL IN THE BLANKS

Have you ever been cruising through your file and you need to replace an element of some sort? Maybe you usedd the wrong tag. Maybe you need to replace a broken URL. Maybe you need to rewrite a function.

Meet your new vim BFFs: `ci` and `vi`.

If you place your cursor somewhere inside single quotes and from command mode run `ci'`, it will Clear the content between the `'`s and go into Insert mode, so you can replace it.

Similarly, if you use `vi<` when your cursor is between brackets, it will go into insert mode and highlight everything between, so you can `y`ank, `p`aste, or do whatever you want.

UPDATE: What if you don't want to replace the tag names- you want to replace the content BETWEEN the tags? `cit` seems to do the trick :)

example: `ci=^..^=`


back to top

11/8 - TIL: NEW WAYS TO "INSERT"!

So I've been cruising along in vim, and I will frequently forget to enter Insert mode before entering text. I noticed that I would start typing and it wouldn't be until a word or two in that text would get entered (or I would do something terrible like delete the line).

Now at the begining, I assumed that the text would start when I came to the first "i" in the sentence. Then I noticed that wasn't always the case, but I was so engrossed in whatever I was doing that I didn't check it out.

Tangetially, when editing files (especially HTML with all its tags) I would go to the end of a line with `$` and hit `i` and then have to move the cursor over with space to get past the closing > and hit enter to make a new line. Wow, writing it all out makes it sound arduous. But like many new things, when you are learning, you don't really notice the little things.

Well today the internet dropped a knowledge bomb on me: THERE ARE DIFFERENT WAYS TO GET INTO INSERT MODE.

  • `i` goes into Insert before your cursor (which is why I had to move over to get around a closing tag)
  • `a` goes into Insert AFTER your cursor
  • `A` goes into Insert AT THE END OF THE LINE! (new favorite, since it replaces both `$` and `i` for me)
  • `o` ADDS A LINE BELOW and goes into Insert
  • `O` ADDS A LINE ABOVE and goes into Insert

I think I'm going to be using `A`, `o`, and `O` a whole lot now :)


back to top

11/8 part deux - TIL: `~`

`~` will change the case of the letter the cursor is under (in command mode). Some may have noticed that I generally ignore the shift key when typing and refer to myself as "i" and other things that make the sticklers cringe. Whelp, now it is a bajillion times easier for me to edit those :)


back to top

11/6 - TIL: `P`

`P` (capitalized) pastes *in front* of your curser (super helpful if you need to add a bunch of tags to the beginings of lines)


back to top
www.reliablecounter.com
Click here