Category: Development

Vim and Emacs


After watching the Bloomerg video of Mr Torvalds, I found that he is using Emacs-like editor, which is uemacs, I immediately compiled my Vim and Emacs cheatsheet.

A programmer can write program without IDE, but cannot write without a good text editor. There are a lot of modern text editors. But I feel that learning using a text user interface (console-based) text is very much important. Imagine that you have to use SSH to write some scripts on the remote computer.

I personally inclined to Vim. This is because of Vi editor, which is a very common text editor in every Unix-like OS. Therefore, I learnt Vim.

Unlike Vim, Emacs does not have the visual mode or insert mode. Therefore, Emacs is easier to highlight/select the text.

The following is the comparison table (my personal cheatsheet),

vim emacs
Navigation
Left, down, up, right h, j, k, l ←, ↓, ↑, →
Left by word b Ctrl+←
Right by word w (begin), e (end) Ctrl+→
Begin line ^, 0 Home, Meta+a
End line $ End, Meta+e
First line gg Meta+<
Last line G Meta+>
Next single page, previous single page Ctrl+f, Ctrl+b Ctrl+v, Meta+v
Next half page, previous half page Ctrl+d, Ctrl+u
Go to line [:digit:]g Meta+x goto-line
Select v (visual mode) Shift+arrow
Copy y Meta+w
Paste p Ctrl+y
Cut d Ctrl+w
Edit i (insert mode), R (replace mode)
Delete x, d[hjkleb] Del
Append a
Open new line below, above o, O Enter
Undo, redo u, Ctrl+r Ctrl+/
Revert :e! Meta-x revert-buffer
Search
Forward, backward search /pattern, ?pattern Ctrl+s, Ctrl+r
Next, previous n, N Ctrl+s, Ctrl+r
Search and replace :%s/old/new/g, :%s/old/new/gc (with confirmation) Meta+% (interactive)
Toggle case sensitivity :set ic (insensitive case) Meta+c
Regular expression search forward, backward /pattern, ?pattern Ctrl+Meta+s, Ctrl+Meta+r
Regular expression search and replace :%s/old/new/g, :%s/old/new/gc (with confirmation) Ctrl+Meta+% (interactive)
Programming
Increase indent, decrease indent >, < (in V) Ctrl+x Tab (interactive)
Window
Horizontal, vertical split Ctrl+ws, Ctrl+wv Ctrl+x 2, Ctrl+x 3
Focus window Ctrl+w[hjkl] Meta+x windmove-[left|down|up|right]
Close window Ctrl+wq Ctrl+x 0
Close others Ctrl+wo Ctrl+x 1
Horizontal, vertical resize :res [+-][:digit:], :vert res [+-][:digit:] Ctrl+x { (horizontal shrink)

Ctrl+x } (horizontal enlarge)

Ctrl+x ^ (vertical enlarge)

Equal size Ctrl+w= Ctrl+x + (balance all)
Switch window Ctrl+ww Ctrl+x o
Tab (buffer and frame)
New tab :tabnew Ctrl+x 5 2 (frame)
Close tab :tabc Ctrl+x 5 0 (frame)
Close others :tab
Switch next, previous tab gt, gT Ctrl+x Left, Ctrl+x Right (buffer)
Switch n tab [:digit:]gt Ctrl+x b (interactive for buffer)
Move tab :tabmove [:digit:]
File
Quit :q, :q! (without changes) Ctrl+x Ctrl+c
Quit all :qa
Save :w Ctrl+x Ctrl+s
Save as :w filename Ctrl+x Ctrl+w
Save and exit :wq
Open
:o filename
Ctrl+x Ctrl+f
Miscellaneous
Cancel command Ctrl+g
Command Meta+x command

 

Text editors with window splitting


Using a good text editor during development is very important, as it can ease the job. There are two powerful and old text editor: Vim and Emacs. They can handle large files, split window, indent smartly, highlight syntax, work in console (without GUI). However, both text editors have steep learning curve. Users have to memorize a lot of hotkeys if possible.

My primary text editor is jEdit. It also provides approximately unlimited window splitting. jEdit can also installed with multiple plugins. Users can also create their own macro script using Java language. And it is cross platform. Similar to jEdit, Kate also works with window splitting.

Recently there are a lot modern text editors: Sublime Text, Lime, Atom, Brackets, Light Table, etc. Among these editors, I found that Atom is the one that can split the window. Atom is very interesting that it provides API for the developers to create the Atom packages. There are a lot of packages available in the repository. It is based on Chromium and using CoffeScript (which is similar to JavaScript). Therefore, the installation of the packages is just like installing extensions to Chromium. Creating packages using CoffeeScript is just like writing the user script in Chromium with JavaScript.

Atom seems like a very promising text editor. However, the current stage has a limitation. Atom now cannot handle session. It does not reopen all the files opened in the previous session. There are some packages related to the session, yet none of them works. Besides that, open recent file is available only through the packages.

Hopefully Atom will keep improving. It is full of potentiality.

Cross platform text editor: jEdit


As a software developer, a good text editor is always needed. I don’t like to use any IDE (Integrated Development Environment), but I think Eclipse is a very good IDE.

As a Linux user, at least need to know how to use a text-based editor: nano, vim, or emacs. vim and emacs are very powerful text editor. And learning to use these two text editors is really very troublesome. The main reason is there are too many commands or shortcuts need to memorize. But both of them has these good features:

  • File
    • quit, open, save – very basic file read/write functions
    • buffers (open multiple files) – Windows Notepad cannot do so with one window
  • Edit
    • insert, delete, select (or highlight) – very basic editing functions
    • cut, copy, paste (or yank) – also very basic editing functions
    • find, find next, find previous
    • search and replace (with regular expression)
    • undo, redo
  • Others
    • macro
    • window (split)
    • mark
    • folding
    • syntax highlighting
    • autoindent or smart indent

There are several good text editors I like: Notepad++ and jEdit. I personally prefer jEdit more than Notepad++, because jEdit is cross-platform, available in Linux. Besides that, jEdit has all the features mentioned above, and it is GUI. In Linux, there is a clone of Notepad++, that is Notepadqq.

Using the macro of jEdit is very easy. Besides that, anyone who knows Java programming can edit the macro easier. Recently, I have wrote several macros for commenting and uncommenting with “hash” and “slashes”, because jEdit only provides commenting, but not uncommenting.

The comment and uncomment macros  (skip here for those who are not interested)

The macro files are available here.

To install them, copy them to $HOME/.jedit/macros/ folder.

To use them, in jEdit, highlight the multiple lines that you want to comment, then go the Menu > Macros, select the macro.