멋진 열 레이아웃을 위해 vim으로 다시 포맷
이 데이터 세트를 CSV 파일로 가지고 있습니다
1.33570301776, 3.61194e-06, 7.24503e-06, -9.91572e-06, 1.25098e-05, 0.0102828, 0.010352, 0.0102677, 0.0103789, 0.00161604, 0.00167978, 0.00159998, 0.00182596, 0.0019804, 0.0133687, 0.010329, 0.00163437, 0.00191202, 0.0134425
1.34538754675, 3.3689e-06, 9.86066e-06, -9.12075e-06, 1.18058e-05, 0.00334344, 0.00342207, 0.00332897, 0.00345504, 0.00165532, 0.00170412, 0.00164234, 0.00441903, 0.00459294, 0.00449357, 0.00339737, 0.00166596, 0.00451926, 0.00455153
1.34808186291, -1.99011e-06, 6.53026e-06, -1.18909e-05, 9.52337e-06, 0.00158065, 0.00166529, 0.0015657, 0.0017022, 0.000740644, 0.00078635, 0.000730052, 0.00219736, 0.00238191, 0.00212762, 0.00163783, 0.000750669, 0.00230171, 0.00217917
보시다시피 숫자의 형식이 다르고 잘못 정렬되었습니다. vim에서 열을 올바르게 올바르게 정렬 할 수있는 방법이 있습니까?
1.33570301776, 3.61194e-06, 7.24503e-06, -9.91572e-06, 1.25098e-05, 0.0102828, 0.010352, 0.0102677, 0.0103789, 0.00161604, 0.00167978, 0.00159998, 0.00182596, 0.0019804, 0.0133687, 0.010329, 0.00163437, 0.00191202, 0.0134425
1.34538754675, 3.3689e-06, 9.86066e-06, -9.12075e-06, 1.18058e-05, 0.00334344, 0.00342207, 0.00332897, 0.00345504,0.00165532, 0.00170412, 0.00164234, 0.00441903, 0.00459294, 0.00449357, 0.00339737, 0.00166596, 0.00451926, 0.00455153
1.34808186291, -1.99011e-06, 6.53026e-06, -1.18909e-05, 9.52337e-06, 0.00158065, 0.00166529, 0.0015657, 0.0017022, 0.000740644,0.00078635, 0.000730052,0.00219736, 0.00238191, 0.00212762, 0.00163783, 0.000750669,0.00230171, 0.00217917
ctrl-v를 사용하여 섹션을 복사하여 붙여 넣는 것이 좋습니다. 힌트가 있습니까?
어떤 종류의 UNIX (Linux 등)를 사용하는 경우 column (1) 명령을 통해 치팅하고 필터링 할 수 있습니다.
:%!column -t
위의 내용은 문자열 리터럴 내부의 구분 기호를 잘못 구문 분석하므로 사전 처리 단계가 필요 하고이 파일의 구분 기호를 지정해야합니다.
%!sed 's/","/\&/' | column -t -s '&'
때로는 두 개의 열만 정렬하려고합니다. 이 경우 플러그인이 필요 없으며 다음과 같이 순수한 Vim 기능을 사용할 수 있습니다.
- 구분 기호를 선택하십시오. OP의 게시물에서 이것은 쉼표이며 내 예에서 이것은입니다
=
. - 전후에 공백을 추가하십시오. 나는 이것을 위해
s/=/= ...spaces... /
시각적 선택에 사용합니다. - 가장 긴 단어를 찾아 그 뒤에 커서를 놓습니다.
dw
수직 이동을 사용하여 여분의 공백을 모두 제거하십시오 .
이 기술의 예는 다음과 같습니다.
다른 플러그인을 설치하기에 충분히 자주 정렬해야 할 필요가 없기 때문에 이것이 내가 선호하는 방법이었습니다. 특히 많은 생각이 필요하지 않습니다.
sunny256이 제안했듯이 column
명령은 Unix / Linux 시스템 에서이 작업을 수행하는 좋은 방법이지만 순수한 Vim에서 수행하려면 (Windows에서도 사용할 수 있음) 가장 쉬운 방법은 Align 을 설치하는 것입니다 플러그인 후 다음을 수행하십시오.
:%Align ,
:%s/\(\s\+\),\s/,\1/g
첫 번째 줄은 쉼표의 항목을 정렬하고 두 번째 줄은 쉼표를 이전 값과 같은 높이로 옮깁니다. AlignCtrl
한 번에 전체 작업을 수행하는 사용자 지정 매핑을 정의하는 데 사용할 수 있지만 사용 방법을 기억할 수는 없습니다 ...
편집하다
항목 사이에 두 개의 공백이 필요하지 않고 하나의 명령으로이 작업을 수행하려는 경우 다음을 수행 할 수도 있습니다.
:%Align ,\zs
또한 Tabularize는 매우 좋습니다 http://vimcasts.org/episodes/aligning-text-with-tabular-vim/
이것은 vim 매크로를 사용하는 훌륭한 대답입니다 : https : //.com/a/8363786/59384- 기본적으로 매크로 기록을 시작하고 첫 번째 열을 포맷 한 다음 기록을 중지 한 다음 나머지 모든 줄에 대해 매크로를 반복하십시오.
해당 답변에서 복사 / 붙여 넣기 :
qa0f:w100i <Esc>19|dwjq4@a
Note the single space after the 100i, and the <Esc> means "press escape"--don't type "<Esc>" literally.
Translation:
qa -- record macro in hotkey a
0 -- go to beginning of line
f: -- go to first : symbol
w -- go to next non-space character after the symbol
100i <Esc> -- insert 100 spaces
19| -- go to 19th column (value 19 figured out manually)
dw -- delete spaces until : symbol
j -- go to next line
q -- stop recording macro
4@a -- run the macro 4 times (for the remaining 4 lines)
We now also have the fabulous EasyAlign plugin, written by junegunn.
Demonstration GIF from its README:
You could use the csv.vim plugin.
:%ArrangeColumn
However, this will not do exactly what you have asked: it will right adjust the contents of cells, whereas you have your values aligned by the decimal point or by the first digit.
The plugin has many other useful commands for working with CSV files.
also if you have very long columns it can be handy to disable default wrapping
:set nowrap :%!column -t
(note in debian you also have a further option for column -n which if you want to split multiple adjacent delimiters)
Pretty old question, but I've recently availed myself of an excellent vim plugin that enables table formatting either on the fly or after-the-fact (as your use case requires):
https://github.com/dhruvasagar/vim-table-mode
I just wrote tablign for this purpose. Install with
[sudo -H] pip3 install tablign
Then simply mark the table in vim and do
:'<,'>:!tablign
I wrote python script that allows users to basicly columize any type of text also outside of vim. Not sure if this will work for windows or mac users.
Usage when in vim.
:'<,'>!columnice =
This will use the equals sign as the delimeter. The delimeter is not thrown away though.
Here’s a pure Vim script answer, no plugins, no macros:
It might be most clear to start out with my problem’s solution as an example. I selected the lines of code I wanted to affect, then used the following command (recall that entering command mode from visual mode automatically prepends the “'<,'>”, so it acts on the visual range):
:'<,'>g``normal / "value<0d>D70|P`
Except I did NOT actually type “<0d>”. You can enter unprintable characters on the command line by pressing ctrl-v, then the key you want to type. “<0d>” is what is rendered on the command line after I typed ‘ctrl-v enter’. Here, it’s parsed by the “normal” command as the exit from “/” search mode. The cursor then jumps to “ value” in the current line.
Then we simply [D]elete the rest of the line, jump to column 70 (or whatever you need in your case), and [P]ut what we just deleted. This does mean we have to determine the width of the widest line, up to our search. If you haven’t put that information in your statusline, you can see the column of the cursor by entering the normal mode command ‘g ctrl-g’. Also note that jumping to a column that doesn’t exist requires the setting 'virtualedit'!
I left the search term for the :g(lobal) command empty, since we used a visual block and wanted to affect every line, but you can leave off using a visual selection (and the “'<,'>”) and put a search term there instead. Or combine a visual selection and a search term to narrow things more finely/easily.
최근에 배운 것이 있습니다. 복잡한 명령 모드 명령을 엉망으로 만들 경우 'u'(버퍼에 영향을주는 경우)로 실행을 취소 한 다음 "q :"를 눌러 일반 버퍼와 매우 유사한 특수 명령 히스토리 버퍼를 입력하십시오. . 행을 편집하고 Enter 키를 누르면 변경된 명령이 새 명령으로 입력됩니다. 처음부터 모든 것을 완벽하게 제형 화하는 것을 강조하지 않으려는 경우 필수 불가결합니다.
참고 URL : https://stackoverflow.com/questions/1229900/reformat-in-vim-for-a-nice-column-layout
'Programming' 카테고리의 다른 글
'(groovy.lang.Closure)'에 '종속성'을 적용 할 수 없습니다 (0) | 2020.07.16 |
---|---|
SQL Server : Case 문에서 UniqueIdentifier를 문자열로 변환 (0) | 2020.07.16 |
하나의 명령문에서 한 번에 여러 항목을 HashMap에 추가 (0) | 2020.07.16 |
Bash에서 파일의 마지막 수정 날짜를 인쇄하십시오. (0) | 2020.07.16 |
Visual Studio Code에 상자 선택 / 여러 줄 편집이 있습니까? (0) | 2020.07.16 |