At this point in your Computer Science career, you've worked with at least one text editor: jpico
.
Love it or hate it, jpico
is a useful program for reading and writing plain ASCII text.
C++ programs1 are written in plain ASCII text.
ASCII is a convenient format for humans and programs2 alike to read and process.
Because of its simple and featureless interface, many people find editors like jpico
to be frustrating to use.
Many users miss the ability to use a mouse or simply copy/paste lines from files without bewildering keyboard shortcuts.
Fortunately, there are myriad text editors available.3 Many popular options are available to you on campus machines and can be installed on your personal computers as well! These editors offer many features that may already be familiar to you. Such features include:
- Syntax highlighting
- Cut, copy, and paste
- Code completion
Whether you're writing programs, viewing saved program output, or editing Markdown files, you will often find yourself in need of a text editor. Learning the features of a specific text editor will make your life easier when programming. In this lab, you will try using several text editors with the goal of finding one that fits your needs.
Several of the editors you will see do not have a graphical user interface (GUI). Although the ability to use a mouse is comfortable and familiar, don't discount the console editors! Despite their learning curves, many experienced programmers still prefer console editors due to their speed, stability, and convenience. Knowing a console editor is also handy in situations where you need to edit files on a machine halfway around the globe!4
Note: This chapter focuses on text editors; integrated development environments will be discussed later in the semester. Even if you prefer to use an IDE for development, you will still run into situations where a simple text editor is more convenient to use.
- Recognize the value of plain text editors.
- Familiarize yourself with different text editors available on campus machines.
- Choose a favorite editor; master it.
Note: Because this is your first pre-lab, the walkthrough will be completed in class.
Notepad++5 is a popular text editor for Windows. It is free, easy to install, and sports a variety of features including syntax highlighting and automatic indentation. Many people choose this editor because it is lightweight and easy to use.
Beyond the standard editing shortcuts that most programs use, Notepad++ has some key shortcuts that come in handy when programming. Word- and line-focused shortcuts are useful when editing variable names or rearranging snippets of code. Other shortcuts indent or outdent6 blocks of code or insert or remove comments.
In addition to those shortcuts, if your cursor is on a brace, bracket, or parenthesis, you can jump to the matching brace, bracket, or parenthesis with \keys{\ctrl+b}.
- \keys{\ctrl+\arrowkeyleft} / \keys{\arrowkeyright}: Move cursor forward or backward by one word
- \keys{\ctrl+\backdel}: Delete to start/end of word
- \keys{\ctrl+\shift+\backspace}: Delete to start/end of line
- \keys{\ctrl+l}: Delete current line
- \keys{\ctrl+t}: Transpose (swap) current and previous lines
- \keys{\ctrl+\shift+\arrowkeyup} / \keys{\arrowkeydown}: Move current line/selection up or down
- \keys{\ctrl+d}: Duplicate current line
- \keys{\ctrl+j}: Join selected lines
- \keys{\tab}: Indent current line/block
- \keys{\shift+\tab}: Outdent current line/block
- \keys{\ctrl+q}: Single-line comment/uncomment current line/selection
- \keys{\ctrl+\shift+q}: Block comment current line/selection
You can also select text in columns, rather than line by line. To do this, use \keys{\Alt+\shift+\arrowkeyup} / \keys{\arrowkeydown} / \keys{\arrowkeyleft} / \keys{\arrowkeyright} to perform a column selection, or hold \keys{\Alt} and left-click.
If you have selected a column of text, you can type to insert text on each line in the column or edit as usual (e.g., \keys{\del} deletes the selection or one character from each line). Notepad++ also features a column editor that can insert text or a column of increasing numbers. When you have performed a column selection, press \keys{\Alt+c} to open the column editor window.
\begin{figure}[!h] \centering \includegraphics[height=0.5\textheight]{01/npp-column-editor.png} \caption{Column Editor} \end{figure}
Notepad++ supports multiple cursors, allowing you to edit text in multiple locations at once. To place multiple cursors, you'll need to enable it first. Navigate to \menu{Settings > Preferences > Editing}, and toggle the box within the \menu{Multi-Editing Settings} box. After that, just hold \keys{\ctrl} and left-click everywhere you want a cursor. Then, you can type as normal and your edits will appear at each cursor location.
\begin{figure}[!h] \centering \includegraphics[height=0.5\textheight]{01/npp-multi-editing-enable.png} \caption{Enabling Multi-Cursor Editing} \end{figure}
For example, suppose we've written the declaration for a class named road
and that we've copied the member function declarations to an implementation file.
We want to scope them (road::width()
instead of width()
), but that's tedious to do one function at a time.
With multiple cursors, though, you can do that all in one go!
\newpage First, place a cursor at the beginning of each function name: \begin{figure}[!h] \centering \includegraphics[height=0.4\textheight]{01/npp-mult-cursors-1.PNG} \caption{Placing multiple cursors with Ctrl + left-click} \end{figure}
Then, type road::
. Like magic, it appears in front of each function:
\begin{figure}[!h]
\centering
\includegraphics[height=0.4\textheight]{01/npp-mult-cursors-2.PNG}
\caption{Typing \texttt{road::} inserts that text at each cursor location}
\end{figure}
A document map can be handy when navigating large files7. It shows a bird's-eye view of the document; you can click to jump to particular locations.
The document map can be enabled by clicking \menu{View > Document Map}
Notepad++ has a multitude of settings that can configure everything from syntax highlight colors to keyboard shortcuts. You can even customize some settings per programming language, including indentation. One common setting is to switch Notepad++ to use spaces instead of tabs:
Notepad++ has support for plugins; you can see a list of them here.8 Unfortunately, plugins must be installed to the same directory Notepad++ is installed in, so you will need to install Notepad++ yourself to use plugins.
Programmers like to program. Some programmers like pretty things. Thus there is Atom.
Atom is a featureful text editor that is developed by GitHub. Designed with customization in mind, Atom is built on top of the engine that drives the Google Chrome web browser. Atom allows users to customize just about every feature that it offers. Style can be changed using cascading style sheets9 and behavior can be changed using JavaScript.10
Additionally, being a hip-and-trendy\texttrademark{} piece of software, you can install community packages written by other developers. In fact, if you find that Atom is missing some particular behavior, you can create a package and make it available to the world, as well!11
Atom has a GUI, so it is mouse friendly and human friendly, too.
\begin{figure}[!h] \centering \includegraphics[height=0.3\textheight]{01/atom-window.png} \caption{One Atom window with Tree View on the left and an empty pane on the right} \end{figure}
Using \keys{\ctrl+\textbackslash} (or \menu{View > Toggle Tree View}), you can toggle Atom's Tree View. The Tree View is a convenient tool for browsing files within a folder or subfolders. By clicking the down arrow to the left of a folder, you can see its contents. Simply double click a file to open it up.
As you double click files, they open up in new tabs.
\begin{figure}[!h] \centering \includegraphics[height=0.3\textheight]{01/atom-tabs.png} \caption{Atom with multiple tabs in one pane} \end{figure}
To switch between tabs, simply click on them at the top of the window. It works much the same way as browser tabs do.
Keep an eye on your tabs!
Atom will indicate when a file has changed and needs to be saved.
This can be very helpful when you find yourself asking "why is g++
still complaining?".
\begin{figure}[!h] \centering \includegraphics[height=0.3\textheight]{01/atom-split-pane.png} \caption{One window split into two panes} \end{figure}
In addition to opening files in several tabs, you can display several files at once in separate panes. Each pane has its own collection of tabs.
You can split your window into left-and-right panes by right-clicking in the tabs area and choosing \menu{Split Right} or \menu{Split Left}. You can split your window into top-and-bottom panes by right-clicking in the tabs area and choosing \menu{Split Down} or \menu{Split Up}. You can also close panes by choosing \menu{Close Pane}.
\begin{figure}[!h] \centering \includegraphics[height=0.3\textheight]{01/atom-command-palette.png} \caption{The Atom command palette (i.e., your best friend forever).} \end{figure}
You may notice that Atom's drop-down menu options are sparse. There is not much to choose from. Don't fret!12
Most of Atom's functionality is accessible using its command palette. To open the command palette simply type \keys{\ctrl + \shift + p}. The command palette is the place to search for any fancy thing you might want to do with Atom.
Any.
Fancy.
Thing.
You can even use it to accomplish a lot of the tasks you would otherwise use your mouse for!
For example, you can split your pane using the pane:split-right
command in the command palette.
Many of the commands have corresponding keybindings, as well. These are very handy, as they can save you a lot of command typing.
\begin{figure}[!h] \centering \includegraphics[height=0.3\textheight]{01/atom-settings.png} \caption{Atom's settings open in a new tab. You can search through its keybindings here.} \end{figure}
If you open up Atom's settings (using the menu or command palette), you'll find quite a few bells and whistles that you can customize. As you explore these options, take note that you can search for keybindings here. Atom has a helpful search tool that makes it easy to quickly find the keybinding for a particular command.
If you don't see a keybinding for a command you like, just create your own! You can also choose preset keymaps to make Atom behave like other text editors including (but not limited to) emacs!
nano
is a command-line text editor for Linux, Windows, and macOS.
People choose this editor because it is easy to use (as command-line editors go), has syntax highlighting, and is usually installed on Linux systems.
It may seem simple, but it has a surprising number of features that most people are unaware of.
Many features draw inspiration from emacs
, so you may observe some parallels between the two editors.
(Historical note: nano
was created as a Free as in Freedom13 replacement for pico
.14
pico
is a small (eh? eh?) text editor that came with the PINE newsreader15 and was designed to be easy to use.16)
You can open files in nano
by passing them as command-line arguments:
$ nano my-cool-file.txt
At the bottom of the nano
screen are two lines that show commonly used commands.
For more detailed documentation, \keys{\ctrl + g} displays a summary of all the available keyboard shortcuts.
The notation for controls may be unfamiliar to you. In Unix-land, ^
is shorthand for the \keys{\ctrl} key.
So, for instance, ^X
corresponds to \keys{\ctrl + x}.
M-U
is read as "meta-U"; typically this is equivalent to pressing \keys{\Alt + u}.
If you'd rather exercise your pinky finger (i.e., press \keys{\ctrl} a lot) than use the arrow keys, you can move your cursor around with some commands:
- \keys{\ctrl + f}: Forward (right) one character
- \keys{\ctrl + b}: Back (left) one character
- \keys{\ctrl + p}: Up one line
- \keys{\ctrl + n}: Down one line
- \keys{\ctrl + a}: Beginning of line
- \keys{\ctrl + e}: End of line
You can also move by word; \keys{\ctrl + \arrowkeyright} or \keys{\ctrl+Space} moves forward one word, and \keys{\ctrl + \arrowkeyleft} or \keys{\Alt + Space} moves back one word.
Furthermore, \keys{\Alt + )} and \keys{\Alt + (} jump to the next or previous paragraph.
\keys{PgUp} and \keys{PgDn} move up and down one screen at a time. Alternatively, \keys{\ctrl+y} and \keys{\ctrl+v} do the same thing.
Analogously, to jump to the beginning of a file, press \keys{\ctrl+home}, and to jump to the end, \keys{\ctrl+end}.
If there's a particular line number you want to jump to (for instance, if you're fixing a compiler error), press \keys{\ctrl+\shift+-}, then type the line number to go to and press \keys{\enter}. Alternatively, to see where you are in a file, press \keys{\ctrl+c}.
These are pretty easy. Undo is \keys{\Alt + u}; redo is \keys{\Alt + e}.
You: "So, nano
is kinda cool. But \keys{\ctrl+c} and \keys{\ctrl+v} both mean something other than 'copy' and 'paste'. Can't I just use the normal clipboard?"
Ghost of UNIX past: "It's 1969 and what on earth is a clipboard?"
You: "You know, the thing where you select some text and then copy it and you can paste that text somewhere else."
GOUP: "It's 1969 and what is 'select some text'?????"
You: "Uh, you know, maybe with the mouse, or with the cursor?"
GOUP: "The what now?"
You: "????"
GOUP: "????"
You: "?????????"
GOUP: "?????????!"
Seriously, though, the concept of a system-wide clipboard wasn't invented until the 1980s, when GUIs first became available.17
Before that, every terminal program had to invent its own copy/paste system!
In nano
, the clipboard is known as the 'cutbuffer'; text can be cut or copied to the cutbuffer and then 'un-cut' (i.e., pasted).
You can cut one line with \keys{\ctrl + k}, or cut from the cursor to the end of the file with \keys{\Alt + t}. \keys{\Alt + 6} copies one line, rather than cutting it.
You can repeat a cut or copy command to add more to the last thing cut or copied. For example, to cut several lines at once, just keep pressing \keys{\ctrl + k}. When you paste, all the lines will get pasted as one piece of text.
If you want to cut a selection of text, press \keys{\ctrl + \shift + 6} to start selecting text. Move the cursor around like normal; once you have completed selecting, press \keys{\ctrl + k} to cut the selection or \keys{\Alt + 6} to copy it.
Cut/copied text goes to the cutbuffer. To un-cut (paste) the contents of the cutbuffer, press \keys{\ctrl + u}.
\keys{\ctrl+w} lets you search for text. Type the text you want to search for and press \keys{\enter}. \keys{\Alt+w} will jump to the next search result.
Alternatively, before pressing \keys{enter}, you can toggle a number of search features:
- \keys{\Alt + c} to make the search case-sensitive
- \keys{\Alt + b} to search backwards, instead of forwards
- \keys{\Alt + r} to search using a regular expression (see the Regular Expressions chapter for more)
- \keys{\ctrl + r} to interactively replace each match with a given replacement string (you can use backreferences in the replacement if you are searching by regular expression)
To search-and-replace only in part of a file, you can set a mark (with \keys{\ctrl + \shift + 6} at one end of the region to replace in, navigate to the other, then press \keys{\ctrl+w} to replace only in the marked area.
nano
refers to open files as 'buffers'.
It displays only one buffer at a time, but it can have multiple buffers open simultaneously.
By default, \keys{\ctrl + r} reads the contents of a file (or the output of a command) into the current buffer. This makes it easy to make copies of files or to save the output of commands. To do this, press \keys{\ctrl + r} and enter a filename to read in. Once you've pressed \keys{\ctrl + r}, you can press \keys{\ctrl + t} to open a file browser, which is quite nifty! Alternatively, you can press \keys{\ctrl + x} to execute a command and put its output in the current buffer.
To open a file in a new buffer, press \keys{\ctrl + r}, then \keys{\Alt + f}. Then open a file or execute a command; the file or command output will appear in a new buffer.
You can switch between open buffers with \keys{\Alt + ,} and \keys{\Alt + .}. The top left corner shows how many buffers are open and which buffer you're currently displaying.
nano
looks for a configuration file in ~/.nanorc
and gives second priority to settings in /etc/nanorc
.
Some handy options:
set tabsize 4
: sets indentation width to 4 columnsset tabstospaces
: uses spaces for indentation, rather than tabsset linenumbers
: show line numbersset mouse
: enables the mouse!
(You can read more about mouse support here.)
Emacs is a command-line and GUI text editor for Linux, Windows, and macOS. Many joke that Emacs is so featureful that it is the only program you need to have installed on any computer. Some have taken this to an extreme and shown as a proof of concept that you can use Emacs as your operating system. Although that's a fun fact, you shouldn't actually do that.
Emacs was originally developed using a keyboard known as the space-cadet keyboard. Its layout is similar to, though notably different from, today's typical keyboard layout. One such difference is that the space-cadet had a Meta key, which we no longer have today. Another difference is the layout of modifier keys. Many of the Emacs keybindings (keyboard shortcuts, sort of) felt natural for space-cadet users but feel like insane acrobatics today. When starting to use Emacs, many users will find that reaching for Alt, Control, and Escape leaves their pinky fingers feeling tired and swollen. This has known as "Emacs pinky". Prolonged use of Emacs will lead to inhuman pinky strength which can be used with measurable success in combat situations.
Success with Emacs boils down to your development of muscle memory for its vast collection of keybindings. Once you have the basics down, you will find yourself angry about having to ever use a mouse. Emacs provides a tutorial that you can access from any Emacs installation. After launching Emacs, simply type \keys{\ctrl+h} followed by \keys{\ctrl+t} to start the tutorial. The tutorial is just like any other editable file, so you can play with it as you please. When you're done, simply exit Emacs with \keys{\ctrl+x} followed by \keys{\ctrl+c}. Your changes to the tutorial won't be saved.
The command used to start Emacs is simply emacs
.
Just like nano
, you can open specific files by listing them as arguments to the command.
$ emacs main.cpp
When it starts, Emacs will first check to see whether or not it has the ability to open any GUI windows for you.18 Assuming it can, Emacs will opt to start its GUI interface. The Emacs GUI is no more featureful than the command-line interface. Sure, you have the ability to reach for your mouse and click the Cut button, but that is no faster than simply typing \keys{\ctrl+k}.
In the name of speed and convenience, many Emacs users choose to skip the GUI.
You can start Emacs without a GUI by running emacs -nw
.
The -nw
flag tells Emacs...19
Dear Emacs,
I know you're very fancy, and you can draw all sorts of cute shapes. That scissor you got there is dandy, and your save button looks like a floppy disk isn't that so great?
Please don't bother with any of that, though. I just want you to open in the command-line like
nano
so that I can get some work done and move on with my life.With love, Me, the user.
If you choose to use the GUI, you should be aware of the following: Emacs is still quirky and it is not going to behave like Notepad++ or Atom. Cut, copy, and paste, for example, are not going to work the way you expect. It is really worth your time to get familiar with Emacs before you jump in blind.
To use Emacs (at all, really) you need to know its keybindings. Keybindings are important enough that this little bit of information deserves its own section.
Keybindings can be thought of as one or more keyboard shortcut. You may have to type a series of things in order to get things to work. What's more -- if you mess up, you'll likely have to start again from scratch.
Keybindings are read left to right using the following notation:
- The
C-
prefix indicates you need to hold the Control key while you type - The
M-
prefix indicates you need to hold the Alt key (formerly Meta key) while you type - Anything by itself you type without a modifier key.
Here are a handful of examples:
C-f
(\keys{\ctrl+f}) -- Move your cursor forward one characterM-w
(\keys{\Alt+w}) -- Copy a regionC-x C-c
(\keys{\ctrl+x} followed by \keys{\ctrl+c}) -- Exit EmacsC-u 8 r
(\keys{\ctrl+u} followed by \keys{8} followed by \keys{r}) -- Type 8 lowercaser
's in a row.
You can always ask Emacs what a keybinding does using C-h k <keybinding>
.
For example,
C-h k C-f
-- What doesC-f
do?C-h k C-x C-c
-- What doesC-x C-c
do?
Finally, if you done goofed, you can always tell Emacs to cancel your keybinding-in-progress.
Simply type C-g
.
According to the Emacs help page...
C-g
runs the command keyboard-quit...this character quits directly. At a top-level, as an editor command, this simply beeps.
As mentioned, you can also use C-g
to get your fill of beeps.
It is worth mention that every keybinding just runs a function in Emacs.
For example, C-f
(which moves your cursor forward) runs a function called forward-char
.
You can run any function by name using M-x
.
M-x
creates a little command prompt at the very bottom of Emacs.
Simply type the name of a command there and press Enter to run it.
For example, if you typed M-x
and entered forward-char
in the prompt and pressed Enter, your cursor would move forward one character.
Granted, that requires... 13?... More keystrokes than C-f
, but by golly, you can do it!
M-x
is very useful for invoking commands that don't actually have keybindings.
Although you can use your arrow keys to move your cursor around, you will feel much fancier if you learn the proper keybindings to do so in Emacs.
Moving by character:
C-f
Move forward a characterC-b
Move backward a character
Moving by word:
M-f
Move forward a wordM-b
Move backward a word
Moving by line:
C-n
Move to next lineC-p
Move to previous line
Moving around lines:
C-a
Move to beginning of lineC-e
Move to end of line
Moving by sentence:
M-a
Move back to beginning of sentenceM-e
Move forward to end of sentence
Scrolling by page:
C-v
Move forward one screenful (Page Down)M-v
Move backward one screenful (Page Up)
Some other useful commands:
C-l
Emacs will keep your cursor in place and shift the text within your window. Try typingC-l
a few times in a row to see what it does.C-s
starts search. After you typeC-s
, you will see a prompt at the bottom of Emacs. Simply type the string you're searching for and press Enter. Emacs will highlight the matches one at a time. Continue to typeC-s
to scroll through all the matches in the document.C-g
will quit.
Type C-_
to undo the last operation.
If you type C-_
repeatedly, Emacs will continue to undo actions as far as it can remember.
The way Emacs saves actions takes a little getting used to.
Undo actions are, themselves, undo-able.
The consequences of this are more obvious when you play around with C-_
yourself.
To add further quirkiness, Emacs doesn't have redo. So don't mess up, or you're going to have to undo all your undoing.
You can save a document with C-x C-s
.
If necessary, Emacs will prompt you for a file name.
Just watch the bottom of Emacs to see if it's asking you any questions.
You can quit Emacs with C-x C-c
.
If you have anything open that has not been saved, Emacs will prompt you to see if you really want to quit.
In Emacs, your "copied" and "cut" information is stored in the "kill ring".21 The kill ring is...a ring that stores things you've killed (cut), so that you can yank (paste) them later.
Vocabulary:
- Kill - Cut
- Yank - Paste
In order to kill parts of a file, you'll need to be able to select them.
You can select a region by first setting a mark at your current cursor location with C-space
.
Then, simply move your cursor to highlight the stuff you want to select.
Use C-w
to kill the selection and add it to your kill ring.
You can also use M-w
to kill the selection without actually removing it (copy instead of cut).
If you want to get content out of your kill ring, you can "yank" it out with C-y
.
By default, C-y
will yank whatever you last killed.
You can follow C-y
with M-y
to circle through other things you've previously killed.
That is, Emacs will maintain a history of things you've killed.
That's right! Emacs' kill ring is more sophisticated than a clipboard, because you can store several things in there.
To understand why it's called the kill ring, consider the following scenario:
First, I kill "Kermit". Then, I kill "Ms. Piggy". Then, I kill "Gonzo".
Next, I yank from my kill ring. Emacs will first yank "Gonzo". If I use
M-y
to circle through my previous kills, Emacs will yank "Ms. Piggy". If I useM-y
again, Emacs will yank "Kermit".If I use
M-y
again, Emacs will yank "Gonzo" again.
You can circle through your kill ring as necessary to find previously killed content. Emacs will simply replace the yanked text with the next thing from the kill ring.
You can have several different files open in Emacs at once.
Simply use C-x C-f
to open a new file into a new buffer.
By default, you can only see one buffer at a time.
You can switch between the buffers using C-x b
.
Emacs will open a prompt asking for the name of the buffer you want to switch to.
You have several options for entering that name:
- Type it! Tab-completion works, so that's handy.
- Use your arrow keys to scroll through the names of the buffers.
If you're done with a buffer, you can kill21 it (close it) using C-x k
.
You can also see a list of buffers using C-x C-b
.
This will open a new window in Emacs.
You can switch between windows using C-x o
.
This is convenient if you want to, say, have a .h
file and a .cpp
file open at the same time.
C-x b
works the same for switching buffers, so you can tell Emacs which buffer to show in each window.
You can open windows yourself, too:
C-x 2
(runssplit-window-below
) splits the current window in half by drawing a line left-to-right.C-x 3
(runssplit-window-right
) splits the current window in half by drawing a line top-to-bottom.
And, of course, you can close windows, too.
C-x 0
closes the current windowC-x 1
closes every window except the current window. This command is very handy if Emacs opens too much junk.
Emacs stores all of its configuration using a dialect of the Lisp programming language.
The default location of its configuration file is in your home directory in .emacs/init.el
.
The init.el
file contains Lisp code that Emacs runs on start up (initialization).
This runs code and sets variables within Emacs to customize how it behaves.
Although you can (and sometimes have to) write your own Lisp code, it's usually easier to let Emacs do it for you.
Running the customize
command (M-x customize
) will start the customization tool.
You can use your normal moving-around keybindings and the Enter key to navigate through the customize
menus.
You can also search for variables to change.
For example:
- Run the
customize
command (M-x customize
) - In the search bar, type "indent-tabs". Then move your cursor to
[ Search ]
and press Enter. - Locate the
Indent Tabs Mode
option and press the[Toggle]
button by placing your cursor on it and pressing Enter. You'll notice that the State changes fromSTANDARD
toEDITED
. - Press the
[ State ]
button and choose option1
for Save for Future Sessions.
These steps will modify your init.el
file, so that Emacs will use spaces instead of tab characters whenever you press the tab key.
It may seem tedious, but customize
will always write correct Lisp code to your init.el
file.
customize
and other more advanced commands are available by default in Emacs.
As further evidence that it is nearly its own operating system, you can install packages in Emacs using its built-in package manager.
If you run the list-packages
command (M-x list-packages
), you can see a list of packages available for install.
Simply scroll through the list like you would any old buffer.
For instructions on installing packages and searching for packages in unofficial software repositories, refer to the Emacs wiki.
Vim is a command-line and GUI22 text editor for Linux, Windows, and macOS. It is popular for its power, configurability, and the composability of its commands.
For example, rather than having separate commands for deleting words, lines, paragraphs, and the like,
Vim has a single delete command (d
) that can be combined with motion commands to delete a word (w
), line (d
), paragraph ({
), etc.
In this sense, learning to use Vim is like learning a language: difficult at first, but once you become fluent it's easy to express complex tasks.
Vim offers a tutorial as a separate program: at a command prompt, run vimtutor
.
You can also access help in Vim by typing :help <thing you want help with>
.
The help search can be tab-completed.
To close the help window, type :q
.
Vim is what's known as a 'modal editor': keys have different meanings in different modes.
When you start Vim, it is in 'normal' mode; here, your keys will perform different commands -- no need to press \keys{\ctrl} all the time!
However, usually when you open a text file, you want to, you know, type some text into it.
For this task, you want to enter 'insert' mode.
There are a number of ways to put vim
into insert mode, but the simplest is just to press \keys{i}, which drops you into insert mode
wherever your cursor is.
Some other ways to get into insert mode:
- \keys{I}: Insert at beginning of line
- \keys{a}: Insert after cursor (append at cursor)
- \keys{A}: Insert at end of line (Append to line)
- \keys{o}: Insert on new line below cursor
- \keys{O}: Insert on new line above cursor
When in insert mode, you can move around with the arrow keys.
To get back to normal mode, press \keys{\esc} or \keys{\ctrl + c}.
(Many people who use vim
swap \keys{Caps Lock} and \keys{\esc} to make switching modes easier.)
In normal mode, you can move around with the arrow keys, but normal mode also features a number of motion commands for efficiently moving around files. Motion commands can also be combined with other commands, as we will see later on.
Some common motions:
- \keys{j}/\keys{k}/\keys{h}/\keys{l}: up/down/left/right23
- \keys{^{}}/\keys{$}: Beginning/end of line
- \keys{w}: Next word
- \keys{e}: End of current word, or end of next word
- \keys{b}: Back one word
- \keys{%}: Matching brace, bracket, or parenthesis
- \keys{gg}/\keys{G}: Top/bottom of document
Commands can be repeated a number of times; for instance, 3w
moves forward three words.
One very handy application of the motion keys is to change some text with the \keys{c} command.
For example, typing c$
in normal mode deletes from the cursor to the end of the line and puts you in insert mode
so you can type your changes.
Repeating a command character twice usually applies it to the whole current line; so cc
changes the whole current line.
Vim has a visual mode for selecting text; usually this is useful in conjunction with the change, yank, or delete commands. \keys{v} enters visual mode; motion commands extend the selection. If you want to select whole lines, \keys{V} selects line-by-line instead.
Vim also has a block select mode: \keys{\ctrl+v}. In this mode, you can select and modify blocks of text similar to Notepad++'s column selection feature. Pressing \keys{I} will insert at the beginning of the selection. After returning to normal mode, whatever you insert on the first row is propagated to all other rows. Likewise, \keys{c} can be used to change the contents of a bunch of rows in one go.
To undo a change, type \keys{u}. \keys{U} undoes all changes on the current line.
To redo (undo an undo), press \keys{\ctrl + r}.
In normal mode, you can save a file by typing :w
.
To save and quit, type :wq
or ZZ
.
If you've saved your file already and just want to quit, :q
quits; :q!
lets you quit without saving changes.
Vim has an internal clipboard like nano
. The command to copy (yank, in Vim lingo) is \keys{y}.
Combine this with a motion command; yw
yanks one word and y3j
yanks 4 lines.
As with cc
, yy
yanks the current line.
In addition to yank, there is the \keys{d} command to cut/delete text; it is used in the same way.
Pasting is done with \keys{p} or \keys{P}; the former pastes the clipboard contents after the character the cursor is on, the latter pastes before the cursor.
While Vim lacks a killring, it does allow you to use multiple paste registers with the \keys{''} key.
Paste registers are given one-character names; for example, "ayy
yanks the current line into the a
register.
"ap
would then paste the current line elsewhere.
If you want to copy to the system clipboard, the paste register name for that is +
. So "+p
would paste from the system clipboard.
(To read about this register and other special registers, type :help registers
.)
You can indent code one level with \keys{$>$} and outdent with \keys{$<$}.
Like \keys{c}, these must be combined with a motion or repeated to apply to the current line.
For instance, >%
indents everything up to the matching '}' (or bracket or parenthesis) one level.
Vim also features an auto-indenter: \keys{=}.
It is incredibly handy when copying code around.
For example, gg=G
will format an entire file (to break the command down, gg
moves to the top of the file, then =G
formats to the bottom).
In Vim terminology, every open file is a 'buffer'. Buffers can be active (visible) or hidden (not on the screen). When you start Vim, it has one window open; each window can show a buffer.
Working with buffers:
:e <filename>
opens (edits) a file in a new buffer. You can use tab completion here!:bn
and:bp
switch the current window to show the next or previous buffer:b <filename>
switches to a buffer matching the given filename (tab completion also works here):buffers
shows a list of open buffers
Working with windows:
:split
splits the current window in half horizontally:vsplit
splits the current window vertically- \keys{\ctrl+w} \keys{w} switches focus to the next window
- \keys{\ctrl+w} \keys{h}/\keys{j}/\keys{k}/\keys{l} switches focus to the window above/left/right/below the current window
Vim also has tabs!
:tabe
edits a file in a new tabgt
andgT
switch forward and backward through tabs
To close a window/tab, type :q
. :qall
or :wqall
let you close / save and close all buffers in one go.
Vim's user configuration file is located at ~/.vimrc
or ~/.vim/vimrc
.
You do not need to copy a default configuration; just create one and add the configuration values you like.
Vim has a mouse mode that can be used to place the cursor or select things in visual mode.
In your config file, enter set mouse=a
.
To use four-space tabs for indentation in Vim, the following two options should be set:
set tabstop=4
set expandtab
\newpage
Note: Because this is your first pre-lab, questions will be answered in class.
For each of the following editors...
- Notepad++
- Atom
- Nano
- Vim
- Emacs
... figure out how to do the following:
- Open a file
- Save a file
- Close a file
- Exit the editor
- Move your cursor around
-
Up/down/right/left
-
Skip words
-
- Edit the contents of a file
- Undo
- Cut / Copy / Paste ("Yank")
-
Whole lines
-
Select/highlight areas of text
-
- Open two files at once (tabs/splits/whatever)
-
Change between open files
-
View a list of open files
-
Return to a normal view/frame (just a single file)
-
- Configure your editor
-
How do you change settings? (Tab width, cleanup trailing whitespace, UI colors, etc.)
-
\newpage
Name: ______________________________
For each editor, answer the following questions:
-
What do you like about it? \vspace{0.4\textheight}
-
What do you dislike about it?
\newpage Each editor has its own learning curves, but any seasoned programmer will tell you the value of knowing your text editor inside and out. Pick an editor and master it. If you get tired of one, try a different one!
Once you get comfortable with an editor, check out “plugins” for various languages. These can assist you as you code to correct your syntax as you go as well as various other features.
- The Notepad++ Website
- The Notepad++ Wiki, a handy reference for a lot of Notepad++ features
- Notepad++ Plugin Directory, a list of plugins you might want to install
- Notepad++ Source Code -- Notepad++ is free and open source, so you can modify it yourself!
- The Nano Website
- Nano FAQ, including some history of its development
- Nano Manual
- Nano Source Code
- Vim Website
- A Vim Cheat Sheet
- Another Vim Cheat Sheet
- Why do people use Vi?, with handy examples of how to combine Vim features together
- Vim Tips Wiki, full of useful "how do I do X" articles
- Vim Plugins Directory (there are a LOT of plugins...)
- Vim Source Code
Footnotes
-
And many other programming languages, for that matter. ↩
-
Including compilers. ↩
-
If you are reading this, you may ignore the rest of this chapter and instead learn
ed
, the standard editor. ↩ -
Thanks to cloud computing, it may not even be on the globe! ↩
-
Website: https://notepad-plus-plus.org/ ↩
-
Outdent (verb). Latin: To remove a tooth; English: The opposite of indent. ↩
-
Of course, this feature might encourage making large files rather than multiple manageable files... ↩
-
http://docs.notepad-plus-plus.org/index.php?title=Plugin_Central ↩
-
CSS is used to specify the design for websites, and it works in Atom, too. ↩
-
JavaScript is the language of the web. It makes web pages interactive! ↩
-
Just don't expect to get rich. ↩
-
Please, please don't fret. It'll be OK. Just keep a-readin', friend. ↩
-
Or, less tongue-in-cheek: licensed so users can modify and distribute the software as they please ---
nano
is licensed under the GNU GPL. ↩ -
A newsreader is a program for reading Usenet posts. Imagine Reddit, but in the 1980s. ↩
-
Well, easy to use for people (sometimes known as 'humanitty') who were already used to using Unix terminal programs! ↩
-
Command-line programs even predate computer screens! Before then, people used 'teletypes'--electric typewriters that the computer could control. They were incredibly slow to output anything, and there was no way to erase what had already been printed, so having a 'cursor' didn't really make much sense (how would you show it?). Some terminals didn't even have arrow keys as a result! ↩
-
For example, if you are using X forwarding, Emacs can detect the ability to open a GUI for you. ↩
-
Well, the
nw
in-nw
stands for no window, but Emacs takes it much more dramatically. ↩ -
\textcopyright{} 2009 Will Willis. Used with permission. ↩
-
Don't ask why Emacs has such violent terms. There's no keyboard-related excuse for that one. ↩ ↩2
-
The graphical version is cleverly named
gvim
. ↩ -
Why these letters? Two reasons: first, they're on the home row of a QWERTY keyboard, so they're easy to reach. Second, when Bill Joy wrote
vi
(which inspiredvim
), he was using a Lear Siegler ADM-3A terminal, which didn't have individual arrow keys. Instead, the arrow keys were placed on the h, j, k, and l keys. This keyboard is also the reason for why ~ refers to your home directory in Linux: ~ and Home are on the same key on an ADM-3A terminal. ↩