CLI Part 4. Working With File Contents

vim - Text Editor

Oh all right, let us roll up our sleeves and get down and dirty with some vim.

Now vim has way to many features to even scratch the surface here - but I'll jot down some of the very basic concepts and commands. For the full run down, I'd recommend perusing through:

First thing you need to understand that there are two basic modes you can be in, command and insert. Command mode is for running, um... commands and insert mode is for writing your fancy code.

Switching Between Modes

To insert Mode:

  • i will take you from command mode into insert mode
  • a will take you from command mode into insert mode, one character to the right of the cursor
  • o will take you from command mode into insert mode, with a brand new line under the cursor

To command Mode:

  • esc will take you from insert mode into command mode

Insert Mode

So insert mode is where you will be making you textual changes. It works mostly as you'd expect it. Type what you want, navigate with the arrow keys and you're away. In this mode commands are usually triggered in conjunction with the control key.

Completion:

  • ctrl + p automatic word completion
  • ctrl + x ctrl + f automatically completes filenames. This is so very handy for configuration files. It will by default match files within the current directory, but will also work with directory structure. Try typing "/usr/lo" then

Command Mode

Command mode is where you might get a unhinged. For almost everything that isn't writing in text, you do it here.

Most functions are prefixed with a colon, for example ":x".

The ones that I tend to use most often are:

Navigation:

  • :set nu to show line numbers
  • :set nonu to hide line-numbers
  • arrow keys for you know what
  • w moves one word forward
  • b moves one word back
  • $ moves to the end of a line
  • 0 moves to the start of a line

Searching:

  • /foo searches for a given string, in this case it would be "foo"
  • :set hlsearch highlights all the occurrences of the selected word
  • :nohlsearch removes the highlights
  • \* marks a selected word for searching occurrences of
  • n cycles through occurrences of previously selected word
  • $s/foo/bar/g will replace all occurrences of "foo" with "bar"

Editing:

  • x deletes a single character
  • dd while in command mode deletes an entire line

Syntax Highlighting

  • :syntax enable turns on syntax highlighting on
  • :set filetype If vim can't decide on a file type, this will set it. An example usage might ":set filetype=ruby"

Saving and Exiting:

  • :q will exit if there are no changes
  • :w will save/write any changes
  • wq will write, then exit vim
  • :w newfile will save to a new file

nano - Text Editor

If all that is a bit much, and your after something a little more straight forward, nono is probably what you're after. Simple, straight forward stuff.

Example:

$ nano myFile.txt

And some basic commands are:

  • ctrl + w the your search term for finding a string the document
  • ctrl + x to exit. You'll also be provided with a save / save as option
  • arrow keys for you know... going places

Comments:

Leave a comment